blob: 5bdac8ddb7f3663c3e063c3c5c592736e65f6513 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080025#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020027#include "qapi-visit.h"
28#include "qapi/qmp-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010030#include "qapi/qmp/qjson.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020031#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000032#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/option.h"
34#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030035#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000036#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020038#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010039#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020040#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080041#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010042#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030043#include "trace/control.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020044#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000045
Don Slutz61979a62015-01-09 10:17:35 -050046#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Peter Maydelld915b7b2016-08-04 12:14:36 +010047 ", " QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040048
Anthony Liguoric227f092009-10-01 16:12:16 -050049typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010050 const char *name;
51 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050052} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010053
Federico Simoncelli8599ea42013-01-28 06:59:47 -050054enum {
55 OPTION_OUTPUT = 256,
56 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000057 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000058 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020059 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020060 OPTION_FLUSH_INTERVAL = 261,
61 OPTION_NO_DRAIN = 262,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050062};
63
64typedef enum OutputFormat {
65 OFORMAT_JSON,
66 OFORMAT_HUMAN,
67} OutputFormat;
68
Kevin Wolfe6996142016-03-15 13:03:11 +010069/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040070#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000071
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010072static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000073{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010074 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000075}
76
Fam Zhengac1307a2014-04-22 13:36:11 +080077static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
78{
79 va_list ap;
80
81 error_printf("qemu-img: ");
82
83 va_start(ap, fmt);
84 error_vprintf(fmt, ap);
85 va_end(ap);
86
87 error_printf("\nTry 'qemu-img --help' for more information\n");
88 exit(EXIT_FAILURE);
89}
90
blueswir1d2c639d2009-01-24 18:19:25 +000091/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080092static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000093{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010094 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040095 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +030096 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +030097 "QEMU disk image utility\n"
98 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +030099 " '-h', '--help' display this help and exit\n"
100 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300101 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
102 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300103 "\n"
malc3f020d72010-02-08 12:04:56 +0300104 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100105#define DEF(option, callback, arg_string) \
106 " " arg_string "\n"
107#include "qemu-img-cmds.h"
108#undef DEF
109#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300110 "\n"
111 "Command parameters:\n"
112 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000113 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
114 " manual page for a description of the object properties. The most common\n"
115 " object type is a 'secret', which is used to supply passwords and/or\n"
116 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300117 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400118 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800119 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
120 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100121 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
122 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300123 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200124 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
125 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
126 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300127 " 'output_filename' is the destination disk image filename\n"
128 " 'output_fmt' is the destination format\n"
129 " 'options' is a comma separated list of format specific options in a\n"
130 " name=value format. Use -o ? for an overview of the options supported by the\n"
131 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800132 " 'snapshot_param' is param used for internal snapshot, format\n"
133 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
134 " '[ID_OR_NAME]'\n"
135 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
136 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300137 " '-c' indicates that target image must be compressed (qcow format only)\n"
138 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
139 " match exactly. The image doesn't need a working backing file before\n"
140 " rebasing in this case (useful for renaming the backing file)\n"
141 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200142 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100143 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200144 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
145 " contain only zeros for qemu-img to create a sparse image during\n"
146 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
147 " unallocated or zero sectors, and the destination image will always be\n"
148 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200149 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100150 " '-n' skips the target volume creation (useful if the volume is created\n"
151 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300152 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200153 "Parameters to check subcommand:\n"
154 " '-r' tries to repair any inconsistencies that are found during the check.\n"
155 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
156 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200157 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200158 "\n"
malc3f020d72010-02-08 12:04:56 +0300159 "Parameters to snapshot subcommand:\n"
160 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
161 " '-a' applies a snapshot (revert disk to saved state)\n"
162 " '-c' creates a snapshot\n"
163 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100164 " '-l' lists all snapshots in the given image\n"
165 "\n"
166 "Parameters to compare subcommand:\n"
167 " '-f' first image format\n"
168 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200169 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
170 "\n"
171 "Parameters to dd subcommand:\n"
172 " 'bs=BYTES' read and write up to BYTES bytes at a time "
173 "(default: 512)\n"
174 " 'count=N' copy only N input blocks\n"
175 " 'if=FILE' read from FILE\n"
176 " 'of=FILE' write to FILE\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100177
178 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100179 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000180 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800181 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000182}
183
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000184static QemuOptsList qemu_object_opts = {
185 .name = "object",
186 .implied_opt_name = "qom-type",
187 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
188 .desc = {
189 { }
190 },
191};
192
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000193static QemuOptsList qemu_source_opts = {
194 .name = "source",
195 .implied_opt_name = "file",
196 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
197 .desc = {
198 { }
199 },
200};
201
Stefan Weil7c30f652013-06-16 17:01:05 +0200202static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100203{
204 int ret = 0;
205 if (!quiet) {
206 va_list args;
207 va_start(args, fmt);
208 ret = vprintf(fmt, args);
209 va_end(args);
210 }
211 return ret;
212}
213
bellardea2384d2004-08-01 21:59:26 +0000214
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100215static int print_block_option_help(const char *filename, const char *fmt)
216{
217 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800218 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500219 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100220
221 /* Find driver and parse its options */
222 drv = bdrv_find_format(fmt);
223 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100224 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100225 return 1;
226 }
227
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800228 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100229 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500230 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100231 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100232 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800233 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100234 return 1;
235 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800236 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100237 }
238
Chunyan Liu83d05212014-06-05 17:20:51 +0800239 qemu_opts_print_help(create_opts);
240 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100241 return 0;
242}
243
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000244
245static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000246 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000247{
248 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000249 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000250
251 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000252 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
253 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000254 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
255 if (qemu_read_password(password, sizeof(password)) < 0) {
256 error_report("No password given");
257 return -1;
258 }
259 if (bdrv_set_key(bs, password) < 0) {
260 error_report("invalid password");
261 return -1;
262 }
263 }
264 return 0;
265}
266
267
Max Reitzefaa7c42016-03-16 19:54:38 +0100268static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100269 QemuOpts *opts, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000270 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000271{
272 QDict *options;
273 Error *local_err = NULL;
274 BlockBackend *blk;
275 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100276 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000277 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100278 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000279 return NULL;
280 }
Kevin Wolfce099542016-03-15 13:01:04 +0100281 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000282
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000283 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000284 blk_unref(blk);
285 return NULL;
286 }
287 return blk;
288}
289
Max Reitzefaa7c42016-03-16 19:54:38 +0100290static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000291 const char *fmt, int flags,
Kevin Wolfce099542016-03-15 13:01:04 +0100292 bool writethrough, bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000293{
294 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200295 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500296 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100297
bellard75c23802004-08-27 21:28:58 +0000298 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500299 options = qdict_new();
300 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000301 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100302
Max Reitzefaa7c42016-03-16 19:54:38 +0100303 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500304 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100305 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000306 return NULL;
bellard75c23802004-08-27 21:28:58 +0000307 }
Kevin Wolfce099542016-03-15 13:01:04 +0100308 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100309
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000310 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000311 blk_unref(blk);
312 return NULL;
bellard75c23802004-08-27 21:28:58 +0000313 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200314 return blk;
bellard75c23802004-08-27 21:28:58 +0000315}
316
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000317
Max Reitzefaa7c42016-03-16 19:54:38 +0100318static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100320 const char *fmt, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000321 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000322{
323 BlockBackend *blk;
324 if (image_opts) {
325 QemuOpts *opts;
326 if (fmt) {
327 error_report("--image-opts and --format are mutually exclusive");
328 return NULL;
329 }
330 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
331 filename, true);
332 if (!opts) {
333 return NULL;
334 }
Kevin Wolfce099542016-03-15 13:01:04 +0100335 blk = img_open_opts(filename, opts, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000336 } else {
Kevin Wolfce099542016-03-15 13:01:04 +0100337 blk = img_open_file(filename, fmt, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000338 }
339 return blk;
340}
341
342
Chunyan Liu83d05212014-06-05 17:20:51 +0800343static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100344 const char *base_filename,
345 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200346{
Markus Armbruster6750e792015-02-12 17:43:08 +0100347 Error *err = NULL;
348
Kevin Wolfefa84d42009-05-18 16:42:12 +0200349 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100350 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100351 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100352 error_report("Backing file not supported for file format '%s'",
353 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100354 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900355 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200356 }
357 }
358 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100359 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100360 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100361 error_report("Backing file format not supported for file "
362 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100363 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900364 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200365 }
366 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900367 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200368}
369
bellardea2384d2004-08-01 21:59:26 +0000370static int img_create(int argc, char **argv)
371{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200372 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100373 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000374 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000375 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000376 const char *filename;
377 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200378 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200379 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100380 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000381
bellardea2384d2004-08-01 21:59:26 +0000382 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000383 static const struct option long_options[] = {
384 {"help", no_argument, 0, 'h'},
385 {"object", required_argument, 0, OPTION_OBJECT},
386 {0, 0, 0, 0}
387 };
388 c = getopt_long(argc, argv, "F:b:f:he6o:q",
389 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100390 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000391 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100392 }
bellardea2384d2004-08-01 21:59:26 +0000393 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100394 case '?':
bellardea2384d2004-08-01 21:59:26 +0000395 case 'h':
396 help();
397 break;
aliguori9230eaf2009-03-28 17:55:19 +0000398 case 'F':
399 base_fmt = optarg;
400 break;
bellardea2384d2004-08-01 21:59:26 +0000401 case 'b':
402 base_filename = optarg;
403 break;
404 case 'f':
405 fmt = optarg;
406 break;
407 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200408 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100409 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100410 goto fail;
thsd8871c52007-10-24 16:11:42 +0000411 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200412 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100413 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100414 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200415 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100416 if (!is_valid_option_list(optarg)) {
417 error_report("Invalid option list: %s", optarg);
418 goto fail;
419 }
420 if (!options) {
421 options = g_strdup(optarg);
422 } else {
423 char *old_options = options;
424 options = g_strdup_printf("%s,%s", options, optarg);
425 g_free(old_options);
426 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200427 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100428 case 'q':
429 quiet = true;
430 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000431 case OPTION_OBJECT: {
432 QemuOpts *opts;
433 opts = qemu_opts_parse_noisily(&qemu_object_opts,
434 optarg, true);
435 if (!opts) {
436 goto fail;
437 }
438 } break;
bellardea2384d2004-08-01 21:59:26 +0000439 }
440 }
aliguori9230eaf2009-03-28 17:55:19 +0000441
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900442 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100443 filename = (optind < argc) ? argv[optind] : NULL;
444 if (options && has_help_option(options)) {
445 g_free(options);
446 return print_block_option_help(filename, fmt);
447 }
448
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100449 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800450 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100451 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100452 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900453
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000454 if (qemu_opts_foreach(&qemu_object_opts,
455 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200456 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000457 goto fail;
458 }
459
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100460 /* Get image size, if specified */
461 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100462 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100463 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200464 sval = qemu_strtosz_suffix(argv[optind++], &end,
465 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100466 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800467 if (sval == -ERANGE) {
468 error_report("Image size must be less than 8 EiB!");
469 } else {
470 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200471 "G, T, P or E suffixes for ");
472 error_report("kilobytes, megabytes, gigabytes, terabytes, "
473 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800474 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100475 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100476 }
477 img_size = (uint64_t)sval;
478 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200479 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800480 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200481 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100482
Luiz Capitulino9b375252012-11-30 10:52:05 -0200483 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Kevin Wolf61de4c62016-03-18 17:46:45 +0100484 options, img_size, 0, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100485 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100486 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100487 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900488 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200489
Kevin Wolf77386bf2014-02-21 16:24:04 +0100490 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000491 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100492
493fail:
494 g_free(options);
495 return 1;
bellardea2384d2004-08-01 21:59:26 +0000496}
497
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100498static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500499{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500500 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500501 QObject *obj;
Eric Blake3b098d52016-06-09 10:48:43 -0600502 Visitor *v = qmp_output_visitor_new(&obj);
503
504 visit_type_ImageCheck(v, NULL, &check, &error_abort);
505 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500506 str = qobject_to_json_pretty(obj);
507 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100508 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500509 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600510 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500511 QDECREF(str);
512}
513
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100514static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500515{
516 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100517 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500518 } else {
519 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100520 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
521 "Data may be corrupted, or further writes to the image "
522 "may corrupt it.\n",
523 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500524 }
525
526 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100527 qprintf(quiet,
528 "\n%" PRId64 " leaked clusters were found on the image.\n"
529 "This means waste of disk space, but no harm to data.\n",
530 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500531 }
532
533 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100534 qprintf(quiet,
535 "\n%" PRId64
536 " internal errors have occurred during the check.\n",
537 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500538 }
539 }
540
541 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100542 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
543 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
544 check->allocated_clusters, check->total_clusters,
545 check->allocated_clusters * 100.0 / check->total_clusters,
546 check->fragmented_clusters * 100.0 / check->allocated_clusters,
547 check->compressed_clusters * 100.0 /
548 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500549 }
550
551 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100552 qprintf(quiet,
553 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500554 }
555}
556
557static int collect_image_check(BlockDriverState *bs,
558 ImageCheck *check,
559 const char *filename,
560 const char *fmt,
561 int fix)
562{
563 int ret;
564 BdrvCheckResult result;
565
566 ret = bdrv_check(bs, &result, fix);
567 if (ret < 0) {
568 return ret;
569 }
570
571 check->filename = g_strdup(filename);
572 check->format = g_strdup(bdrv_get_format_name(bs));
573 check->check_errors = result.check_errors;
574 check->corruptions = result.corruptions;
575 check->has_corruptions = result.corruptions != 0;
576 check->leaks = result.leaks;
577 check->has_leaks = result.leaks != 0;
578 check->corruptions_fixed = result.corruptions_fixed;
579 check->has_corruptions_fixed = result.corruptions != 0;
580 check->leaks_fixed = result.leaks_fixed;
581 check->has_leaks_fixed = result.leaks != 0;
582 check->image_end_offset = result.image_end_offset;
583 check->has_image_end_offset = result.image_end_offset != 0;
584 check->total_clusters = result.bfi.total_clusters;
585 check->has_total_clusters = result.bfi.total_clusters != 0;
586 check->allocated_clusters = result.bfi.allocated_clusters;
587 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
588 check->fragmented_clusters = result.bfi.fragmented_clusters;
589 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100590 check->compressed_clusters = result.bfi.compressed_clusters;
591 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592
593 return 0;
594}
595
Kevin Wolfe076f332010-06-29 11:43:13 +0200596/*
597 * Checks an image for consistency. Exit codes:
598 *
Max Reitzd6635c42014-06-02 22:15:21 +0200599 * 0 - Check completed, image is good
600 * 1 - Check not completed because of internal errors
601 * 2 - Check completed, image is corrupted
602 * 3 - Check completed, image has leaked clusters, but is good otherwise
603 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200604 */
aliguori15859692009-04-21 23:11:53 +0000605static int img_check(int argc, char **argv)
606{
607 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500608 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200609 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200610 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000611 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200612 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100613 int flags = BDRV_O_CHECK;
614 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200615 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100616 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000617 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000618
619 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500620 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200621 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100622
aliguori15859692009-04-21 23:11:53 +0000623 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500624 int option_index = 0;
625 static const struct option long_options[] = {
626 {"help", no_argument, 0, 'h'},
627 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530628 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500629 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000630 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000631 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500632 {0, 0, 0, 0}
633 };
Max Reitz40055952014-07-22 22:58:42 +0200634 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500635 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100636 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000637 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100638 }
aliguori15859692009-04-21 23:11:53 +0000639 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100640 case '?':
aliguori15859692009-04-21 23:11:53 +0000641 case 'h':
642 help();
643 break;
644 case 'f':
645 fmt = optarg;
646 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200647 case 'r':
648 flags |= BDRV_O_RDWR;
649
650 if (!strcmp(optarg, "leaks")) {
651 fix = BDRV_FIX_LEAKS;
652 } else if (!strcmp(optarg, "all")) {
653 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
654 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800655 error_exit("Unknown option value for -r "
656 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200657 }
658 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500659 case OPTION_OUTPUT:
660 output = optarg;
661 break;
Max Reitz40055952014-07-22 22:58:42 +0200662 case 'T':
663 cache = optarg;
664 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100665 case 'q':
666 quiet = true;
667 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000668 case OPTION_OBJECT: {
669 QemuOpts *opts;
670 opts = qemu_opts_parse_noisily(&qemu_object_opts,
671 optarg, true);
672 if (!opts) {
673 return 1;
674 }
675 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000676 case OPTION_IMAGE_OPTS:
677 image_opts = true;
678 break;
aliguori15859692009-04-21 23:11:53 +0000679 }
680 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200681 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800682 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100683 }
aliguori15859692009-04-21 23:11:53 +0000684 filename = argv[optind++];
685
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500686 if (output && !strcmp(output, "json")) {
687 output_format = OFORMAT_JSON;
688 } else if (output && !strcmp(output, "human")) {
689 output_format = OFORMAT_HUMAN;
690 } else if (output) {
691 error_report("--output must be used with human or json as argument.");
692 return 1;
693 }
694
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000695 if (qemu_opts_foreach(&qemu_object_opts,
696 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200697 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000698 return 1;
699 }
700
Kevin Wolfce099542016-03-15 13:01:04 +0100701 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200702 if (ret < 0) {
703 error_report("Invalid source cache option: %s", cache);
704 return 1;
705 }
706
Kevin Wolfce099542016-03-15 13:01:04 +0100707 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200708 if (!blk) {
709 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900710 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200711 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500712
713 check = g_new0(ImageCheck, 1);
714 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200715
716 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200717 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200718 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500719 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200720 }
721
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500722 if (check->corruptions_fixed || check->leaks_fixed) {
723 int corruptions_fixed, leaks_fixed;
724
725 leaks_fixed = check->leaks_fixed;
726 corruptions_fixed = check->corruptions_fixed;
727
728 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100729 qprintf(quiet,
730 "The following inconsistencies were found and repaired:\n\n"
731 " %" PRId64 " leaked clusters\n"
732 " %" PRId64 " corruptions\n\n"
733 "Double checking the fixed image now...\n",
734 check->leaks_fixed,
735 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500736 }
737
738 ret = collect_image_check(bs, check, filename, fmt, 0);
739
740 check->leaks_fixed = leaks_fixed;
741 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200742 }
743
Max Reitz832390a2014-10-23 15:29:12 +0200744 if (!ret) {
745 switch (output_format) {
746 case OFORMAT_HUMAN:
747 dump_human_image_check(check, quiet);
748 break;
749 case OFORMAT_JSON:
750 dump_json_image_check(check, quiet);
751 break;
752 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500753 }
754
755 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200756 if (ret) {
757 error_report("Check failed: %s", strerror(-ret));
758 } else {
759 error_report("Check failed");
760 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500761 ret = 1;
762 goto fail;
763 }
764
765 if (check->corruptions) {
766 ret = 2;
767 } else if (check->leaks) {
768 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200769 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500770 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000771 }
772
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500773fail:
774 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200775 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500776 return ret;
aliguori15859692009-04-21 23:11:53 +0000777}
778
Max Reitzd4a32382014-10-24 15:57:37 +0200779typedef struct CommonBlockJobCBInfo {
780 BlockDriverState *bs;
781 Error **errp;
782} CommonBlockJobCBInfo;
783
784static void common_block_job_cb(void *opaque, int ret)
785{
786 CommonBlockJobCBInfo *cbi = opaque;
787
788 if (ret < 0) {
789 error_setg_errno(cbi->errp, -ret, "Block job failed");
790 }
Max Reitzd4a32382014-10-24 15:57:37 +0200791}
792
793static void run_block_job(BlockJob *job, Error **errp)
794{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200795 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200796
797 do {
798 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500799 qemu_progress_print(job->len ?
800 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200801 } while (!job->ready);
802
803 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200804
805 /* A block job may finish instantaneously without publishing any progress,
806 * so just signal completion here */
807 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200808}
809
bellardea2384d2004-08-01 21:59:26 +0000810static int img_commit(int argc, char **argv)
811{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400812 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200813 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200814 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200815 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200816 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100817 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200818 Error *local_err = NULL;
819 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000820 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000821
822 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400823 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200824 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000825 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000826 static const struct option long_options[] = {
827 {"help", no_argument, 0, 'h'},
828 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000829 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000830 {0, 0, 0, 0}
831 };
832 c = getopt_long(argc, argv, "f:ht:b:dpq",
833 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100834 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000835 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100836 }
bellardea2384d2004-08-01 21:59:26 +0000837 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100838 case '?':
bellardea2384d2004-08-01 21:59:26 +0000839 case 'h':
840 help();
841 break;
842 case 'f':
843 fmt = optarg;
844 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400845 case 't':
846 cache = optarg;
847 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200848 case 'b':
849 base = optarg;
850 /* -b implies -d */
851 drop = true;
852 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200853 case 'd':
854 drop = true;
855 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200856 case 'p':
857 progress = true;
858 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100859 case 'q':
860 quiet = true;
861 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000862 case OPTION_OBJECT: {
863 QemuOpts *opts;
864 opts = qemu_opts_parse_noisily(&qemu_object_opts,
865 optarg, true);
866 if (!opts) {
867 return 1;
868 }
869 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000870 case OPTION_IMAGE_OPTS:
871 image_opts = true;
872 break;
bellardea2384d2004-08-01 21:59:26 +0000873 }
874 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200875
876 /* Progress is not shown in Quiet mode */
877 if (quiet) {
878 progress = false;
879 }
880
Kevin Wolffc11eb22013-08-05 10:53:04 +0200881 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800882 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100883 }
bellardea2384d2004-08-01 21:59:26 +0000884 filename = argv[optind++];
885
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000886 if (qemu_opts_foreach(&qemu_object_opts,
887 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200888 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000889 return 1;
890 }
891
Max Reitz9a86fe42014-10-24 15:57:38 +0200892 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100893 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400894 if (ret < 0) {
895 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100896 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400897 }
898
Kevin Wolfce099542016-03-15 13:01:04 +0100899 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200900 if (!blk) {
901 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900902 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200903 bs = blk_bs(blk);
904
Max Reitz687fa1d2014-10-24 15:57:39 +0200905 qemu_progress_init(progress, 1.f);
906 qemu_progress_print(0.f, 100);
907
Max Reitz1b22bff2014-10-24 15:57:40 +0200908 if (base) {
909 base_bs = bdrv_find_backing_image(bs, base);
910 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100911 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200912 goto done;
913 }
914 } else {
915 /* This is different from QMP, which by default uses the deepest file in
916 * the backing chain (i.e., the very base); however, the traditional
917 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200918 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200919 if (!base_bs) {
920 error_setg(&local_err, "Image does not have a backing file");
921 goto done;
922 }
bellardea2384d2004-08-01 21:59:26 +0000923 }
924
Max Reitzd4a32382014-10-24 15:57:37 +0200925 cbi = (CommonBlockJobCBInfo){
926 .errp = &local_err,
927 .bs = bs,
928 };
929
Alberto Garciaa5d5a3b2016-07-05 17:29:01 +0300930 commit_active_start("commit", bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
Wen Congyangb49f7ea2016-07-27 15:01:47 +0800931 common_block_job_cb, &cbi, &local_err, false);
Max Reitzd4a32382014-10-24 15:57:37 +0200932 if (local_err) {
933 goto done;
934 }
935
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200936 /* When the block job completes, the BlockBackend reference will point to
937 * the old backing file. In order to avoid that the top image is already
938 * deleted, so we can still empty it afterwards, increment the reference
939 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200940 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200941 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200942 }
943
Max Reitzd4a32382014-10-24 15:57:37 +0200944 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200945 if (local_err) {
946 goto unref_backing;
947 }
948
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200949 if (!drop && bs->drv->bdrv_make_empty) {
950 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200951 if (ret) {
952 error_setg_errno(&local_err, -ret, "Could not empty %s",
953 filename);
954 goto unref_backing;
955 }
956 }
957
958unref_backing:
959 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200960 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200961 }
Max Reitzd4a32382014-10-24 15:57:37 +0200962
963done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200964 qemu_progress_end();
965
Markus Armbruster26f54e92014-10-07 13:59:04 +0200966 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200967
968 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100969 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900970 return 1;
971 }
Max Reitzd4a32382014-10-24 15:57:37 +0200972
973 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000974 return 0;
975}
976
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400977/*
thsf58c7b32008-06-05 21:53:49 +0000978 * Returns true iff the first sector pointed to by 'buf' contains at least
979 * a non-NUL byte.
980 *
981 * 'pnum' is set to the number of sectors (including and immediately following
982 * the first one) that are known to be in the same allocated/unallocated state.
983 */
bellardea2384d2004-08-01 21:59:26 +0000984static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
985{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000986 bool is_zero;
987 int i;
bellardea2384d2004-08-01 21:59:26 +0000988
989 if (n <= 0) {
990 *pnum = 0;
991 return 0;
992 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000993 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000994 for(i = 1; i < n; i++) {
995 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000996 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000997 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000998 }
bellardea2384d2004-08-01 21:59:26 +0000999 }
1000 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001001 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001002}
1003
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001004/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001005 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1006 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1007 * breaking up write requests for only small sparse areas.
1008 */
1009static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1010 int min)
1011{
1012 int ret;
1013 int num_checked, num_used;
1014
1015 if (n < min) {
1016 min = n;
1017 }
1018
1019 ret = is_allocated_sectors(buf, n, pnum);
1020 if (!ret) {
1021 return ret;
1022 }
1023
1024 num_used = *pnum;
1025 buf += BDRV_SECTOR_SIZE * *pnum;
1026 n -= *pnum;
1027 num_checked = num_used;
1028
1029 while (n > 0) {
1030 ret = is_allocated_sectors(buf, n, pnum);
1031
1032 buf += BDRV_SECTOR_SIZE * *pnum;
1033 n -= *pnum;
1034 num_checked += *pnum;
1035 if (ret) {
1036 num_used = num_checked;
1037 } else if (*pnum >= min) {
1038 break;
1039 }
1040 }
1041
1042 *pnum = num_used;
1043 return 1;
1044}
1045
1046/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001047 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1048 * buffers matches, non-zero otherwise.
1049 *
1050 * pnum is set to the number of sectors (including and immediately following
1051 * the first one) that are known to have the same comparison result
1052 */
1053static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1054 int *pnum)
1055{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001056 bool res;
1057 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001058
1059 if (n <= 0) {
1060 *pnum = 0;
1061 return 0;
1062 }
1063
1064 res = !!memcmp(buf1, buf2, 512);
1065 for(i = 1; i < n; i++) {
1066 buf1 += 512;
1067 buf2 += 512;
1068
1069 if (!!memcmp(buf1, buf2, 512) != res) {
1070 break;
1071 }
1072 }
1073
1074 *pnum = i;
1075 return res;
1076}
1077
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001078#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001079
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001080static int64_t sectors_to_bytes(int64_t sectors)
1081{
1082 return sectors << BDRV_SECTOR_BITS;
1083}
1084
1085static int64_t sectors_to_process(int64_t total, int64_t from)
1086{
1087 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1088}
1089
1090/*
1091 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1092 *
1093 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1094 * data and negative value on error.
1095 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001096 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001097 * @param sect_num: Number of first sector to check
1098 * @param sect_count: Number of sectors to check
1099 * @param filename: Name of disk file we are checking (logging purpose)
1100 * @param buffer: Allocated buffer for storing read data
1101 * @param quiet: Flag for quiet mode
1102 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001103static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001104 int sect_count, const char *filename,
1105 uint8_t *buffer, bool quiet)
1106{
1107 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001108 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1109 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001110 if (ret < 0) {
1111 error_report("Error while reading offset %" PRId64 " of %s: %s",
1112 sectors_to_bytes(sect_num), filename, strerror(-ret));
1113 return ret;
1114 }
1115 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1116 if (ret || pnum != sect_count) {
1117 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1118 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1119 return 1;
1120 }
1121
1122 return 0;
1123}
1124
1125/*
1126 * Compares two images. Exit codes:
1127 *
1128 * 0 - Images are identical
1129 * 1 - Images differ
1130 * >1 - Error occurred
1131 */
1132static int img_compare(int argc, char **argv)
1133{
Max Reitz40055952014-07-22 22:58:42 +02001134 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001135 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001136 BlockDriverState *bs1, *bs2;
1137 int64_t total_sectors1, total_sectors2;
1138 uint8_t *buf1 = NULL, *buf2 = NULL;
1139 int pnum1, pnum2;
1140 int allocated1, allocated2;
1141 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1142 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001143 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001144 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001145 int64_t total_sectors;
1146 int64_t sector_num = 0;
1147 int64_t nb_sectors;
1148 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001149 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001150 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001151
Max Reitz40055952014-07-22 22:58:42 +02001152 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001153 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001154 static const struct option long_options[] = {
1155 {"help", no_argument, 0, 'h'},
1156 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001157 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001158 {0, 0, 0, 0}
1159 };
1160 c = getopt_long(argc, argv, "hf:F:T:pqs",
1161 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001162 if (c == -1) {
1163 break;
1164 }
1165 switch (c) {
1166 case '?':
1167 case 'h':
1168 help();
1169 break;
1170 case 'f':
1171 fmt1 = optarg;
1172 break;
1173 case 'F':
1174 fmt2 = optarg;
1175 break;
Max Reitz40055952014-07-22 22:58:42 +02001176 case 'T':
1177 cache = optarg;
1178 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001179 case 'p':
1180 progress = true;
1181 break;
1182 case 'q':
1183 quiet = true;
1184 break;
1185 case 's':
1186 strict = true;
1187 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001188 case OPTION_OBJECT: {
1189 QemuOpts *opts;
1190 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1191 optarg, true);
1192 if (!opts) {
1193 ret = 2;
1194 goto out4;
1195 }
1196 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001197 case OPTION_IMAGE_OPTS:
1198 image_opts = true;
1199 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001200 }
1201 }
1202
1203 /* Progress is not shown in Quiet mode */
1204 if (quiet) {
1205 progress = false;
1206 }
1207
1208
Kevin Wolffc11eb22013-08-05 10:53:04 +02001209 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001210 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001211 }
1212 filename1 = argv[optind++];
1213 filename2 = argv[optind++];
1214
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001215 if (qemu_opts_foreach(&qemu_object_opts,
1216 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001217 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001218 ret = 2;
1219 goto out4;
1220 }
1221
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001222 /* Initialize before goto out */
1223 qemu_progress_init(progress, 2.0);
1224
Kevin Wolfce099542016-03-15 13:01:04 +01001225 flags = 0;
1226 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001227 if (ret < 0) {
1228 error_report("Invalid source cache option: %s", cache);
1229 ret = 2;
1230 goto out3;
1231 }
1232
Kevin Wolfce099542016-03-15 13:01:04 +01001233 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001234 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001235 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001236 goto out3;
1237 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001238
Kevin Wolfce099542016-03-15 13:01:04 +01001239 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001240 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001241 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001242 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001243 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001244 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001245 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001246
Max Reitzf1d3cd72015-02-05 13:58:18 -05001247 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1248 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1249 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001250 if (total_sectors1 < 0) {
1251 error_report("Can't get size of %s: %s",
1252 filename1, strerror(-total_sectors1));
1253 ret = 4;
1254 goto out;
1255 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001256 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001257 if (total_sectors2 < 0) {
1258 error_report("Can't get size of %s: %s",
1259 filename2, strerror(-total_sectors2));
1260 ret = 4;
1261 goto out;
1262 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263 total_sectors = MIN(total_sectors1, total_sectors2);
1264 progress_base = MAX(total_sectors1, total_sectors2);
1265
1266 qemu_progress_print(0, 100);
1267
1268 if (strict && total_sectors1 != total_sectors2) {
1269 ret = 1;
1270 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1271 goto out;
1272 }
1273
1274 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001275 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001276 BlockDriverState *file;
1277
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001278 nb_sectors = sectors_to_process(total_sectors, sector_num);
1279 if (nb_sectors <= 0) {
1280 break;
1281 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001282 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1283 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001284 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001285 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001286 ret = 3;
1287 error_report("Sector allocation test failed for %s", filename1);
1288 goto out;
1289 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001290 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001291
Fam Zheng25ad8e62016-01-13 16:37:41 +08001292 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1293 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001294 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001295 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001296 ret = 3;
1297 error_report("Sector allocation test failed for %s", filename2);
1298 goto out;
1299 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001300 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1301 if (pnum1) {
1302 nb_sectors = MIN(nb_sectors, pnum1);
1303 }
1304 if (pnum2) {
1305 nb_sectors = MIN(nb_sectors, pnum2);
1306 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001307
Fam Zheng25ad8e62016-01-13 16:37:41 +08001308 if (strict) {
1309 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1310 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1311 ret = 1;
1312 qprintf(quiet, "Strict mode: Offset %" PRId64
1313 " block status mismatch!\n",
1314 sectors_to_bytes(sector_num));
1315 goto out;
1316 }
1317 }
1318 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1319 nb_sectors = MIN(pnum1, pnum2);
1320 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001321 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001322 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1323 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001324 if (ret < 0) {
1325 error_report("Error while reading offset %" PRId64 " of %s:"
1326 " %s", sectors_to_bytes(sector_num), filename1,
1327 strerror(-ret));
1328 ret = 4;
1329 goto out;
1330 }
Eric Blake91669202016-05-06 10:26:43 -06001331 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1332 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001333 if (ret < 0) {
1334 error_report("Error while reading offset %" PRId64
1335 " of %s: %s", sectors_to_bytes(sector_num),
1336 filename2, strerror(-ret));
1337 ret = 4;
1338 goto out;
1339 }
1340 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1341 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001342 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1343 sectors_to_bytes(
1344 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001345 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001346 goto out;
1347 }
1348 }
1349 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001350
1351 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001352 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001353 filename1, buf1, quiet);
1354 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001355 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001356 filename2, buf1, quiet);
1357 }
1358 if (ret) {
1359 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001360 error_report("Error while reading offset %" PRId64 ": %s",
1361 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001362 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001363 }
1364 goto out;
1365 }
1366 }
1367 sector_num += nb_sectors;
1368 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1369 }
1370
1371 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001372 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001373 int64_t total_sectors_over;
1374 const char *filename_over;
1375
1376 qprintf(quiet, "Warning: Image size mismatch!\n");
1377 if (total_sectors1 > total_sectors2) {
1378 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001379 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001380 filename_over = filename1;
1381 } else {
1382 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001383 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384 filename_over = filename2;
1385 }
1386
1387 for (;;) {
1388 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1389 if (nb_sectors <= 0) {
1390 break;
1391 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001392 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001393 nb_sectors, &pnum);
1394 if (ret < 0) {
1395 ret = 3;
1396 error_report("Sector allocation test failed for %s",
1397 filename_over);
1398 goto out;
1399
1400 }
1401 nb_sectors = pnum;
1402 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001403 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001404 filename_over, buf1, quiet);
1405 if (ret) {
1406 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001407 error_report("Error while reading offset %" PRId64
1408 " of %s: %s", sectors_to_bytes(sector_num),
1409 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001410 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001411 }
1412 goto out;
1413 }
1414 }
1415 sector_num += nb_sectors;
1416 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1417 }
1418 }
1419
1420 qprintf(quiet, "Images are identical.\n");
1421 ret = 0;
1422
1423out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001424 qemu_vfree(buf1);
1425 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001426 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001427out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001428 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001429out3:
1430 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001431out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001432 return ret;
1433}
1434
Kevin Wolf690c7302015-03-19 13:33:32 +01001435enum ImgConvertBlockStatus {
1436 BLK_DATA,
1437 BLK_ZERO,
1438 BLK_BACKING_FILE,
1439};
1440
1441typedef struct ImgConvertState {
1442 BlockBackend **src;
1443 int64_t *src_sectors;
1444 int src_cur, src_num;
1445 int64_t src_cur_offset;
1446 int64_t total_sectors;
1447 int64_t allocated_sectors;
1448 enum ImgConvertBlockStatus status;
1449 int64_t sector_next_status;
1450 BlockBackend *target;
1451 bool has_zero_init;
1452 bool compressed;
1453 bool target_has_backing;
1454 int min_sparse;
1455 size_t cluster_sectors;
1456 size_t buf_sectors;
1457} ImgConvertState;
1458
1459static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1460{
1461 assert(sector_num >= s->src_cur_offset);
1462 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1463 s->src_cur_offset += s->src_sectors[s->src_cur];
1464 s->src_cur++;
1465 assert(s->src_cur < s->src_num);
1466 }
1467}
1468
1469static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1470{
1471 int64_t ret;
1472 int n;
1473
1474 convert_select_part(s, sector_num);
1475
1476 assert(s->total_sectors > sector_num);
1477 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1478
1479 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001480 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001481 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1482 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001483 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001484 if (ret < 0) {
1485 return ret;
1486 }
1487
1488 if (ret & BDRV_BLOCK_ZERO) {
1489 s->status = BLK_ZERO;
1490 } else if (ret & BDRV_BLOCK_DATA) {
1491 s->status = BLK_DATA;
1492 } else if (!s->target_has_backing) {
1493 /* Without a target backing file we must copy over the contents of
1494 * the backing file as well. */
Ren Kimura263a6f42016-04-28 01:04:58 +09001495 /* Check block status of the backing file chain to avoid
Kevin Wolf690c7302015-03-19 13:33:32 +01001496 * needlessly reading zeroes and limiting the iteration to the
1497 * buffer size */
Ren Kimura263a6f42016-04-28 01:04:58 +09001498 ret = bdrv_get_block_status_above(blk_bs(s->src[s->src_cur]), NULL,
1499 sector_num - s->src_cur_offset,
1500 n, &n, &file);
1501 if (ret < 0) {
1502 return ret;
1503 }
1504
1505 if (ret & BDRV_BLOCK_ZERO) {
1506 s->status = BLK_ZERO;
1507 } else {
1508 s->status = BLK_DATA;
1509 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001510 } else {
1511 s->status = BLK_BACKING_FILE;
1512 }
1513
1514 s->sector_next_status = sector_num + n;
1515 }
1516
1517 n = MIN(n, s->sector_next_status - sector_num);
1518 if (s->status == BLK_DATA) {
1519 n = MIN(n, s->buf_sectors);
1520 }
1521
1522 /* We need to write complete clusters for compressed images, so if an
1523 * unallocated area is shorter than that, we must consider the whole
1524 * cluster allocated. */
1525 if (s->compressed) {
1526 if (n < s->cluster_sectors) {
1527 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1528 s->status = BLK_DATA;
1529 } else {
1530 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1531 }
1532 }
1533
1534 return n;
1535}
1536
1537static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1538 uint8_t *buf)
1539{
1540 int n;
1541 int ret;
1542
Kevin Wolf690c7302015-03-19 13:33:32 +01001543 assert(nb_sectors <= s->buf_sectors);
1544 while (nb_sectors > 0) {
1545 BlockBackend *blk;
1546 int64_t bs_sectors;
1547
1548 /* In the case of compression with multiple source files, we can get a
1549 * nb_sectors that spreads into the next part. So we must be able to
1550 * read across multiple BDSes for one convert_read() call. */
1551 convert_select_part(s, sector_num);
1552 blk = s->src[s->src_cur];
1553 bs_sectors = s->src_sectors[s->src_cur];
1554
1555 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
Eric Blake91669202016-05-06 10:26:43 -06001556 ret = blk_pread(blk,
1557 (sector_num - s->src_cur_offset) << BDRV_SECTOR_BITS,
1558 buf, n << BDRV_SECTOR_BITS);
Kevin Wolf690c7302015-03-19 13:33:32 +01001559 if (ret < 0) {
1560 return ret;
1561 }
1562
1563 sector_num += n;
1564 nb_sectors -= n;
1565 buf += n * BDRV_SECTOR_SIZE;
1566 }
1567
1568 return 0;
1569}
1570
1571static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1572 const uint8_t *buf)
1573{
1574 int ret;
1575
1576 while (nb_sectors > 0) {
1577 int n = nb_sectors;
1578
1579 switch (s->status) {
1580 case BLK_BACKING_FILE:
1581 /* If we have a backing file, leave clusters unallocated that are
1582 * unallocated in the source image, so that the backing file is
1583 * visible at the respective offset. */
1584 assert(s->target_has_backing);
1585 break;
1586
1587 case BLK_DATA:
1588 /* We must always write compressed clusters as a whole, so don't
1589 * try to find zeroed parts in the buffer. We can only save the
1590 * write if the buffer is completely zeroed and we're allowed to
1591 * keep the target sparse. */
1592 if (s->compressed) {
1593 if (s->has_zero_init && s->min_sparse &&
1594 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1595 {
1596 assert(!s->target_has_backing);
1597 break;
1598 }
1599
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001600 ret = blk_pwrite_compressed(s->target,
1601 sector_num << BDRV_SECTOR_BITS,
1602 buf, n << BDRV_SECTOR_BITS);
Kevin Wolf690c7302015-03-19 13:33:32 +01001603 if (ret < 0) {
1604 return ret;
1605 }
1606 break;
1607 }
1608
1609 /* If there is real non-zero data or we're told to keep the target
1610 * fully allocated (-S 0), we must write it. Otherwise we can treat
1611 * it as zero sectors. */
1612 if (!s->min_sparse ||
1613 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1614 {
Eric Blake91669202016-05-06 10:26:43 -06001615 ret = blk_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1616 buf, n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001617 if (ret < 0) {
1618 return ret;
1619 }
1620 break;
1621 }
1622 /* fall-through */
1623
1624 case BLK_ZERO:
1625 if (s->has_zero_init) {
1626 break;
1627 }
Eric Blaked004bd52016-05-24 16:25:20 -06001628 ret = blk_pwrite_zeroes(s->target, sector_num << BDRV_SECTOR_BITS,
1629 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001630 if (ret < 0) {
1631 return ret;
1632 }
1633 break;
1634 }
1635
1636 sector_num += n;
1637 nb_sectors -= n;
1638 buf += n * BDRV_SECTOR_SIZE;
1639 }
1640
1641 return 0;
1642}
1643
1644static int convert_do_copy(ImgConvertState *s)
1645{
1646 uint8_t *buf = NULL;
1647 int64_t sector_num, allocated_done;
1648 int ret;
1649 int n;
1650
1651 /* Check whether we have zero initialisation or can get it efficiently */
1652 s->has_zero_init = s->min_sparse && !s->target_has_backing
1653 ? bdrv_has_zero_init(blk_bs(s->target))
1654 : false;
1655
1656 if (!s->has_zero_init && !s->target_has_backing &&
1657 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1658 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001659 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001660 if (ret == 0) {
1661 s->has_zero_init = true;
1662 }
1663 }
1664
1665 /* Allocate buffer for copied data. For compressed images, only one cluster
1666 * can be copied at a time. */
1667 if (s->compressed) {
1668 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1669 error_report("invalid cluster size");
1670 ret = -EINVAL;
1671 goto fail;
1672 }
1673 s->buf_sectors = s->cluster_sectors;
1674 }
1675 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1676
1677 /* Calculate allocated sectors for progress */
1678 s->allocated_sectors = 0;
1679 sector_num = 0;
1680 while (sector_num < s->total_sectors) {
1681 n = convert_iteration_sectors(s, sector_num);
1682 if (n < 0) {
1683 ret = n;
1684 goto fail;
1685 }
Max Reitzaad15de2016-03-24 23:33:57 +01001686 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1687 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001688 s->allocated_sectors += n;
1689 }
1690 sector_num += n;
1691 }
1692
1693 /* Do the copy */
1694 s->src_cur = 0;
1695 s->src_cur_offset = 0;
1696 s->sector_next_status = 0;
1697
1698 sector_num = 0;
1699 allocated_done = 0;
1700
1701 while (sector_num < s->total_sectors) {
1702 n = convert_iteration_sectors(s, sector_num);
1703 if (n < 0) {
1704 ret = n;
1705 goto fail;
1706 }
Max Reitzaad15de2016-03-24 23:33:57 +01001707 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1708 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001709 allocated_done += n;
1710 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1711 0);
1712 }
1713
Max Reitzaad15de2016-03-24 23:33:57 +01001714 if (s->status == BLK_DATA) {
1715 ret = convert_read(s, sector_num, n, buf);
1716 if (ret < 0) {
1717 error_report("error while reading sector %" PRId64
1718 ": %s", sector_num, strerror(-ret));
1719 goto fail;
1720 }
1721 } else if (!s->min_sparse && s->status == BLK_ZERO) {
1722 n = MIN(n, s->buf_sectors);
1723 memset(buf, 0, n * BDRV_SECTOR_SIZE);
1724 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001725 }
1726
1727 ret = convert_write(s, sector_num, n, buf);
1728 if (ret < 0) {
1729 error_report("error while writing sector %" PRId64
1730 ": %s", sector_num, strerror(-ret));
1731 goto fail;
1732 }
1733
1734 sector_num += n;
1735 }
1736
1737 if (s->compressed) {
1738 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001739 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001740 if (ret < 0) {
1741 goto fail;
1742 }
1743 }
1744
1745 ret = 0;
1746fail:
1747 qemu_vfree(buf);
1748 return ret;
1749}
1750
bellardea2384d2004-08-01 21:59:26 +00001751static int img_convert(int argc, char **argv)
1752{
Kevin Wolf690c7302015-03-19 13:33:32 +01001753 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001754 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001755 int progress = 0, flags, src_flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001756 bool writethrough, src_writethrough;
Max Reitz40055952014-07-22 22:58:42 +02001757 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001758 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001759 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001760 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001761 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001762 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001763 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001764 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001765 QemuOpts *opts = NULL;
1766 QemuOptsList *create_opts = NULL;
1767 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001768 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001769 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001770 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001771 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001772 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001773 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001774 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001775 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001776
1777 fmt = NULL;
1778 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001779 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001780 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001781 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001782 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001783 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001784 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001785 static const struct option long_options[] = {
1786 {"help", no_argument, 0, 'h'},
1787 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001788 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001789 {0, 0, 0, 0}
1790 };
1791 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1792 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001793 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001794 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001795 }
bellardea2384d2004-08-01 21:59:26 +00001796 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001797 case '?':
bellardea2384d2004-08-01 21:59:26 +00001798 case 'h':
1799 help();
1800 break;
1801 case 'f':
1802 fmt = optarg;
1803 break;
1804 case 'O':
1805 out_fmt = optarg;
1806 break;
thsf58c7b32008-06-05 21:53:49 +00001807 case 'B':
1808 out_baseimg = optarg;
1809 break;
bellardea2384d2004-08-01 21:59:26 +00001810 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001811 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001812 break;
1813 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001814 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001815 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001816 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001817 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001818 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001819 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001820 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001821 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001822 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001823 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001824 if (!is_valid_option_list(optarg)) {
1825 error_report("Invalid option list: %s", optarg);
1826 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001827 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001828 }
1829 if (!options) {
1830 options = g_strdup(optarg);
1831 } else {
1832 char *old_options = options;
1833 options = g_strdup_printf("%s,%s", options, optarg);
1834 g_free(old_options);
1835 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001836 break;
edison51ef6722010-09-21 19:58:41 -07001837 case 's':
1838 snapshot_name = optarg;
1839 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001840 case 'l':
1841 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001842 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1843 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001844 if (!sn_opts) {
1845 error_report("Failed in parsing snapshot param '%s'",
1846 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001847 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001848 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001849 }
1850 } else {
1851 snapshot_name = optarg;
1852 }
1853 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001854 case 'S':
1855 {
1856 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001857 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001858 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001859 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001860 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001861 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001862 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001863 }
1864
1865 min_sparse = sval / BDRV_SECTOR_SIZE;
1866 break;
1867 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001868 case 'p':
1869 progress = 1;
1870 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001871 case 't':
1872 cache = optarg;
1873 break;
Max Reitz40055952014-07-22 22:58:42 +02001874 case 'T':
1875 src_cache = optarg;
1876 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001877 case 'q':
1878 quiet = true;
1879 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001880 case 'n':
1881 skip_create = 1;
1882 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001883 case OPTION_OBJECT:
1884 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1885 optarg, true);
1886 if (!opts) {
1887 goto fail_getopt;
1888 }
1889 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001890 case OPTION_IMAGE_OPTS:
1891 image_opts = true;
1892 break;
bellardea2384d2004-08-01 21:59:26 +00001893 }
1894 }
ths3b46e622007-09-17 08:09:54 +00001895
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001896 if (qemu_opts_foreach(&qemu_object_opts,
1897 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001898 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001899 goto fail_getopt;
1900 }
1901
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001902 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001903 if (quiet) {
1904 progress = 0;
1905 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001906 qemu_progress_init(progress, 1.0);
1907
balrog926c2d22007-10-31 01:11:44 +00001908 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001909 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001910
Kevin Wolf2dc83282014-02-21 16:24:05 +01001911 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001912 ret = print_block_option_help(out_filename, out_fmt);
1913 goto out;
1914 }
1915
bellardea2384d2004-08-01 21:59:26 +00001916 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001917 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001918 }
bellardea2384d2004-08-01 21:59:26 +00001919
thsf58c7b32008-06-05 21:53:49 +00001920
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001921 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001922 error_report("-B makes no sense when concatenating multiple input "
1923 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001924 ret = -1;
1925 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001926 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001927
Kevin Wolfce099542016-03-15 13:01:04 +01001928 src_flags = 0;
1929 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001930 if (ret < 0) {
1931 error_report("Invalid source cache option: %s", src_cache);
1932 goto out;
1933 }
1934
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001935 qemu_progress_print(0, 100);
1936
Markus Armbruster26f54e92014-10-07 13:59:04 +02001937 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001938 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001939 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001940
1941 total_sectors = 0;
1942 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001943 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Kevin Wolfce099542016-03-15 13:01:04 +01001944 fmt, src_flags, src_writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001945 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001946 ret = -1;
1947 goto out;
1948 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001949 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001950 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001951 if (bs_sectors[bs_i] < 0) {
1952 error_report("Could not get size of %s: %s",
1953 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1954 ret = -1;
1955 goto out;
1956 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001957 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001958 }
bellardea2384d2004-08-01 21:59:26 +00001959
Wenchao Xiaef806542013-12-04 17:10:57 +08001960 if (sn_opts) {
1961 ret = bdrv_snapshot_load_tmp(bs[0],
1962 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1963 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1964 &local_err);
1965 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001966 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001967 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001968 ret = -1;
1969 goto out;
1970 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001971
1972 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001973 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001974 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001975 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001976 ret = -1;
1977 goto out;
edison51ef6722010-09-21 19:58:41 -07001978 }
1979
Kevin Wolfefa84d42009-05-18 16:42:12 +02001980 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001981 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001982 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001983 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001984 ret = -1;
1985 goto out;
1986 }
balrog926c2d22007-10-31 01:11:44 +00001987
Max Reitzb65a5e12015-02-05 13:58:12 -05001988 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001989 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001990 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001991 ret = -1;
1992 goto out;
1993 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001994
Max Reitz2e024cd2015-02-11 09:58:46 -05001995 if (!skip_create) {
1996 if (!drv->create_opts) {
1997 error_report("Format driver '%s' does not support image creation",
1998 drv->format_name);
1999 ret = -1;
2000 goto out;
2001 }
Max Reitzf75613c2014-12-02 18:32:46 +01002002
Max Reitz2e024cd2015-02-11 09:58:46 -05002003 if (!proto_drv->create_opts) {
2004 error_report("Protocol driver '%s' does not support image creation",
2005 proto_drv->format_name);
2006 ret = -1;
2007 goto out;
2008 }
Max Reitzf75613c2014-12-02 18:32:46 +01002009
Max Reitz2e024cd2015-02-11 09:58:46 -05002010 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2011 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002012
Max Reitz2e024cd2015-02-11 09:58:46 -05002013 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002014 if (options) {
2015 qemu_opts_do_parse(opts, options, NULL, &local_err);
2016 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002017 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002018 ret = -1;
2019 goto out;
2020 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002021 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002022
Markus Armbruster39101f22015-02-12 16:46:36 +01002023 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
2024 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002025 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2026 if (ret < 0) {
2027 goto out;
2028 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002029 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002030
Kevin Wolfa18953f2010-10-14 15:46:04 +02002031 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002032 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002033 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002034 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002035 }
2036
Kevin Wolfefa84d42009-05-18 16:42:12 +02002037 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01002038 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002039 bool encryption =
2040 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2041 const char *preallocation =
2042 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002043
Pavel Butsykin35fadca2016-07-22 11:17:48 +03002044 if (!drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002045 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002046 ret = -1;
2047 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002048 }
2049
Chunyan Liu83d05212014-06-05 17:20:51 +08002050 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002051 error_report("Compression and encryption not supported at "
2052 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002053 ret = -1;
2054 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002055 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002056
Chunyan Liu83d05212014-06-05 17:20:51 +08002057 if (preallocation
2058 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002059 {
2060 error_report("Compression and preallocation not supported at "
2061 "the same time");
2062 ret = -1;
2063 goto out;
2064 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002065 }
2066
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002067 if (!skip_create) {
2068 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002069 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002070 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002071 error_reportf_err(local_err, "%s: error while converting %s: ",
2072 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002073 goto out;
bellardea2384d2004-08-01 21:59:26 +00002074 }
2075 }
ths3b46e622007-09-17 08:09:54 +00002076
Peter Lieven5a37b602013-10-24 12:07:06 +02002077 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002078 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002079 if (ret < 0) {
2080 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002081 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002082 }
2083
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002084 /* XXX we should allow --image-opts to trigger use of
2085 * img_open() here, but then we have trouble with
2086 * the bdrv_create() call which takes different params.
2087 * Not critical right now, so fix can wait...
2088 */
Kevin Wolfce099542016-03-15 13:01:04 +01002089 out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002090 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002091 ret = -1;
2092 goto out;
2093 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002094 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002095
Eric Blake5def6b82016-06-23 16:37:19 -06002096 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002097 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2098 * as maximum. */
2099 bufsectors = MIN(32768,
Eric Blake5def6b82016-06-23 16:37:19 -06002100 MAX(bufsectors,
2101 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
Eric Blakeb9f78552016-06-23 16:37:21 -06002102 out_bs->bl.pdiscard_alignment >>
2103 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002104
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002105 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002106 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002107 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002108 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002109 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002110 ret = -1;
2111 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002112 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002113 error_report("output file is smaller than input file");
2114 ret = -1;
2115 goto out;
2116 }
2117 }
2118
Peter Lieven24f833c2013-11-27 11:07:07 +01002119 cluster_sectors = 0;
2120 ret = bdrv_get_info(out_bs, &bdi);
2121 if (ret < 0) {
2122 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002123 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002124 goto out;
2125 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002126 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002127 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002128 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2129 }
2130
Kevin Wolf690c7302015-03-19 13:33:32 +01002131 state = (ImgConvertState) {
2132 .src = blk,
2133 .src_sectors = bs_sectors,
2134 .src_num = bs_n,
2135 .total_sectors = total_sectors,
2136 .target = out_blk,
2137 .compressed = compress,
2138 .target_has_backing = (bool) out_baseimg,
2139 .min_sparse = min_sparse,
2140 .cluster_sectors = cluster_sectors,
2141 .buf_sectors = bufsectors,
2142 };
2143 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002144
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002145out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002146 if (!ret) {
2147 qemu_progress_print(100, 0);
2148 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002149 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002150 qemu_opts_del(opts);
2151 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002152 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002153 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002154 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002155 if (blk) {
2156 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2157 blk_unref(blk[bs_i]);
2158 }
2159 g_free(blk);
2160 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002161 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002162fail_getopt:
2163 g_free(options);
2164
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002165 if (ret) {
2166 return 1;
2167 }
bellardea2384d2004-08-01 21:59:26 +00002168 return 0;
2169}
2170
bellard57d1a2b2004-08-03 21:15:11 +00002171
bellardfaea38e2006-08-05 21:31:00 +00002172static void dump_snapshots(BlockDriverState *bs)
2173{
2174 QEMUSnapshotInfo *sn_tab, *sn;
2175 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002176
2177 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2178 if (nb_sns <= 0)
2179 return;
2180 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002181 bdrv_snapshot_dump(fprintf, stdout, NULL);
2182 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002183 for(i = 0; i < nb_sns; i++) {
2184 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002185 bdrv_snapshot_dump(fprintf, stdout, sn);
2186 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002187 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002188 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002189}
2190
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002191static void dump_json_image_info_list(ImageInfoList *list)
2192{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002193 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002194 QObject *obj;
Eric Blake3b098d52016-06-09 10:48:43 -06002195 Visitor *v = qmp_output_visitor_new(&obj);
2196
2197 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2198 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002199 str = qobject_to_json_pretty(obj);
2200 assert(str != NULL);
2201 printf("%s\n", qstring_get_str(str));
2202 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002203 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002204 QDECREF(str);
2205}
2206
Benoît Canetc054b3f2012-09-05 13:09:02 +02002207static void dump_json_image_info(ImageInfo *info)
2208{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002209 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002210 QObject *obj;
Eric Blake3b098d52016-06-09 10:48:43 -06002211 Visitor *v = qmp_output_visitor_new(&obj);
2212
2213 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2214 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002215 str = qobject_to_json_pretty(obj);
2216 assert(str != NULL);
2217 printf("%s\n", qstring_get_str(str));
2218 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002219 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002220 QDECREF(str);
2221}
2222
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002223static void dump_human_image_info_list(ImageInfoList *list)
2224{
2225 ImageInfoList *elem;
2226 bool delim = false;
2227
2228 for (elem = list; elem; elem = elem->next) {
2229 if (delim) {
2230 printf("\n");
2231 }
2232 delim = true;
2233
Wenchao Xia5b917042013-05-25 11:09:45 +08002234 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002235 }
2236}
2237
2238static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2239{
2240 return strcmp(a, b) == 0;
2241}
2242
2243/**
2244 * Open an image file chain and return an ImageInfoList
2245 *
2246 * @filename: topmost image filename
2247 * @fmt: topmost image format (may be NULL to autodetect)
2248 * @chain: true - enumerate entire backing file chain
2249 * false - only topmost image file
2250 *
2251 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2252 * image file. If there was an error a message will have been printed to
2253 * stderr.
2254 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002255static ImageInfoList *collect_image_info_list(bool image_opts,
2256 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002257 const char *fmt,
2258 bool chain)
2259{
2260 ImageInfoList *head = NULL;
2261 ImageInfoList **last = &head;
2262 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002263 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002264
2265 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2266
2267 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002268 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002269 BlockDriverState *bs;
2270 ImageInfo *info;
2271 ImageInfoList *elem;
2272
2273 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2274 error_report("Backing file '%s' creates an infinite loop.",
2275 filename);
2276 goto err;
2277 }
2278 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2279
Max Reitzefaa7c42016-03-16 19:54:38 +01002280 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002281 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002282 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002283 goto err;
2284 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002285 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002286
Wenchao Xia43526ec2013-06-06 12:27:58 +08002287 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002288 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002289 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002290 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002291 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002292 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002293
2294 elem = g_new0(ImageInfoList, 1);
2295 elem->value = info;
2296 *last = elem;
2297 last = &elem->next;
2298
Markus Armbruster26f54e92014-10-07 13:59:04 +02002299 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002300
2301 filename = fmt = NULL;
2302 if (chain) {
2303 if (info->has_full_backing_filename) {
2304 filename = info->full_backing_filename;
2305 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002306 error_report("Could not determine absolute backing filename,"
2307 " but backing filename '%s' present",
2308 info->backing_filename);
2309 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002310 }
2311 if (info->has_backing_filename_format) {
2312 fmt = info->backing_filename_format;
2313 }
2314 }
2315 }
2316 g_hash_table_destroy(filenames);
2317 return head;
2318
2319err:
2320 qapi_free_ImageInfoList(head);
2321 g_hash_table_destroy(filenames);
2322 return NULL;
2323}
2324
Benoît Canetc054b3f2012-09-05 13:09:02 +02002325static int img_info(int argc, char **argv)
2326{
2327 int c;
2328 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002329 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002330 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002331 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002332 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002333
bellardea2384d2004-08-01 21:59:26 +00002334 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002335 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002336 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002337 int option_index = 0;
2338 static const struct option long_options[] = {
2339 {"help", no_argument, 0, 'h'},
2340 {"format", required_argument, 0, 'f'},
2341 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002342 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002343 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002344 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002345 {0, 0, 0, 0}
2346 };
2347 c = getopt_long(argc, argv, "f:h",
2348 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002349 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002350 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002351 }
bellardea2384d2004-08-01 21:59:26 +00002352 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002353 case '?':
bellardea2384d2004-08-01 21:59:26 +00002354 case 'h':
2355 help();
2356 break;
2357 case 'f':
2358 fmt = optarg;
2359 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002360 case OPTION_OUTPUT:
2361 output = optarg;
2362 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002363 case OPTION_BACKING_CHAIN:
2364 chain = true;
2365 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002366 case OPTION_OBJECT: {
2367 QemuOpts *opts;
2368 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2369 optarg, true);
2370 if (!opts) {
2371 return 1;
2372 }
2373 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002374 case OPTION_IMAGE_OPTS:
2375 image_opts = true;
2376 break;
bellardea2384d2004-08-01 21:59:26 +00002377 }
2378 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002379 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002380 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002381 }
bellardea2384d2004-08-01 21:59:26 +00002382 filename = argv[optind++];
2383
Benoît Canetc054b3f2012-09-05 13:09:02 +02002384 if (output && !strcmp(output, "json")) {
2385 output_format = OFORMAT_JSON;
2386 } else if (output && !strcmp(output, "human")) {
2387 output_format = OFORMAT_HUMAN;
2388 } else if (output) {
2389 error_report("--output must be used with human or json as argument.");
2390 return 1;
2391 }
2392
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002393 if (qemu_opts_foreach(&qemu_object_opts,
2394 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002395 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002396 return 1;
2397 }
2398
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002399 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002400 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002401 return 1;
2402 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002403
Benoît Canetc054b3f2012-09-05 13:09:02 +02002404 switch (output_format) {
2405 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002406 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002407 break;
2408 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002409 if (chain) {
2410 dump_json_image_info_list(list);
2411 } else {
2412 dump_json_image_info(list->value);
2413 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002414 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002415 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002416
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002417 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002418 return 0;
2419}
2420
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002421static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2422 MapEntry *next)
2423{
2424 switch (output_format) {
2425 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002426 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002427 error_report("File contains external, encrypted or compressed clusters.");
2428 exit(1);
2429 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002430 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002431 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002432 e->start, e->length,
2433 e->has_offset ? e->offset : 0,
2434 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002435 }
2436 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2437 * Modify the flags here to allow more coalescing.
2438 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002439 if (next && (!next->data || next->zero)) {
2440 next->data = false;
2441 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002442 }
2443 break;
2444 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002445 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2446 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002447 (e->start == 0 ? "[" : ",\n"),
2448 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002449 e->zero ? "true" : "false",
2450 e->data ? "true" : "false");
2451 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002452 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002453 }
2454 putchar('}');
2455
2456 if (!next) {
2457 printf("]\n");
2458 }
2459 break;
2460 }
2461}
2462
2463static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2464 int nb_sectors, MapEntry *e)
2465{
2466 int64_t ret;
2467 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002468 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002469 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002470
2471 /* As an optimization, we could cache the current range of unallocated
2472 * clusters in each file of the chain, and avoid querying the same
2473 * range repeatedly.
2474 */
2475
2476 depth = 0;
2477 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002478 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2479 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002480 if (ret < 0) {
2481 return ret;
2482 }
2483 assert(nb_sectors);
2484 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2485 break;
2486 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002487 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002488 if (bs == NULL) {
2489 ret = 0;
2490 break;
2491 }
2492
2493 depth++;
2494 }
2495
John Snow28756452016-02-05 13:12:33 -05002496 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2497
2498 *e = (MapEntry) {
2499 .start = sector_num * BDRV_SECTOR_SIZE,
2500 .length = nb_sectors * BDRV_SECTOR_SIZE,
2501 .data = !!(ret & BDRV_BLOCK_DATA),
2502 .zero = !!(ret & BDRV_BLOCK_ZERO),
2503 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2504 .has_offset = has_offset,
2505 .depth = depth,
2506 .has_filename = file && has_offset,
2507 .filename = file && has_offset ? file->filename : NULL,
2508 };
2509
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002510 return 0;
2511}
2512
Fam Zheng16b0d552016-01-26 11:59:02 +08002513static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2514{
2515 if (curr->length == 0) {
2516 return false;
2517 }
2518 if (curr->zero != next->zero ||
2519 curr->data != next->data ||
2520 curr->depth != next->depth ||
2521 curr->has_filename != next->has_filename ||
2522 curr->has_offset != next->has_offset) {
2523 return false;
2524 }
2525 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2526 return false;
2527 }
2528 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2529 return false;
2530 }
2531 return true;
2532}
2533
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002534static int img_map(int argc, char **argv)
2535{
2536 int c;
2537 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002538 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002539 BlockDriverState *bs;
2540 const char *filename, *fmt, *output;
2541 int64_t length;
2542 MapEntry curr = { .length = 0 }, next;
2543 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002544 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002545
2546 fmt = NULL;
2547 output = NULL;
2548 for (;;) {
2549 int option_index = 0;
2550 static const struct option long_options[] = {
2551 {"help", no_argument, 0, 'h'},
2552 {"format", required_argument, 0, 'f'},
2553 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002554 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002555 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002556 {0, 0, 0, 0}
2557 };
2558 c = getopt_long(argc, argv, "f:h",
2559 long_options, &option_index);
2560 if (c == -1) {
2561 break;
2562 }
2563 switch (c) {
2564 case '?':
2565 case 'h':
2566 help();
2567 break;
2568 case 'f':
2569 fmt = optarg;
2570 break;
2571 case OPTION_OUTPUT:
2572 output = optarg;
2573 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002574 case OPTION_OBJECT: {
2575 QemuOpts *opts;
2576 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2577 optarg, true);
2578 if (!opts) {
2579 return 1;
2580 }
2581 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002582 case OPTION_IMAGE_OPTS:
2583 image_opts = true;
2584 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002585 }
2586 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002587 if (optind != argc - 1) {
2588 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002589 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002590 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002591
2592 if (output && !strcmp(output, "json")) {
2593 output_format = OFORMAT_JSON;
2594 } else if (output && !strcmp(output, "human")) {
2595 output_format = OFORMAT_HUMAN;
2596 } else if (output) {
2597 error_report("--output must be used with human or json as argument.");
2598 return 1;
2599 }
2600
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002601 if (qemu_opts_foreach(&qemu_object_opts,
2602 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002603 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002604 return 1;
2605 }
2606
Kevin Wolfce099542016-03-15 13:01:04 +01002607 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002608 if (!blk) {
2609 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002610 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002611 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002612
2613 if (output_format == OFORMAT_HUMAN) {
2614 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2615 }
2616
Max Reitzf1d3cd72015-02-05 13:58:18 -05002617 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002618 while (curr.start + curr.length < length) {
2619 int64_t nsectors_left;
2620 int64_t sector_num;
2621 int n;
2622
2623 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2624
2625 /* Probe up to 1 GiB at a time. */
2626 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2627 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2628 ret = get_block_status(bs, sector_num, n, &next);
2629
2630 if (ret < 0) {
2631 error_report("Could not read file metadata: %s", strerror(-ret));
2632 goto out;
2633 }
2634
Fam Zheng16b0d552016-01-26 11:59:02 +08002635 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002636 curr.length += next.length;
2637 continue;
2638 }
2639
2640 if (curr.length > 0) {
2641 dump_map_entry(output_format, &curr, &next);
2642 }
2643 curr = next;
2644 }
2645
2646 dump_map_entry(output_format, &curr, NULL);
2647
2648out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002649 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002650 return ret < 0;
2651}
2652
aliguorif7b4a942009-01-07 17:40:15 +00002653#define SNAPSHOT_LIST 1
2654#define SNAPSHOT_CREATE 2
2655#define SNAPSHOT_APPLY 3
2656#define SNAPSHOT_DELETE 4
2657
Stuart Brady153859b2009-06-07 00:42:17 +01002658static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002659{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002660 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002661 BlockDriverState *bs;
2662 QEMUSnapshotInfo sn;
2663 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002664 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002665 int action = 0;
2666 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002667 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002668 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002669 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002670
Kevin Wolfce099542016-03-15 13:01:04 +01002671 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002672 /* Parse commandline parameters */
2673 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002674 static const struct option long_options[] = {
2675 {"help", no_argument, 0, 'h'},
2676 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002677 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002678 {0, 0, 0, 0}
2679 };
2680 c = getopt_long(argc, argv, "la:c:d:hq",
2681 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002682 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002683 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002684 }
aliguorif7b4a942009-01-07 17:40:15 +00002685 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002686 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002687 case 'h':
2688 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002689 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002690 case 'l':
2691 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002692 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002693 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002694 }
2695 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002696 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002697 break;
2698 case 'a':
2699 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002700 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002701 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002702 }
2703 action = SNAPSHOT_APPLY;
2704 snapshot_name = optarg;
2705 break;
2706 case 'c':
2707 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002708 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002709 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002710 }
2711 action = SNAPSHOT_CREATE;
2712 snapshot_name = optarg;
2713 break;
2714 case 'd':
2715 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002716 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002717 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002718 }
2719 action = SNAPSHOT_DELETE;
2720 snapshot_name = optarg;
2721 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002722 case 'q':
2723 quiet = true;
2724 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002725 case OPTION_OBJECT: {
2726 QemuOpts *opts;
2727 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2728 optarg, true);
2729 if (!opts) {
2730 return 1;
2731 }
2732 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002733 case OPTION_IMAGE_OPTS:
2734 image_opts = true;
2735 break;
aliguorif7b4a942009-01-07 17:40:15 +00002736 }
2737 }
2738
Kevin Wolffc11eb22013-08-05 10:53:04 +02002739 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002740 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002741 }
aliguorif7b4a942009-01-07 17:40:15 +00002742 filename = argv[optind++];
2743
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002744 if (qemu_opts_foreach(&qemu_object_opts,
2745 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002746 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002747 return 1;
2748 }
2749
aliguorif7b4a942009-01-07 17:40:15 +00002750 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002751 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002752 if (!blk) {
2753 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002754 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002755 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002756
2757 /* Perform the requested action */
2758 switch(action) {
2759 case SNAPSHOT_LIST:
2760 dump_snapshots(bs);
2761 break;
2762
2763 case SNAPSHOT_CREATE:
2764 memset(&sn, 0, sizeof(sn));
2765 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2766
2767 qemu_gettimeofday(&tv);
2768 sn.date_sec = tv.tv_sec;
2769 sn.date_nsec = tv.tv_usec * 1000;
2770
2771 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002772 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002773 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002774 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002775 }
aliguorif7b4a942009-01-07 17:40:15 +00002776 break;
2777
2778 case SNAPSHOT_APPLY:
2779 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002780 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002781 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002782 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002783 }
aliguorif7b4a942009-01-07 17:40:15 +00002784 break;
2785
2786 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002787 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002788 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002789 error_reportf_err(err, "Could not delete snapshot '%s': ",
2790 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002791 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002792 }
aliguorif7b4a942009-01-07 17:40:15 +00002793 break;
2794 }
2795
2796 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002797 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002798 if (ret) {
2799 return 1;
2800 }
Stuart Brady153859b2009-06-07 00:42:17 +01002801 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002802}
2803
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002804static int img_rebase(int argc, char **argv)
2805{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002806 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002807 uint8_t *buf_old = NULL;
2808 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002809 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002810 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002811 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2812 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002813 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002814 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002815 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002816 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002817 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002818 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002819
2820 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002821 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002822 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002823 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002824 out_baseimg = NULL;
2825 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002826 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002827 static const struct option long_options[] = {
2828 {"help", no_argument, 0, 'h'},
2829 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002830 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002831 {0, 0, 0, 0}
2832 };
2833 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2834 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002835 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002836 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002837 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002838 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002839 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002840 case 'h':
2841 help();
2842 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002843 case 'f':
2844 fmt = optarg;
2845 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002846 case 'F':
2847 out_basefmt = optarg;
2848 break;
2849 case 'b':
2850 out_baseimg = optarg;
2851 break;
2852 case 'u':
2853 unsafe = 1;
2854 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002855 case 'p':
2856 progress = 1;
2857 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002858 case 't':
2859 cache = optarg;
2860 break;
Max Reitz40055952014-07-22 22:58:42 +02002861 case 'T':
2862 src_cache = optarg;
2863 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002864 case 'q':
2865 quiet = true;
2866 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002867 case OPTION_OBJECT: {
2868 QemuOpts *opts;
2869 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2870 optarg, true);
2871 if (!opts) {
2872 return 1;
2873 }
2874 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002875 case OPTION_IMAGE_OPTS:
2876 image_opts = true;
2877 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002878 }
2879 }
2880
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002881 if (quiet) {
2882 progress = 0;
2883 }
2884
Fam Zhengac1307a2014-04-22 13:36:11 +08002885 if (optind != argc - 1) {
2886 error_exit("Expecting one image file name");
2887 }
2888 if (!unsafe && !out_baseimg) {
2889 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002890 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002891 filename = argv[optind++];
2892
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002893 if (qemu_opts_foreach(&qemu_object_opts,
2894 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002895 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002896 return 1;
2897 }
2898
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002899 qemu_progress_init(progress, 2.0);
2900 qemu_progress_print(0, 100);
2901
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002902 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01002903 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002904 if (ret < 0) {
2905 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002906 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002907 }
2908
Kevin Wolfce099542016-03-15 13:01:04 +01002909 src_flags = 0;
2910 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02002911 if (ret < 0) {
2912 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002913 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002914 }
2915
Kevin Wolfce099542016-03-15 13:01:04 +01002916 /* The source files are opened read-only, don't care about WCE */
2917 assert((src_flags & BDRV_O_RDWR) == 0);
2918 (void) src_writethrough;
2919
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002920 /*
2921 * Open the images.
2922 *
2923 * Ignore the old backing file for unsafe rebase in case we want to correct
2924 * the reference to a renamed or moved backing file.
2925 */
Kevin Wolfce099542016-03-15 13:01:04 +01002926 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002927 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002928 ret = -1;
2929 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002930 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002931 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002932
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002933 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002934 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002935 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002936 ret = -1;
2937 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002938 }
2939 }
2940
2941 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002942 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002943 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002944 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002945
Max Reitz644483d2015-02-05 13:58:17 -05002946 if (bs->backing_format[0] != '\0') {
2947 options = qdict_new();
2948 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2949 }
2950
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002951 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002952 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002953 options, src_flags, &local_err);
2954 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002955 error_reportf_err(local_err,
2956 "Could not open old backing file '%s': ",
2957 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002958 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002959 }
Max Reitz644483d2015-02-05 13:58:17 -05002960
Alex Bligha6166732012-10-16 13:46:18 +01002961 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002962 if (out_basefmt) {
2963 options = qdict_new();
2964 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2965 } else {
2966 options = NULL;
2967 }
2968
Max Reitzefaa7c42016-03-16 19:54:38 +01002969 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002970 options, src_flags, &local_err);
2971 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002972 error_reportf_err(local_err,
2973 "Could not open new backing file '%s': ",
2974 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002975 goto out;
2976 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002977 }
2978 }
2979
2980 /*
2981 * Check each unallocated cluster in the COW file. If it is unallocated,
2982 * accesses go to the backing file. We must therefore compare this cluster
2983 * in the old and new backing file, and if they differ we need to copy it
2984 * from the old backing file into the COW file.
2985 *
2986 * If qemu-img crashes during this step, no harm is done. The content of
2987 * the image is the same as the original one at any time.
2988 */
2989 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002990 int64_t num_sectors;
2991 int64_t old_backing_num_sectors;
2992 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002993 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002994 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002995 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002996
Max Reitzf1d3cd72015-02-05 13:58:18 -05002997 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2998 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002999
Max Reitzf1d3cd72015-02-05 13:58:18 -05003000 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003001 if (num_sectors < 0) {
3002 error_report("Could not get size of '%s': %s",
3003 filename, strerror(-num_sectors));
3004 ret = -1;
3005 goto out;
3006 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003007 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003008 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003009 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003010
3011 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3012 error_report("Could not get size of '%s': %s",
3013 backing_name, strerror(-old_backing_num_sectors));
3014 ret = -1;
3015 goto out;
3016 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003017 if (blk_new_backing) {
3018 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003019 if (new_backing_num_sectors < 0) {
3020 error_report("Could not get size of '%s': %s",
3021 out_baseimg, strerror(-new_backing_num_sectors));
3022 ret = -1;
3023 goto out;
3024 }
Alex Bligha6166732012-10-16 13:46:18 +01003025 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003026
Kevin Wolf1f710492012-10-12 14:29:18 +02003027 if (num_sectors != 0) {
3028 local_progress = (float)100 /
3029 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3030 }
3031
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003032 for (sector = 0; sector < num_sectors; sector += n) {
3033
3034 /* How many sectors can we handle with the next read? */
3035 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3036 n = (IO_BUF_SIZE / 512);
3037 } else {
3038 n = num_sectors - sector;
3039 }
3040
3041 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003042 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003043 if (ret < 0) {
3044 error_report("error while reading image metadata: %s",
3045 strerror(-ret));
3046 goto out;
3047 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003048 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003049 continue;
3050 }
3051
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003052 /*
3053 * Read old and new backing file and take into consideration that
3054 * backing files may be smaller than the COW image.
3055 */
3056 if (sector >= old_backing_num_sectors) {
3057 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3058 } else {
3059 if (sector + n > old_backing_num_sectors) {
3060 n = old_backing_num_sectors - sector;
3061 }
3062
Eric Blake91669202016-05-06 10:26:43 -06003063 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3064 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003065 if (ret < 0) {
3066 error_report("error while reading from old backing file");
3067 goto out;
3068 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003069 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003070
Max Reitzf1d3cd72015-02-05 13:58:18 -05003071 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003072 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3073 } else {
3074 if (sector + n > new_backing_num_sectors) {
3075 n = new_backing_num_sectors - sector;
3076 }
3077
Eric Blake91669202016-05-06 10:26:43 -06003078 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3079 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003080 if (ret < 0) {
3081 error_report("error while reading from new backing file");
3082 goto out;
3083 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003084 }
3085
3086 /* If they differ, we need to write to the COW file */
3087 uint64_t written = 0;
3088
3089 while (written < n) {
3090 int pnum;
3091
3092 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003093 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003094 {
Eric Blake91669202016-05-06 10:26:43 -06003095 ret = blk_pwrite(blk,
3096 (sector + written) << BDRV_SECTOR_BITS,
3097 buf_old + written * 512,
3098 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003099 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003100 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003101 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003102 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003103 }
3104 }
3105
3106 written += pnum;
3107 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003108 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003109 }
3110 }
3111
3112 /*
3113 * Change the backing file. All clusters that are different from the old
3114 * backing file are overwritten in the COW file now, so the visible content
3115 * doesn't change when we switch the backing file.
3116 */
Alex Bligha6166732012-10-16 13:46:18 +01003117 if (out_baseimg && *out_baseimg) {
3118 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3119 } else {
3120 ret = bdrv_change_backing_file(bs, NULL, NULL);
3121 }
3122
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003123 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003124 error_report("Could not change the backing file to '%s': No "
3125 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003126 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003127 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003128 out_baseimg, strerror(-ret));
3129 }
3130
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003131 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003132 /*
3133 * TODO At this point it is possible to check if any clusters that are
3134 * allocated in the COW file are the same in the backing file. If so, they
3135 * could be dropped from the COW file. Don't do this before switching the
3136 * backing file, in case of a crash this would lead to corruption.
3137 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003138out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003139 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003140 /* Cleanup */
3141 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003142 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003143 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003144 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003145 qemu_vfree(buf_old);
3146 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003147
Markus Armbruster26f54e92014-10-07 13:59:04 +02003148 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003149 if (ret) {
3150 return 1;
3151 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003152 return 0;
3153}
3154
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003155static int img_resize(int argc, char **argv)
3156{
Markus Armbruster6750e792015-02-12 17:43:08 +01003157 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003158 int c, ret, relative;
3159 const char *filename, *fmt, *size;
3160 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003161 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003162 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003163 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003164
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003165 static QemuOptsList resize_options = {
3166 .name = "resize_options",
3167 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3168 .desc = {
3169 {
3170 .name = BLOCK_OPT_SIZE,
3171 .type = QEMU_OPT_SIZE,
3172 .help = "Virtual disk size"
3173 }, {
3174 /* end of list */
3175 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003176 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003177 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003178 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003179
Kevin Wolfe80fec72011-04-29 10:58:12 +02003180 /* Remove size from argv manually so that negative numbers are not treated
3181 * as options by getopt. */
3182 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003183 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003184 return 1;
3185 }
3186
3187 size = argv[--argc];
3188
3189 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003190 fmt = NULL;
3191 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003192 static const struct option long_options[] = {
3193 {"help", no_argument, 0, 'h'},
3194 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003195 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003196 {0, 0, 0, 0}
3197 };
3198 c = getopt_long(argc, argv, "f:hq",
3199 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003200 if (c == -1) {
3201 break;
3202 }
3203 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003204 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003205 case 'h':
3206 help();
3207 break;
3208 case 'f':
3209 fmt = optarg;
3210 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003211 case 'q':
3212 quiet = true;
3213 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003214 case OPTION_OBJECT: {
3215 QemuOpts *opts;
3216 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3217 optarg, true);
3218 if (!opts) {
3219 return 1;
3220 }
3221 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003222 case OPTION_IMAGE_OPTS:
3223 image_opts = true;
3224 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003225 }
3226 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003227 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003228 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003229 }
3230 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003231
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003232 if (qemu_opts_foreach(&qemu_object_opts,
3233 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003234 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003235 return 1;
3236 }
3237
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003238 /* Choose grow, shrink, or absolute resize mode */
3239 switch (size[0]) {
3240 case '+':
3241 relative = 1;
3242 size++;
3243 break;
3244 case '-':
3245 relative = -1;
3246 size++;
3247 break;
3248 default:
3249 relative = 0;
3250 break;
3251 }
3252
3253 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003254 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003255 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003256 if (err) {
3257 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003258 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003259 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003260 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003261 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003262 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3263 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003264
Max Reitzefaa7c42016-03-16 19:54:38 +01003265 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01003266 BDRV_O_RDWR, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003267 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003268 ret = -1;
3269 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003270 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003271
3272 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003273 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003274 } else {
3275 total_size = n;
3276 }
3277 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003278 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003279 ret = -1;
3280 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003281 }
3282
Max Reitzf1d3cd72015-02-05 13:58:18 -05003283 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003284 switch (ret) {
3285 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003286 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003287 break;
3288 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003289 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003290 break;
3291 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003292 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003293 break;
3294 default:
Max Reitzbcf23482016-06-15 17:36:29 +02003295 error_report("Error resizing image: %s", strerror(-ret));
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003296 break;
3297 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003298out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003299 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003300 if (ret) {
3301 return 1;
3302 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003303 return 0;
3304}
3305
Max Reitz76a3a342014-10-27 11:12:51 +01003306static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003307 int64_t offset, int64_t total_work_size,
3308 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003309{
3310 qemu_progress_print(100.f * offset / total_work_size, 0);
3311}
3312
Max Reitz6f176b42013-09-03 10:09:50 +02003313static int img_amend(int argc, char **argv)
3314{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003315 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003316 int c, ret = 0;
3317 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003318 QemuOptsList *create_opts = NULL;
3319 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003320 const char *fmt = NULL, *filename, *cache;
3321 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003322 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003323 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003324 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003325 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003326 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003327
Max Reitzbd39e6e2014-07-22 22:58:43 +02003328 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003329 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003330 static const struct option long_options[] = {
3331 {"help", no_argument, 0, 'h'},
3332 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003333 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003334 {0, 0, 0, 0}
3335 };
3336 c = getopt_long(argc, argv, "ho:f:t:pq",
3337 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003338 if (c == -1) {
3339 break;
3340 }
3341
3342 switch (c) {
3343 case 'h':
3344 case '?':
3345 help();
3346 break;
3347 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003348 if (!is_valid_option_list(optarg)) {
3349 error_report("Invalid option list: %s", optarg);
3350 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003351 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003352 }
3353 if (!options) {
3354 options = g_strdup(optarg);
3355 } else {
3356 char *old_options = options;
3357 options = g_strdup_printf("%s,%s", options, optarg);
3358 g_free(old_options);
3359 }
Max Reitz6f176b42013-09-03 10:09:50 +02003360 break;
3361 case 'f':
3362 fmt = optarg;
3363 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003364 case 't':
3365 cache = optarg;
3366 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003367 case 'p':
3368 progress = true;
3369 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003370 case 'q':
3371 quiet = true;
3372 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003373 case OPTION_OBJECT:
3374 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3375 optarg, true);
3376 if (!opts) {
3377 ret = -1;
3378 goto out_no_progress;
3379 }
3380 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003381 case OPTION_IMAGE_OPTS:
3382 image_opts = true;
3383 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003384 }
3385 }
3386
Max Reitz6f176b42013-09-03 10:09:50 +02003387 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003388 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003389 }
3390
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003391 if (qemu_opts_foreach(&qemu_object_opts,
3392 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003393 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003394 ret = -1;
3395 goto out_no_progress;
3396 }
3397
Max Reitz76a3a342014-10-27 11:12:51 +01003398 if (quiet) {
3399 progress = false;
3400 }
3401 qemu_progress_init(progress, 1.0);
3402
Kevin Wolfa283cb62014-02-21 16:24:07 +01003403 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3404 if (fmt && has_help_option(options)) {
3405 /* If a format is explicitly specified (and possibly no filename is
3406 * given), print option help here */
3407 ret = print_block_option_help(filename, fmt);
3408 goto out;
3409 }
3410
3411 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003412 error_report("Expecting one image file name");
3413 ret = -1;
3414 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003415 }
Max Reitz6f176b42013-09-03 10:09:50 +02003416
Kevin Wolfce099542016-03-15 13:01:04 +01003417 flags = BDRV_O_RDWR;
3418 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003419 if (ret < 0) {
3420 error_report("Invalid cache option: %s", cache);
3421 goto out;
3422 }
3423
Kevin Wolfce099542016-03-15 13:01:04 +01003424 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003425 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003426 ret = -1;
3427 goto out;
3428 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003429 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003430
3431 fmt = bs->drv->format_name;
3432
Kevin Wolf626f84f2014-02-21 16:24:06 +01003433 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003434 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003435 ret = print_block_option_help(filename, fmt);
3436 goto out;
3437 }
3438
Max Reitzb2439d22014-12-02 18:32:47 +01003439 if (!bs->drv->create_opts) {
3440 error_report("Format driver '%s' does not support any options to amend",
3441 fmt);
3442 ret = -1;
3443 goto out;
3444 }
3445
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003446 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003447 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003448 if (options) {
3449 qemu_opts_do_parse(opts, options, NULL, &err);
3450 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003451 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003452 ret = -1;
3453 goto out;
3454 }
Max Reitz6f176b42013-09-03 10:09:50 +02003455 }
3456
Max Reitz76a3a342014-10-27 11:12:51 +01003457 /* In case the driver does not call amend_status_cb() */
3458 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003459 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003460 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003461 if (ret < 0) {
3462 error_report("Error while amending options: %s", strerror(-ret));
3463 goto out;
3464 }
3465
3466out:
Max Reitz76a3a342014-10-27 11:12:51 +01003467 qemu_progress_end();
3468
Max Reitze814dff2015-08-20 16:00:38 -07003469out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003470 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003471 qemu_opts_del(opts);
3472 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003473 g_free(options);
3474
Max Reitz6f176b42013-09-03 10:09:50 +02003475 if (ret) {
3476 return 1;
3477 }
3478 return 0;
3479}
3480
Kevin Wolfb6133b82014-08-05 14:17:13 +02003481typedef struct BenchData {
3482 BlockBackend *blk;
3483 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003484 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003485 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003486 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003487 int nrreq;
3488 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003489 int flush_interval;
3490 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003491 uint8_t *buf;
3492 QEMUIOVector *qiov;
3493
3494 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003495 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003496 uint64_t offset;
3497} BenchData;
3498
Kevin Wolf55d539c2016-06-03 13:59:41 +02003499static void bench_undrained_flush_cb(void *opaque, int ret)
3500{
3501 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003502 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003503 exit(EXIT_FAILURE);
3504 }
3505}
3506
Kevin Wolfb6133b82014-08-05 14:17:13 +02003507static void bench_cb(void *opaque, int ret)
3508{
3509 BenchData *b = opaque;
3510 BlockAIOCB *acb;
3511
3512 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003513 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003514 exit(EXIT_FAILURE);
3515 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003516
3517 if (b->in_flush) {
3518 /* Just finished a flush with drained queue: Start next requests */
3519 assert(b->in_flight == 0);
3520 b->in_flush = false;
3521 } else if (b->in_flight > 0) {
3522 int remaining = b->n - b->in_flight;
3523
Kevin Wolfb6133b82014-08-05 14:17:13 +02003524 b->n--;
3525 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003526
3527 /* Time for flush? Drain queue if requested, then flush */
3528 if (b->flush_interval && remaining % b->flush_interval == 0) {
3529 if (!b->in_flight || !b->drain_on_flush) {
3530 BlockCompletionFunc *cb;
3531
3532 if (b->drain_on_flush) {
3533 b->in_flush = true;
3534 cb = bench_cb;
3535 } else {
3536 cb = bench_undrained_flush_cb;
3537 }
3538
3539 acb = blk_aio_flush(b->blk, cb, b);
3540 if (!acb) {
3541 error_report("Failed to issue flush request");
3542 exit(EXIT_FAILURE);
3543 }
3544 }
3545 if (b->drain_on_flush) {
3546 return;
3547 }
3548 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003549 }
3550
3551 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003552 if (b->write) {
3553 acb = blk_aio_pwritev(b->blk, b->offset, b->qiov, 0,
3554 bench_cb, b);
3555 } else {
3556 acb = blk_aio_preadv(b->blk, b->offset, b->qiov, 0,
3557 bench_cb, b);
3558 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003559 if (!acb) {
3560 error_report("Failed to issue request");
3561 exit(EXIT_FAILURE);
3562 }
3563 b->in_flight++;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003564 b->offset += b->step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003565 b->offset %= b->image_size;
3566 }
3567}
3568
3569static int img_bench(int argc, char **argv)
3570{
3571 int c, ret = 0;
3572 const char *fmt = NULL, *filename;
3573 bool quiet = false;
3574 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003575 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003576 int count = 75000;
3577 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003578 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003579 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003580 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003581 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003582 int flush_interval = 0;
3583 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003584 int64_t image_size;
3585 BlockBackend *blk = NULL;
3586 BenchData data = {};
3587 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003588 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003589 struct timeval t1, t2;
3590 int i;
3591
3592 for (;;) {
3593 static const struct option long_options[] = {
3594 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003595 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003596 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003597 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003598 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003599 {0, 0, 0, 0}
3600 };
Kevin Wolf83de9be2015-07-13 13:13:17 +02003601 c = getopt_long(argc, argv, "hc:d:f:no:qs:S:t:w", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003602 if (c == -1) {
3603 break;
3604 }
3605
3606 switch (c) {
3607 case 'h':
3608 case '?':
3609 help();
3610 break;
3611 case 'c':
3612 {
3613 char *end;
3614 errno = 0;
3615 count = strtoul(optarg, &end, 0);
3616 if (errno || *end || count > INT_MAX) {
3617 error_report("Invalid request count specified");
3618 return 1;
3619 }
3620 break;
3621 }
3622 case 'd':
3623 {
3624 char *end;
3625 errno = 0;
3626 depth = strtoul(optarg, &end, 0);
3627 if (errno || *end || depth > INT_MAX) {
3628 error_report("Invalid queue depth specified");
3629 return 1;
3630 }
3631 break;
3632 }
3633 case 'f':
3634 fmt = optarg;
3635 break;
3636 case 'n':
3637 flags |= BDRV_O_NATIVE_AIO;
3638 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003639 case 'o':
3640 {
3641 char *end;
3642 errno = 0;
3643 offset = qemu_strtosz_suffix(optarg, &end,
3644 QEMU_STRTOSZ_DEFSUFFIX_B);
3645 if (offset < 0|| *end) {
3646 error_report("Invalid offset specified");
3647 return 1;
3648 }
3649 break;
3650 }
3651 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003652 case 'q':
3653 quiet = true;
3654 break;
3655 case 's':
3656 {
3657 int64_t sval;
3658 char *end;
3659
3660 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
3661 if (sval < 0 || sval > INT_MAX || *end) {
3662 error_report("Invalid buffer size specified");
3663 return 1;
3664 }
3665
3666 bufsize = sval;
3667 break;
3668 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003669 case 'S':
3670 {
3671 int64_t sval;
3672 char *end;
3673
3674 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
3675 if (sval < 0 || sval > INT_MAX || *end) {
3676 error_report("Invalid step size specified");
3677 return 1;
3678 }
3679
3680 step = sval;
3681 break;
3682 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003683 case 't':
3684 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3685 if (ret < 0) {
3686 error_report("Invalid cache mode");
3687 ret = -1;
3688 goto out;
3689 }
3690 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003691 case 'w':
3692 flags |= BDRV_O_RDWR;
3693 is_write = true;
3694 break;
3695 case OPTION_PATTERN:
3696 {
3697 char *end;
3698 errno = 0;
3699 pattern = strtoul(optarg, &end, 0);
3700 if (errno || *end || pattern > 0xff) {
3701 error_report("Invalid pattern byte specified");
3702 return 1;
3703 }
3704 break;
3705 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003706 case OPTION_FLUSH_INTERVAL:
3707 {
3708 char *end;
3709 errno = 0;
3710 flush_interval = strtoul(optarg, &end, 0);
3711 if (errno || *end || flush_interval > INT_MAX) {
3712 error_report("Invalid flush interval specified");
3713 return 1;
3714 }
3715 break;
3716 }
3717 case OPTION_NO_DRAIN:
3718 drain_on_flush = false;
3719 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003720 case OPTION_IMAGE_OPTS:
3721 image_opts = true;
3722 break;
3723 }
3724 }
3725
3726 if (optind != argc - 1) {
3727 error_exit("Expecting one image file name");
3728 }
3729 filename = argv[argc - 1];
3730
Kevin Wolf55d539c2016-06-03 13:59:41 +02003731 if (!is_write && flush_interval) {
3732 error_report("--flush-interval is only available in write tests");
3733 ret = -1;
3734 goto out;
3735 }
3736 if (flush_interval && flush_interval < depth) {
3737 error_report("Flush interval can't be smaller than depth");
3738 ret = -1;
3739 goto out;
3740 }
3741
Kevin Wolfb6133b82014-08-05 14:17:13 +02003742 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
3743 if (!blk) {
3744 ret = -1;
3745 goto out;
3746 }
3747
3748 image_size = blk_getlength(blk);
3749 if (image_size < 0) {
3750 ret = image_size;
3751 goto out;
3752 }
3753
3754 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003755 .blk = blk,
3756 .image_size = image_size,
3757 .bufsize = bufsize,
3758 .step = step ?: bufsize,
3759 .nrreq = depth,
3760 .n = count,
3761 .offset = offset,
3762 .write = is_write,
3763 .flush_interval = flush_interval,
3764 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02003765 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003766 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02003767 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02003768 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02003769 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02003770 if (flush_interval) {
3771 printf("Sending flush every %d requests\n", flush_interval);
3772 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003773
3774 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003775 memset(data.buf, pattern, data.nrreq * data.bufsize);
3776
Kevin Wolfb6133b82014-08-05 14:17:13 +02003777 data.qiov = g_new(QEMUIOVector, data.nrreq);
3778 for (i = 0; i < data.nrreq; i++) {
3779 qemu_iovec_init(&data.qiov[i], 1);
3780 qemu_iovec_add(&data.qiov[i],
3781 data.buf + i * data.bufsize, data.bufsize);
3782 }
3783
3784 gettimeofday(&t1, NULL);
3785 bench_cb(&data, 0);
3786
3787 while (data.n > 0) {
3788 main_loop_wait(false);
3789 }
3790 gettimeofday(&t2, NULL);
3791
3792 printf("Run completed in %3.3f seconds.\n",
3793 (t2.tv_sec - t1.tv_sec)
3794 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
3795
3796out:
3797 qemu_vfree(data.buf);
3798 blk_unref(blk);
3799
3800 if (ret) {
3801 return 1;
3802 }
3803 return 0;
3804}
3805
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003806#define C_BS 01
3807#define C_COUNT 02
3808#define C_IF 04
3809#define C_OF 010
3810
3811struct DdInfo {
3812 unsigned int flags;
3813 int64_t count;
3814};
3815
3816struct DdIo {
3817 int bsz; /* Block size */
3818 char *filename;
3819 uint8_t *buf;
3820};
3821
3822struct DdOpts {
3823 const char *name;
3824 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
3825 unsigned int flag;
3826};
3827
3828static int img_dd_bs(const char *arg,
3829 struct DdIo *in, struct DdIo *out,
3830 struct DdInfo *dd)
3831{
3832 char *end;
3833 int64_t res;
3834
3835 res = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
3836
3837 if (res <= 0 || res > INT_MAX || *end) {
3838 error_report("invalid number: '%s'", arg);
3839 return 1;
3840 }
3841 in->bsz = out->bsz = res;
3842
3843 return 0;
3844}
3845
3846static int img_dd_count(const char *arg,
3847 struct DdIo *in, struct DdIo *out,
3848 struct DdInfo *dd)
3849{
3850 char *end;
3851
3852 dd->count = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
3853
3854 if (dd->count < 0 || *end) {
3855 error_report("invalid number: '%s'", arg);
3856 return 1;
3857 }
3858
3859 return 0;
3860}
3861
3862static int img_dd_if(const char *arg,
3863 struct DdIo *in, struct DdIo *out,
3864 struct DdInfo *dd)
3865{
3866 in->filename = g_strdup(arg);
3867
3868 return 0;
3869}
3870
3871static int img_dd_of(const char *arg,
3872 struct DdIo *in, struct DdIo *out,
3873 struct DdInfo *dd)
3874{
3875 out->filename = g_strdup(arg);
3876
3877 return 0;
3878}
3879
3880static int img_dd(int argc, char **argv)
3881{
3882 int ret = 0;
3883 char *arg = NULL;
3884 char *tmp;
3885 BlockDriver *drv = NULL, *proto_drv = NULL;
3886 BlockBackend *blk1 = NULL, *blk2 = NULL;
3887 QemuOpts *opts = NULL;
3888 QemuOptsList *create_opts = NULL;
3889 Error *local_err = NULL;
3890 bool image_opts = false;
3891 int c, i;
3892 const char *out_fmt = "raw";
3893 const char *fmt = NULL;
3894 int64_t size = 0;
3895 int64_t block_count = 0, out_pos, in_pos;
3896 struct DdInfo dd = {
3897 .flags = 0,
3898 .count = 0,
3899 };
3900 struct DdIo in = {
3901 .bsz = 512, /* Block size is by default 512 bytes */
3902 .filename = NULL,
3903 .buf = NULL
3904 };
3905 struct DdIo out = {
3906 .bsz = 512,
3907 .filename = NULL,
3908 .buf = NULL
3909 };
3910
3911 const struct DdOpts options[] = {
3912 { "bs", img_dd_bs, C_BS },
3913 { "count", img_dd_count, C_COUNT },
3914 { "if", img_dd_if, C_IF },
3915 { "of", img_dd_of, C_OF },
3916 { NULL, NULL, 0 }
3917 };
3918 const struct option long_options[] = {
3919 { "help", no_argument, 0, 'h'},
3920 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3921 { 0, 0, 0, 0 }
3922 };
3923
3924 while ((c = getopt_long(argc, argv, "hf:O:", long_options, NULL))) {
3925 if (c == EOF) {
3926 break;
3927 }
3928 switch (c) {
3929 case 'O':
3930 out_fmt = optarg;
3931 break;
3932 case 'f':
3933 fmt = optarg;
3934 break;
3935 case '?':
3936 error_report("Try 'qemu-img --help' for more information.");
3937 ret = -1;
3938 goto out;
3939 case 'h':
3940 help();
3941 break;
3942 case OPTION_IMAGE_OPTS:
3943 image_opts = true;
3944 break;
3945 }
3946 }
3947
3948 for (i = optind; i < argc; i++) {
3949 int j;
3950 arg = g_strdup(argv[i]);
3951
3952 tmp = strchr(arg, '=');
3953 if (tmp == NULL) {
3954 error_report("unrecognized operand %s", arg);
3955 ret = -1;
3956 goto out;
3957 }
3958
3959 *tmp++ = '\0';
3960
3961 for (j = 0; options[j].name != NULL; j++) {
3962 if (!strcmp(arg, options[j].name)) {
3963 break;
3964 }
3965 }
3966 if (options[j].name == NULL) {
3967 error_report("unrecognized operand %s", arg);
3968 ret = -1;
3969 goto out;
3970 }
3971
3972 if (options[j].f(tmp, &in, &out, &dd) != 0) {
3973 ret = -1;
3974 goto out;
3975 }
3976 dd.flags |= options[j].flag;
3977 g_free(arg);
3978 arg = NULL;
3979 }
3980
3981 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
3982 error_report("Must specify both input and output files");
3983 ret = -1;
3984 goto out;
3985 }
3986 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false);
3987
3988 if (!blk1) {
3989 ret = -1;
3990 goto out;
3991 }
3992
3993 drv = bdrv_find_format(out_fmt);
3994 if (!drv) {
3995 error_report("Unknown file format");
3996 ret = -1;
3997 goto out;
3998 }
3999 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4000
4001 if (!proto_drv) {
4002 error_report_err(local_err);
4003 ret = -1;
4004 goto out;
4005 }
4006 if (!drv->create_opts) {
4007 error_report("Format driver '%s' does not support image creation",
4008 drv->format_name);
4009 ret = -1;
4010 goto out;
4011 }
4012 if (!proto_drv->create_opts) {
4013 error_report("Protocol driver '%s' does not support image creation",
4014 proto_drv->format_name);
4015 ret = -1;
4016 goto out;
4017 }
4018 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4019 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4020
4021 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4022
4023 size = blk_getlength(blk1);
4024 if (size < 0) {
4025 error_report("Failed to get size for '%s'", in.filename);
4026 ret = -1;
4027 goto out;
4028 }
4029
4030 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4031 dd.count * in.bsz < size) {
4032 size = dd.count * in.bsz;
4033 }
4034
4035 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
4036
4037 ret = bdrv_create(drv, out.filename, opts, &local_err);
4038 if (ret < 0) {
4039 error_reportf_err(local_err,
4040 "%s: error while creating output image: ",
4041 out.filename);
4042 ret = -1;
4043 goto out;
4044 }
4045
4046 blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
4047 false, false);
4048
4049 if (!blk2) {
4050 ret = -1;
4051 goto out;
4052 }
4053
4054 in.buf = g_new(uint8_t, in.bsz);
4055
4056 for (in_pos = 0, out_pos = 0; in_pos < size; block_count++) {
4057 int in_ret, out_ret;
4058
4059 if (in_pos + in.bsz > size) {
4060 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4061 } else {
4062 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4063 }
4064 if (in_ret < 0) {
4065 error_report("error while reading from input image file: %s",
4066 strerror(-in_ret));
4067 ret = -1;
4068 goto out;
4069 }
4070 in_pos += in_ret;
4071
4072 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4073
4074 if (out_ret < 0) {
4075 error_report("error while writing to output image file: %s",
4076 strerror(-out_ret));
4077 ret = -1;
4078 goto out;
4079 }
4080 out_pos += out_ret;
4081 }
4082
4083out:
4084 g_free(arg);
4085 qemu_opts_del(opts);
4086 qemu_opts_free(create_opts);
4087 blk_unref(blk1);
4088 blk_unref(blk2);
4089 g_free(in.filename);
4090 g_free(out.filename);
4091 g_free(in.buf);
4092 g_free(out.buf);
4093
4094 if (ret) {
4095 return 1;
4096 }
4097 return 0;
4098}
4099
Kevin Wolfb6133b82014-08-05 14:17:13 +02004100
Anthony Liguoric227f092009-10-01 16:12:16 -05004101static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004102#define DEF(option, callback, arg_string) \
4103 { option, callback },
4104#include "qemu-img-cmds.h"
4105#undef DEF
4106#undef GEN_DOCS
4107 { NULL, NULL, },
4108};
4109
bellardea2384d2004-08-01 21:59:26 +00004110int main(int argc, char **argv)
4111{
Anthony Liguoric227f092009-10-01 16:12:16 -05004112 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004113 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004114 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004115 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004116 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004117 static const struct option long_options[] = {
4118 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004119 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004120 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004121 {0, 0, 0, 0}
4122 };
bellardea2384d2004-08-01 21:59:26 +00004123
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004124#ifdef CONFIG_POSIX
4125 signal(SIGPIPE, SIG_IGN);
4126#endif
4127
Kevin Wolf53f76e52010-12-16 15:10:32 +01004128 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004129 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004130
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004131 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004132 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004133 exit(EXIT_FAILURE);
4134 }
4135
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004136 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004137
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004138 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004139 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004140 if (argc < 2) {
4141 error_exit("Not enough arguments");
4142 }
Stuart Brady153859b2009-06-07 00:42:17 +01004143
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004144 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004145 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004146 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004147
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004148 while ((c = getopt_long(argc, argv, "+hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004149 switch (c) {
4150 case 'h':
4151 help();
4152 return 0;
4153 case 'V':
4154 printf(QEMU_IMG_VERSION);
4155 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004156 case 'T':
4157 g_free(trace_file);
4158 trace_file = trace_opt_parse(optarg);
4159 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004160 }
bellardea2384d2004-08-01 21:59:26 +00004161 }
Stuart Brady153859b2009-06-07 00:42:17 +01004162
Denis V. Lunev10985132016-06-17 17:44:13 +03004163 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004164
Denis V. Lunev10985132016-06-17 17:44:13 +03004165 /* reset getopt_long scanning */
4166 argc -= optind;
4167 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004168 return 0;
4169 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004170 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004171 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004172
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004173 if (!trace_init_backends()) {
4174 exit(1);
4175 }
4176 trace_init_file(trace_file);
4177 qemu_set_log(LOG_TRACE);
4178
Denis V. Lunev10985132016-06-17 17:44:13 +03004179 /* find the command */
4180 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4181 if (!strcmp(cmdname, cmd->name)) {
4182 return cmd->handler(argc, argv);
4183 }
4184 }
Jeff Cody7db16892014-04-25 17:02:32 -04004185
Stuart Brady153859b2009-06-07 00:42:17 +01004186 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004187 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004188}