blob: 2194c2d8e6e41ac76ec64746f0880e38e11363ac [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"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000035#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020037#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010038#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020039#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080040#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010041#include "crypto/init.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020042#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000043
Don Slutz61979a62015-01-09 10:17:35 -050044#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Jeff Cody5f6979c2014-04-28 14:37:18 -040045 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
46
Anthony Liguoric227f092009-10-01 16:12:16 -050047typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010048 const char *name;
49 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050050} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010051
Federico Simoncelli8599ea42013-01-28 06:59:47 -050052enum {
53 OPTION_OUTPUT = 256,
54 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000055 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000056 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020057 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020058 OPTION_FLUSH_INTERVAL = 261,
59 OPTION_NO_DRAIN = 262,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050060};
61
62typedef enum OutputFormat {
63 OFORMAT_JSON,
64 OFORMAT_HUMAN,
65} OutputFormat;
66
Kevin Wolfe6996142016-03-15 13:03:11 +010067/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040068#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000069
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010070static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000071{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010072 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000073}
74
Fam Zhengac1307a2014-04-22 13:36:11 +080075static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
76{
77 va_list ap;
78
79 error_printf("qemu-img: ");
80
81 va_start(ap, fmt);
82 error_vprintf(fmt, ap);
83 va_end(ap);
84
85 error_printf("\nTry 'qemu-img --help' for more information\n");
86 exit(EXIT_FAILURE);
87}
88
blueswir1d2c639d2009-01-24 18:19:25 +000089/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080090static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000091{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010092 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040093 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +030094 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +030095 "QEMU disk image utility\n"
96 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +030097 " '-h', '--help' display this help and exit\n"
98 " '-V', '--version' output version information and exit\n"
99 "\n"
malc3f020d72010-02-08 12:04:56 +0300100 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100101#define DEF(option, callback, arg_string) \
102 " " arg_string "\n"
103#include "qemu-img-cmds.h"
104#undef DEF
105#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300106 "\n"
107 "Command parameters:\n"
108 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000109 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
110 " manual page for a description of the object properties. The most common\n"
111 " object type is a 'secret', which is used to supply passwords and/or\n"
112 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300113 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400114 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800115 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
116 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100117 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
118 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300119 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200120 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
121 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
122 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300123 " 'output_filename' is the destination disk image filename\n"
124 " 'output_fmt' is the destination format\n"
125 " 'options' is a comma separated list of format specific options in a\n"
126 " name=value format. Use -o ? for an overview of the options supported by the\n"
127 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800128 " 'snapshot_param' is param used for internal snapshot, format\n"
129 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
130 " '[ID_OR_NAME]'\n"
131 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
132 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300133 " '-c' indicates that target image must be compressed (qcow format only)\n"
134 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
135 " match exactly. The image doesn't need a working backing file before\n"
136 " rebasing in this case (useful for renaming the backing file)\n"
137 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200138 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100139 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200140 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
141 " contain only zeros for qemu-img to create a sparse image during\n"
142 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
143 " unallocated or zero sectors, and the destination image will always be\n"
144 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200145 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100146 " '-n' skips the target volume creation (useful if the volume is created\n"
147 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300148 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200149 "Parameters to check subcommand:\n"
150 " '-r' tries to repair any inconsistencies that are found during the check.\n"
151 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
152 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200153 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200154 "\n"
malc3f020d72010-02-08 12:04:56 +0300155 "Parameters to snapshot subcommand:\n"
156 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
157 " '-a' applies a snapshot (revert disk to saved state)\n"
158 " '-c' creates a snapshot\n"
159 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100160 " '-l' lists all snapshots in the given image\n"
161 "\n"
162 "Parameters to compare subcommand:\n"
163 " '-f' first image format\n"
164 " '-F' second image format\n"
165 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100166
167 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100168 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000169 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800170 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000171}
172
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000173static QemuOptsList qemu_object_opts = {
174 .name = "object",
175 .implied_opt_name = "qom-type",
176 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
177 .desc = {
178 { }
179 },
180};
181
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000182static QemuOptsList qemu_source_opts = {
183 .name = "source",
184 .implied_opt_name = "file",
185 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
186 .desc = {
187 { }
188 },
189};
190
Stefan Weil7c30f652013-06-16 17:01:05 +0200191static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100192{
193 int ret = 0;
194 if (!quiet) {
195 va_list args;
196 va_start(args, fmt);
197 ret = vprintf(fmt, args);
198 va_end(args);
199 }
200 return ret;
201}
202
bellardea2384d2004-08-01 21:59:26 +0000203
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100204static int print_block_option_help(const char *filename, const char *fmt)
205{
206 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800207 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500208 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100209
210 /* Find driver and parse its options */
211 drv = bdrv_find_format(fmt);
212 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100213 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100214 return 1;
215 }
216
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800217 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100218 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500219 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100220 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100221 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800222 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100223 return 1;
224 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800225 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100226 }
227
Chunyan Liu83d05212014-06-05 17:20:51 +0800228 qemu_opts_print_help(create_opts);
229 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100230 return 0;
231}
232
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000233
234static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000235 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000236{
237 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000238 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000239
240 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000241 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
242 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000243 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
244 if (qemu_read_password(password, sizeof(password)) < 0) {
245 error_report("No password given");
246 return -1;
247 }
248 if (bdrv_set_key(bs, password) < 0) {
249 error_report("invalid password");
250 return -1;
251 }
252 }
253 return 0;
254}
255
256
Max Reitzefaa7c42016-03-16 19:54:38 +0100257static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100258 QemuOpts *opts, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000259 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000260{
261 QDict *options;
262 Error *local_err = NULL;
263 BlockBackend *blk;
264 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100265 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000266 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100267 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000268 return NULL;
269 }
Kevin Wolfce099542016-03-15 13:01:04 +0100270 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000271
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000272 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000273 blk_unref(blk);
274 return NULL;
275 }
276 return blk;
277}
278
Max Reitzefaa7c42016-03-16 19:54:38 +0100279static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000280 const char *fmt, int flags,
Kevin Wolfce099542016-03-15 13:01:04 +0100281 bool writethrough, bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000282{
283 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200284 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500285 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100286
bellard75c23802004-08-27 21:28:58 +0000287 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500288 options = qdict_new();
289 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000290 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100291
Max Reitzefaa7c42016-03-16 19:54:38 +0100292 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500293 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100294 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000295 return NULL;
bellard75c23802004-08-27 21:28:58 +0000296 }
Kevin Wolfce099542016-03-15 13:01:04 +0100297 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100298
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000299 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300 blk_unref(blk);
301 return NULL;
bellard75c23802004-08-27 21:28:58 +0000302 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200303 return blk;
bellard75c23802004-08-27 21:28:58 +0000304}
305
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000306
Max Reitzefaa7c42016-03-16 19:54:38 +0100307static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000308 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100309 const char *fmt, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000310 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000311{
312 BlockBackend *blk;
313 if (image_opts) {
314 QemuOpts *opts;
315 if (fmt) {
316 error_report("--image-opts and --format are mutually exclusive");
317 return NULL;
318 }
319 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
320 filename, true);
321 if (!opts) {
322 return NULL;
323 }
Kevin Wolfce099542016-03-15 13:01:04 +0100324 blk = img_open_opts(filename, opts, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000325 } else {
Kevin Wolfce099542016-03-15 13:01:04 +0100326 blk = img_open_file(filename, fmt, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000327 }
328 return blk;
329}
330
331
Chunyan Liu83d05212014-06-05 17:20:51 +0800332static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100333 const char *base_filename,
334 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200335{
Markus Armbruster6750e792015-02-12 17:43:08 +0100336 Error *err = NULL;
337
Kevin Wolfefa84d42009-05-18 16:42:12 +0200338 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100339 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100340 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100341 error_report("Backing file not supported for file format '%s'",
342 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100343 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900344 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200345 }
346 }
347 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100348 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100349 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100350 error_report("Backing file format not supported for file "
351 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100352 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900353 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200354 }
355 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900356 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200357}
358
bellardea2384d2004-08-01 21:59:26 +0000359static int img_create(int argc, char **argv)
360{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200361 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100362 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000363 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000364 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000365 const char *filename;
366 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200367 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200368 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100369 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000370
bellardea2384d2004-08-01 21:59:26 +0000371 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000372 static const struct option long_options[] = {
373 {"help", no_argument, 0, 'h'},
374 {"object", required_argument, 0, OPTION_OBJECT},
375 {0, 0, 0, 0}
376 };
377 c = getopt_long(argc, argv, "F:b:f:he6o:q",
378 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100379 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000380 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100381 }
bellardea2384d2004-08-01 21:59:26 +0000382 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100383 case '?':
bellardea2384d2004-08-01 21:59:26 +0000384 case 'h':
385 help();
386 break;
aliguori9230eaf2009-03-28 17:55:19 +0000387 case 'F':
388 base_fmt = optarg;
389 break;
bellardea2384d2004-08-01 21:59:26 +0000390 case 'b':
391 base_filename = optarg;
392 break;
393 case 'f':
394 fmt = optarg;
395 break;
396 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200397 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100398 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100399 goto fail;
thsd8871c52007-10-24 16:11:42 +0000400 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200401 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100402 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100403 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200404 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100405 if (!is_valid_option_list(optarg)) {
406 error_report("Invalid option list: %s", optarg);
407 goto fail;
408 }
409 if (!options) {
410 options = g_strdup(optarg);
411 } else {
412 char *old_options = options;
413 options = g_strdup_printf("%s,%s", options, optarg);
414 g_free(old_options);
415 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200416 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100417 case 'q':
418 quiet = true;
419 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000420 case OPTION_OBJECT: {
421 QemuOpts *opts;
422 opts = qemu_opts_parse_noisily(&qemu_object_opts,
423 optarg, true);
424 if (!opts) {
425 goto fail;
426 }
427 } break;
bellardea2384d2004-08-01 21:59:26 +0000428 }
429 }
aliguori9230eaf2009-03-28 17:55:19 +0000430
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900431 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100432 filename = (optind < argc) ? argv[optind] : NULL;
433 if (options && has_help_option(options)) {
434 g_free(options);
435 return print_block_option_help(filename, fmt);
436 }
437
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100438 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800439 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100440 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100441 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900442
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000443 if (qemu_opts_foreach(&qemu_object_opts,
444 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200445 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000446 goto fail;
447 }
448
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100449 /* Get image size, if specified */
450 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100451 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100452 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200453 sval = qemu_strtosz_suffix(argv[optind++], &end,
454 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100455 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800456 if (sval == -ERANGE) {
457 error_report("Image size must be less than 8 EiB!");
458 } else {
459 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200460 "G, T, P or E suffixes for ");
461 error_report("kilobytes, megabytes, gigabytes, terabytes, "
462 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800463 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100464 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100465 }
466 img_size = (uint64_t)sval;
467 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200468 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800469 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200470 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100471
Luiz Capitulino9b375252012-11-30 10:52:05 -0200472 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Kevin Wolf61de4c62016-03-18 17:46:45 +0100473 options, img_size, 0, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100474 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100475 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100476 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900477 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200478
Kevin Wolf77386bf2014-02-21 16:24:04 +0100479 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000480 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100481
482fail:
483 g_free(options);
484 return 1;
bellardea2384d2004-08-01 21:59:26 +0000485}
486
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100487static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500488{
Markus Armbruster4399c432014-04-25 16:50:32 +0200489 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500490 QString *str;
491 QmpOutputVisitor *ov = qmp_output_visitor_new();
492 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -0700493 visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
494 &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500495 obj = qmp_output_get_qobject(ov);
496 str = qobject_to_json_pretty(obj);
497 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100498 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500499 qobject_decref(obj);
500 qmp_output_visitor_cleanup(ov);
501 QDECREF(str);
502}
503
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100504static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500505{
506 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100507 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500508 } else {
509 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100510 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
511 "Data may be corrupted, or further writes to the image "
512 "may corrupt it.\n",
513 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500514 }
515
516 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100517 qprintf(quiet,
518 "\n%" PRId64 " leaked clusters were found on the image.\n"
519 "This means waste of disk space, but no harm to data.\n",
520 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500521 }
522
523 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100524 qprintf(quiet,
525 "\n%" PRId64
526 " internal errors have occurred during the check.\n",
527 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500528 }
529 }
530
531 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100532 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
533 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
534 check->allocated_clusters, check->total_clusters,
535 check->allocated_clusters * 100.0 / check->total_clusters,
536 check->fragmented_clusters * 100.0 / check->allocated_clusters,
537 check->compressed_clusters * 100.0 /
538 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500539 }
540
541 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100542 qprintf(quiet,
543 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500544 }
545}
546
547static int collect_image_check(BlockDriverState *bs,
548 ImageCheck *check,
549 const char *filename,
550 const char *fmt,
551 int fix)
552{
553 int ret;
554 BdrvCheckResult result;
555
556 ret = bdrv_check(bs, &result, fix);
557 if (ret < 0) {
558 return ret;
559 }
560
561 check->filename = g_strdup(filename);
562 check->format = g_strdup(bdrv_get_format_name(bs));
563 check->check_errors = result.check_errors;
564 check->corruptions = result.corruptions;
565 check->has_corruptions = result.corruptions != 0;
566 check->leaks = result.leaks;
567 check->has_leaks = result.leaks != 0;
568 check->corruptions_fixed = result.corruptions_fixed;
569 check->has_corruptions_fixed = result.corruptions != 0;
570 check->leaks_fixed = result.leaks_fixed;
571 check->has_leaks_fixed = result.leaks != 0;
572 check->image_end_offset = result.image_end_offset;
573 check->has_image_end_offset = result.image_end_offset != 0;
574 check->total_clusters = result.bfi.total_clusters;
575 check->has_total_clusters = result.bfi.total_clusters != 0;
576 check->allocated_clusters = result.bfi.allocated_clusters;
577 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
578 check->fragmented_clusters = result.bfi.fragmented_clusters;
579 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100580 check->compressed_clusters = result.bfi.compressed_clusters;
581 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500582
583 return 0;
584}
585
Kevin Wolfe076f332010-06-29 11:43:13 +0200586/*
587 * Checks an image for consistency. Exit codes:
588 *
Max Reitzd6635c42014-06-02 22:15:21 +0200589 * 0 - Check completed, image is good
590 * 1 - Check not completed because of internal errors
591 * 2 - Check completed, image is corrupted
592 * 3 - Check completed, image has leaked clusters, but is good otherwise
593 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200594 */
aliguori15859692009-04-21 23:11:53 +0000595static int img_check(int argc, char **argv)
596{
597 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500598 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200599 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200600 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000601 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200602 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100603 int flags = BDRV_O_CHECK;
604 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200605 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100606 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000607 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000608
609 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500610 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200611 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100612
aliguori15859692009-04-21 23:11:53 +0000613 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500614 int option_index = 0;
615 static const struct option long_options[] = {
616 {"help", no_argument, 0, 'h'},
617 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530618 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500619 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000620 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000621 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500622 {0, 0, 0, 0}
623 };
Max Reitz40055952014-07-22 22:58:42 +0200624 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500625 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100626 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000627 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100628 }
aliguori15859692009-04-21 23:11:53 +0000629 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100630 case '?':
aliguori15859692009-04-21 23:11:53 +0000631 case 'h':
632 help();
633 break;
634 case 'f':
635 fmt = optarg;
636 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200637 case 'r':
638 flags |= BDRV_O_RDWR;
639
640 if (!strcmp(optarg, "leaks")) {
641 fix = BDRV_FIX_LEAKS;
642 } else if (!strcmp(optarg, "all")) {
643 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
644 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800645 error_exit("Unknown option value for -r "
646 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200647 }
648 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500649 case OPTION_OUTPUT:
650 output = optarg;
651 break;
Max Reitz40055952014-07-22 22:58:42 +0200652 case 'T':
653 cache = optarg;
654 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100655 case 'q':
656 quiet = true;
657 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000658 case OPTION_OBJECT: {
659 QemuOpts *opts;
660 opts = qemu_opts_parse_noisily(&qemu_object_opts,
661 optarg, true);
662 if (!opts) {
663 return 1;
664 }
665 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000666 case OPTION_IMAGE_OPTS:
667 image_opts = true;
668 break;
aliguori15859692009-04-21 23:11:53 +0000669 }
670 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200671 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800672 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100673 }
aliguori15859692009-04-21 23:11:53 +0000674 filename = argv[optind++];
675
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500676 if (output && !strcmp(output, "json")) {
677 output_format = OFORMAT_JSON;
678 } else if (output && !strcmp(output, "human")) {
679 output_format = OFORMAT_HUMAN;
680 } else if (output) {
681 error_report("--output must be used with human or json as argument.");
682 return 1;
683 }
684
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000685 if (qemu_opts_foreach(&qemu_object_opts,
686 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200687 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000688 return 1;
689 }
690
Kevin Wolfce099542016-03-15 13:01:04 +0100691 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200692 if (ret < 0) {
693 error_report("Invalid source cache option: %s", cache);
694 return 1;
695 }
696
Kevin Wolfce099542016-03-15 13:01:04 +0100697 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200698 if (!blk) {
699 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900700 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200701 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500702
703 check = g_new0(ImageCheck, 1);
704 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200705
706 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200707 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200708 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500709 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200710 }
711
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500712 if (check->corruptions_fixed || check->leaks_fixed) {
713 int corruptions_fixed, leaks_fixed;
714
715 leaks_fixed = check->leaks_fixed;
716 corruptions_fixed = check->corruptions_fixed;
717
718 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100719 qprintf(quiet,
720 "The following inconsistencies were found and repaired:\n\n"
721 " %" PRId64 " leaked clusters\n"
722 " %" PRId64 " corruptions\n\n"
723 "Double checking the fixed image now...\n",
724 check->leaks_fixed,
725 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500726 }
727
728 ret = collect_image_check(bs, check, filename, fmt, 0);
729
730 check->leaks_fixed = leaks_fixed;
731 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200732 }
733
Max Reitz832390a2014-10-23 15:29:12 +0200734 if (!ret) {
735 switch (output_format) {
736 case OFORMAT_HUMAN:
737 dump_human_image_check(check, quiet);
738 break;
739 case OFORMAT_JSON:
740 dump_json_image_check(check, quiet);
741 break;
742 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500743 }
744
745 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200746 if (ret) {
747 error_report("Check failed: %s", strerror(-ret));
748 } else {
749 error_report("Check failed");
750 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500751 ret = 1;
752 goto fail;
753 }
754
755 if (check->corruptions) {
756 ret = 2;
757 } else if (check->leaks) {
758 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200759 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500760 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000761 }
762
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500763fail:
764 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200765 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500766 return ret;
aliguori15859692009-04-21 23:11:53 +0000767}
768
Max Reitzd4a32382014-10-24 15:57:37 +0200769typedef struct CommonBlockJobCBInfo {
770 BlockDriverState *bs;
771 Error **errp;
772} CommonBlockJobCBInfo;
773
774static void common_block_job_cb(void *opaque, int ret)
775{
776 CommonBlockJobCBInfo *cbi = opaque;
777
778 if (ret < 0) {
779 error_setg_errno(cbi->errp, -ret, "Block job failed");
780 }
Max Reitzd4a32382014-10-24 15:57:37 +0200781}
782
783static void run_block_job(BlockJob *job, Error **errp)
784{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200785 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200786
787 do {
788 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500789 qemu_progress_print(job->len ?
790 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200791 } while (!job->ready);
792
793 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200794
795 /* A block job may finish instantaneously without publishing any progress,
796 * so just signal completion here */
797 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200798}
799
bellardea2384d2004-08-01 21:59:26 +0000800static int img_commit(int argc, char **argv)
801{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400802 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200803 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200804 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200805 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200806 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100807 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200808 Error *local_err = NULL;
809 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000810 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000811
812 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400813 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200814 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000815 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000816 static const struct option long_options[] = {
817 {"help", no_argument, 0, 'h'},
818 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000819 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000820 {0, 0, 0, 0}
821 };
822 c = getopt_long(argc, argv, "f:ht:b:dpq",
823 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100824 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000825 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100826 }
bellardea2384d2004-08-01 21:59:26 +0000827 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100828 case '?':
bellardea2384d2004-08-01 21:59:26 +0000829 case 'h':
830 help();
831 break;
832 case 'f':
833 fmt = optarg;
834 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400835 case 't':
836 cache = optarg;
837 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200838 case 'b':
839 base = optarg;
840 /* -b implies -d */
841 drop = true;
842 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200843 case 'd':
844 drop = true;
845 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200846 case 'p':
847 progress = true;
848 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100849 case 'q':
850 quiet = true;
851 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000852 case OPTION_OBJECT: {
853 QemuOpts *opts;
854 opts = qemu_opts_parse_noisily(&qemu_object_opts,
855 optarg, true);
856 if (!opts) {
857 return 1;
858 }
859 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000860 case OPTION_IMAGE_OPTS:
861 image_opts = true;
862 break;
bellardea2384d2004-08-01 21:59:26 +0000863 }
864 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200865
866 /* Progress is not shown in Quiet mode */
867 if (quiet) {
868 progress = false;
869 }
870
Kevin Wolffc11eb22013-08-05 10:53:04 +0200871 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800872 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100873 }
bellardea2384d2004-08-01 21:59:26 +0000874 filename = argv[optind++];
875
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000876 if (qemu_opts_foreach(&qemu_object_opts,
877 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200878 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000879 return 1;
880 }
881
Max Reitz9a86fe42014-10-24 15:57:38 +0200882 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100883 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400884 if (ret < 0) {
885 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100886 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400887 }
888
Kevin Wolfce099542016-03-15 13:01:04 +0100889 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200890 if (!blk) {
891 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900892 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200893 bs = blk_bs(blk);
894
Max Reitz687fa1d2014-10-24 15:57:39 +0200895 qemu_progress_init(progress, 1.f);
896 qemu_progress_print(0.f, 100);
897
Max Reitz1b22bff2014-10-24 15:57:40 +0200898 if (base) {
899 base_bs = bdrv_find_backing_image(bs, base);
900 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100901 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200902 goto done;
903 }
904 } else {
905 /* This is different from QMP, which by default uses the deepest file in
906 * the backing chain (i.e., the very base); however, the traditional
907 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200908 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200909 if (!base_bs) {
910 error_setg(&local_err, "Image does not have a backing file");
911 goto done;
912 }
bellardea2384d2004-08-01 21:59:26 +0000913 }
914
Max Reitzd4a32382014-10-24 15:57:37 +0200915 cbi = (CommonBlockJobCBInfo){
916 .errp = &local_err,
917 .bs = bs,
918 };
919
920 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
921 common_block_job_cb, &cbi, &local_err);
922 if (local_err) {
923 goto done;
924 }
925
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200926 /* When the block job completes, the BlockBackend reference will point to
927 * the old backing file. In order to avoid that the top image is already
928 * deleted, so we can still empty it afterwards, increment the reference
929 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200930 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200931 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200932 }
933
Max Reitzd4a32382014-10-24 15:57:37 +0200934 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200935 if (local_err) {
936 goto unref_backing;
937 }
938
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200939 if (!drop && bs->drv->bdrv_make_empty) {
940 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200941 if (ret) {
942 error_setg_errno(&local_err, -ret, "Could not empty %s",
943 filename);
944 goto unref_backing;
945 }
946 }
947
948unref_backing:
949 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200950 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200951 }
Max Reitzd4a32382014-10-24 15:57:37 +0200952
953done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200954 qemu_progress_end();
955
Markus Armbruster26f54e92014-10-07 13:59:04 +0200956 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200957
958 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100959 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900960 return 1;
961 }
Max Reitzd4a32382014-10-24 15:57:37 +0200962
963 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000964 return 0;
965}
966
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400967/*
thsf58c7b32008-06-05 21:53:49 +0000968 * Returns true iff the first sector pointed to by 'buf' contains at least
969 * a non-NUL byte.
970 *
971 * 'pnum' is set to the number of sectors (including and immediately following
972 * the first one) that are known to be in the same allocated/unallocated state.
973 */
bellardea2384d2004-08-01 21:59:26 +0000974static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
975{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000976 bool is_zero;
977 int i;
bellardea2384d2004-08-01 21:59:26 +0000978
979 if (n <= 0) {
980 *pnum = 0;
981 return 0;
982 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000983 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000984 for(i = 1; i < n; i++) {
985 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000986 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000987 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000988 }
bellardea2384d2004-08-01 21:59:26 +0000989 }
990 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000991 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000992}
993
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100994/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200995 * Like is_allocated_sectors, but if the buffer starts with a used sector,
996 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
997 * breaking up write requests for only small sparse areas.
998 */
999static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1000 int min)
1001{
1002 int ret;
1003 int num_checked, num_used;
1004
1005 if (n < min) {
1006 min = n;
1007 }
1008
1009 ret = is_allocated_sectors(buf, n, pnum);
1010 if (!ret) {
1011 return ret;
1012 }
1013
1014 num_used = *pnum;
1015 buf += BDRV_SECTOR_SIZE * *pnum;
1016 n -= *pnum;
1017 num_checked = num_used;
1018
1019 while (n > 0) {
1020 ret = is_allocated_sectors(buf, n, pnum);
1021
1022 buf += BDRV_SECTOR_SIZE * *pnum;
1023 n -= *pnum;
1024 num_checked += *pnum;
1025 if (ret) {
1026 num_used = num_checked;
1027 } else if (*pnum >= min) {
1028 break;
1029 }
1030 }
1031
1032 *pnum = num_used;
1033 return 1;
1034}
1035
1036/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001037 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1038 * buffers matches, non-zero otherwise.
1039 *
1040 * pnum is set to the number of sectors (including and immediately following
1041 * the first one) that are known to have the same comparison result
1042 */
1043static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1044 int *pnum)
1045{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001046 bool res;
1047 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001048
1049 if (n <= 0) {
1050 *pnum = 0;
1051 return 0;
1052 }
1053
1054 res = !!memcmp(buf1, buf2, 512);
1055 for(i = 1; i < n; i++) {
1056 buf1 += 512;
1057 buf2 += 512;
1058
1059 if (!!memcmp(buf1, buf2, 512) != res) {
1060 break;
1061 }
1062 }
1063
1064 *pnum = i;
1065 return res;
1066}
1067
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001068#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001069
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001070static int64_t sectors_to_bytes(int64_t sectors)
1071{
1072 return sectors << BDRV_SECTOR_BITS;
1073}
1074
1075static int64_t sectors_to_process(int64_t total, int64_t from)
1076{
1077 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1078}
1079
1080/*
1081 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1082 *
1083 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1084 * data and negative value on error.
1085 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001086 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001087 * @param sect_num: Number of first sector to check
1088 * @param sect_count: Number of sectors to check
1089 * @param filename: Name of disk file we are checking (logging purpose)
1090 * @param buffer: Allocated buffer for storing read data
1091 * @param quiet: Flag for quiet mode
1092 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001093static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001094 int sect_count, const char *filename,
1095 uint8_t *buffer, bool quiet)
1096{
1097 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001098 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1099 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001100 if (ret < 0) {
1101 error_report("Error while reading offset %" PRId64 " of %s: %s",
1102 sectors_to_bytes(sect_num), filename, strerror(-ret));
1103 return ret;
1104 }
1105 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1106 if (ret || pnum != sect_count) {
1107 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1108 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1109 return 1;
1110 }
1111
1112 return 0;
1113}
1114
1115/*
1116 * Compares two images. Exit codes:
1117 *
1118 * 0 - Images are identical
1119 * 1 - Images differ
1120 * >1 - Error occurred
1121 */
1122static int img_compare(int argc, char **argv)
1123{
Max Reitz40055952014-07-22 22:58:42 +02001124 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001125 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001126 BlockDriverState *bs1, *bs2;
1127 int64_t total_sectors1, total_sectors2;
1128 uint8_t *buf1 = NULL, *buf2 = NULL;
1129 int pnum1, pnum2;
1130 int allocated1, allocated2;
1131 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1132 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001133 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001134 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001135 int64_t total_sectors;
1136 int64_t sector_num = 0;
1137 int64_t nb_sectors;
1138 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001139 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001140 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001141
Max Reitz40055952014-07-22 22:58:42 +02001142 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001143 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001144 static const struct option long_options[] = {
1145 {"help", no_argument, 0, 'h'},
1146 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001147 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001148 {0, 0, 0, 0}
1149 };
1150 c = getopt_long(argc, argv, "hf:F:T:pqs",
1151 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001152 if (c == -1) {
1153 break;
1154 }
1155 switch (c) {
1156 case '?':
1157 case 'h':
1158 help();
1159 break;
1160 case 'f':
1161 fmt1 = optarg;
1162 break;
1163 case 'F':
1164 fmt2 = optarg;
1165 break;
Max Reitz40055952014-07-22 22:58:42 +02001166 case 'T':
1167 cache = optarg;
1168 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001169 case 'p':
1170 progress = true;
1171 break;
1172 case 'q':
1173 quiet = true;
1174 break;
1175 case 's':
1176 strict = true;
1177 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001178 case OPTION_OBJECT: {
1179 QemuOpts *opts;
1180 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1181 optarg, true);
1182 if (!opts) {
1183 ret = 2;
1184 goto out4;
1185 }
1186 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001187 case OPTION_IMAGE_OPTS:
1188 image_opts = true;
1189 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001190 }
1191 }
1192
1193 /* Progress is not shown in Quiet mode */
1194 if (quiet) {
1195 progress = false;
1196 }
1197
1198
Kevin Wolffc11eb22013-08-05 10:53:04 +02001199 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001200 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001201 }
1202 filename1 = argv[optind++];
1203 filename2 = argv[optind++];
1204
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001205 if (qemu_opts_foreach(&qemu_object_opts,
1206 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001207 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001208 ret = 2;
1209 goto out4;
1210 }
1211
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001212 /* Initialize before goto out */
1213 qemu_progress_init(progress, 2.0);
1214
Kevin Wolfce099542016-03-15 13:01:04 +01001215 flags = 0;
1216 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001217 if (ret < 0) {
1218 error_report("Invalid source cache option: %s", cache);
1219 ret = 2;
1220 goto out3;
1221 }
1222
Kevin Wolfce099542016-03-15 13:01:04 +01001223 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001224 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001226 goto out3;
1227 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001228
Kevin Wolfce099542016-03-15 13:01:04 +01001229 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001230 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001231 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001232 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001233 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001234 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001235 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001236
Max Reitzf1d3cd72015-02-05 13:58:18 -05001237 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1238 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1239 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001240 if (total_sectors1 < 0) {
1241 error_report("Can't get size of %s: %s",
1242 filename1, strerror(-total_sectors1));
1243 ret = 4;
1244 goto out;
1245 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001246 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001247 if (total_sectors2 < 0) {
1248 error_report("Can't get size of %s: %s",
1249 filename2, strerror(-total_sectors2));
1250 ret = 4;
1251 goto out;
1252 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001253 total_sectors = MIN(total_sectors1, total_sectors2);
1254 progress_base = MAX(total_sectors1, total_sectors2);
1255
1256 qemu_progress_print(0, 100);
1257
1258 if (strict && total_sectors1 != total_sectors2) {
1259 ret = 1;
1260 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1261 goto out;
1262 }
1263
1264 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001265 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001266 BlockDriverState *file;
1267
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001268 nb_sectors = sectors_to_process(total_sectors, sector_num);
1269 if (nb_sectors <= 0) {
1270 break;
1271 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001272 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1273 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001274 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001275 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001276 ret = 3;
1277 error_report("Sector allocation test failed for %s", filename1);
1278 goto out;
1279 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001280 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001281
Fam Zheng25ad8e62016-01-13 16:37:41 +08001282 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1283 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001284 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001285 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001286 ret = 3;
1287 error_report("Sector allocation test failed for %s", filename2);
1288 goto out;
1289 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001290 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1291 if (pnum1) {
1292 nb_sectors = MIN(nb_sectors, pnum1);
1293 }
1294 if (pnum2) {
1295 nb_sectors = MIN(nb_sectors, pnum2);
1296 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001297
Fam Zheng25ad8e62016-01-13 16:37:41 +08001298 if (strict) {
1299 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1300 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1301 ret = 1;
1302 qprintf(quiet, "Strict mode: Offset %" PRId64
1303 " block status mismatch!\n",
1304 sectors_to_bytes(sector_num));
1305 goto out;
1306 }
1307 }
1308 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1309 nb_sectors = MIN(pnum1, pnum2);
1310 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001311 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001312 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1313 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001314 if (ret < 0) {
1315 error_report("Error while reading offset %" PRId64 " of %s:"
1316 " %s", sectors_to_bytes(sector_num), filename1,
1317 strerror(-ret));
1318 ret = 4;
1319 goto out;
1320 }
Eric Blake91669202016-05-06 10:26:43 -06001321 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1322 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001323 if (ret < 0) {
1324 error_report("Error while reading offset %" PRId64
1325 " of %s: %s", sectors_to_bytes(sector_num),
1326 filename2, strerror(-ret));
1327 ret = 4;
1328 goto out;
1329 }
1330 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1331 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001332 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1333 sectors_to_bytes(
1334 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001335 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336 goto out;
1337 }
1338 }
1339 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001340
1341 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001342 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001343 filename1, buf1, quiet);
1344 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001345 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001346 filename2, buf1, quiet);
1347 }
1348 if (ret) {
1349 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001350 error_report("Error while reading offset %" PRId64 ": %s",
1351 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001352 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001353 }
1354 goto out;
1355 }
1356 }
1357 sector_num += nb_sectors;
1358 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1359 }
1360
1361 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001362 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001363 int64_t total_sectors_over;
1364 const char *filename_over;
1365
1366 qprintf(quiet, "Warning: Image size mismatch!\n");
1367 if (total_sectors1 > total_sectors2) {
1368 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001369 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001370 filename_over = filename1;
1371 } else {
1372 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001373 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001374 filename_over = filename2;
1375 }
1376
1377 for (;;) {
1378 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1379 if (nb_sectors <= 0) {
1380 break;
1381 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001382 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001383 nb_sectors, &pnum);
1384 if (ret < 0) {
1385 ret = 3;
1386 error_report("Sector allocation test failed for %s",
1387 filename_over);
1388 goto out;
1389
1390 }
1391 nb_sectors = pnum;
1392 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001393 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001394 filename_over, buf1, quiet);
1395 if (ret) {
1396 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001397 error_report("Error while reading offset %" PRId64
1398 " of %s: %s", sectors_to_bytes(sector_num),
1399 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001400 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001401 }
1402 goto out;
1403 }
1404 }
1405 sector_num += nb_sectors;
1406 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1407 }
1408 }
1409
1410 qprintf(quiet, "Images are identical.\n");
1411 ret = 0;
1412
1413out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414 qemu_vfree(buf1);
1415 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001416 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001417out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001418 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001419out3:
1420 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001421out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001422 return ret;
1423}
1424
Kevin Wolf690c7302015-03-19 13:33:32 +01001425enum ImgConvertBlockStatus {
1426 BLK_DATA,
1427 BLK_ZERO,
1428 BLK_BACKING_FILE,
1429};
1430
1431typedef struct ImgConvertState {
1432 BlockBackend **src;
1433 int64_t *src_sectors;
1434 int src_cur, src_num;
1435 int64_t src_cur_offset;
1436 int64_t total_sectors;
1437 int64_t allocated_sectors;
1438 enum ImgConvertBlockStatus status;
1439 int64_t sector_next_status;
1440 BlockBackend *target;
1441 bool has_zero_init;
1442 bool compressed;
1443 bool target_has_backing;
1444 int min_sparse;
1445 size_t cluster_sectors;
1446 size_t buf_sectors;
1447} ImgConvertState;
1448
1449static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1450{
1451 assert(sector_num >= s->src_cur_offset);
1452 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1453 s->src_cur_offset += s->src_sectors[s->src_cur];
1454 s->src_cur++;
1455 assert(s->src_cur < s->src_num);
1456 }
1457}
1458
1459static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1460{
1461 int64_t ret;
1462 int n;
1463
1464 convert_select_part(s, sector_num);
1465
1466 assert(s->total_sectors > sector_num);
1467 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1468
1469 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001470 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001471 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1472 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001473 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001474 if (ret < 0) {
1475 return ret;
1476 }
1477
1478 if (ret & BDRV_BLOCK_ZERO) {
1479 s->status = BLK_ZERO;
1480 } else if (ret & BDRV_BLOCK_DATA) {
1481 s->status = BLK_DATA;
1482 } else if (!s->target_has_backing) {
1483 /* Without a target backing file we must copy over the contents of
1484 * the backing file as well. */
Ren Kimura263a6f42016-04-28 01:04:58 +09001485 /* Check block status of the backing file chain to avoid
Kevin Wolf690c7302015-03-19 13:33:32 +01001486 * needlessly reading zeroes and limiting the iteration to the
1487 * buffer size */
Ren Kimura263a6f42016-04-28 01:04:58 +09001488 ret = bdrv_get_block_status_above(blk_bs(s->src[s->src_cur]), NULL,
1489 sector_num - s->src_cur_offset,
1490 n, &n, &file);
1491 if (ret < 0) {
1492 return ret;
1493 }
1494
1495 if (ret & BDRV_BLOCK_ZERO) {
1496 s->status = BLK_ZERO;
1497 } else {
1498 s->status = BLK_DATA;
1499 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001500 } else {
1501 s->status = BLK_BACKING_FILE;
1502 }
1503
1504 s->sector_next_status = sector_num + n;
1505 }
1506
1507 n = MIN(n, s->sector_next_status - sector_num);
1508 if (s->status == BLK_DATA) {
1509 n = MIN(n, s->buf_sectors);
1510 }
1511
1512 /* We need to write complete clusters for compressed images, so if an
1513 * unallocated area is shorter than that, we must consider the whole
1514 * cluster allocated. */
1515 if (s->compressed) {
1516 if (n < s->cluster_sectors) {
1517 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1518 s->status = BLK_DATA;
1519 } else {
1520 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1521 }
1522 }
1523
1524 return n;
1525}
1526
1527static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1528 uint8_t *buf)
1529{
1530 int n;
1531 int ret;
1532
Kevin Wolf690c7302015-03-19 13:33:32 +01001533 assert(nb_sectors <= s->buf_sectors);
1534 while (nb_sectors > 0) {
1535 BlockBackend *blk;
1536 int64_t bs_sectors;
1537
1538 /* In the case of compression with multiple source files, we can get a
1539 * nb_sectors that spreads into the next part. So we must be able to
1540 * read across multiple BDSes for one convert_read() call. */
1541 convert_select_part(s, sector_num);
1542 blk = s->src[s->src_cur];
1543 bs_sectors = s->src_sectors[s->src_cur];
1544
1545 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
Eric Blake91669202016-05-06 10:26:43 -06001546 ret = blk_pread(blk,
1547 (sector_num - s->src_cur_offset) << BDRV_SECTOR_BITS,
1548 buf, n << BDRV_SECTOR_BITS);
Kevin Wolf690c7302015-03-19 13:33:32 +01001549 if (ret < 0) {
1550 return ret;
1551 }
1552
1553 sector_num += n;
1554 nb_sectors -= n;
1555 buf += n * BDRV_SECTOR_SIZE;
1556 }
1557
1558 return 0;
1559}
1560
1561static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1562 const uint8_t *buf)
1563{
1564 int ret;
1565
1566 while (nb_sectors > 0) {
1567 int n = nb_sectors;
1568
1569 switch (s->status) {
1570 case BLK_BACKING_FILE:
1571 /* If we have a backing file, leave clusters unallocated that are
1572 * unallocated in the source image, so that the backing file is
1573 * visible at the respective offset. */
1574 assert(s->target_has_backing);
1575 break;
1576
1577 case BLK_DATA:
1578 /* We must always write compressed clusters as a whole, so don't
1579 * try to find zeroed parts in the buffer. We can only save the
1580 * write if the buffer is completely zeroed and we're allowed to
1581 * keep the target sparse. */
1582 if (s->compressed) {
1583 if (s->has_zero_init && s->min_sparse &&
1584 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1585 {
1586 assert(!s->target_has_backing);
1587 break;
1588 }
1589
1590 ret = blk_write_compressed(s->target, sector_num, buf, n);
1591 if (ret < 0) {
1592 return ret;
1593 }
1594 break;
1595 }
1596
1597 /* If there is real non-zero data or we're told to keep the target
1598 * fully allocated (-S 0), we must write it. Otherwise we can treat
1599 * it as zero sectors. */
1600 if (!s->min_sparse ||
1601 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1602 {
Eric Blake91669202016-05-06 10:26:43 -06001603 ret = blk_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1604 buf, n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001605 if (ret < 0) {
1606 return ret;
1607 }
1608 break;
1609 }
1610 /* fall-through */
1611
1612 case BLK_ZERO:
1613 if (s->has_zero_init) {
1614 break;
1615 }
Eric Blaked004bd52016-05-24 16:25:20 -06001616 ret = blk_pwrite_zeroes(s->target, sector_num << BDRV_SECTOR_BITS,
1617 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001618 if (ret < 0) {
1619 return ret;
1620 }
1621 break;
1622 }
1623
1624 sector_num += n;
1625 nb_sectors -= n;
1626 buf += n * BDRV_SECTOR_SIZE;
1627 }
1628
1629 return 0;
1630}
1631
1632static int convert_do_copy(ImgConvertState *s)
1633{
1634 uint8_t *buf = NULL;
1635 int64_t sector_num, allocated_done;
1636 int ret;
1637 int n;
1638
1639 /* Check whether we have zero initialisation or can get it efficiently */
1640 s->has_zero_init = s->min_sparse && !s->target_has_backing
1641 ? bdrv_has_zero_init(blk_bs(s->target))
1642 : false;
1643
1644 if (!s->has_zero_init && !s->target_has_backing &&
1645 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1646 {
1647 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1648 if (ret == 0) {
1649 s->has_zero_init = true;
1650 }
1651 }
1652
1653 /* Allocate buffer for copied data. For compressed images, only one cluster
1654 * can be copied at a time. */
1655 if (s->compressed) {
1656 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1657 error_report("invalid cluster size");
1658 ret = -EINVAL;
1659 goto fail;
1660 }
1661 s->buf_sectors = s->cluster_sectors;
1662 }
1663 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1664
1665 /* Calculate allocated sectors for progress */
1666 s->allocated_sectors = 0;
1667 sector_num = 0;
1668 while (sector_num < s->total_sectors) {
1669 n = convert_iteration_sectors(s, sector_num);
1670 if (n < 0) {
1671 ret = n;
1672 goto fail;
1673 }
Max Reitzaad15de2016-03-24 23:33:57 +01001674 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1675 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001676 s->allocated_sectors += n;
1677 }
1678 sector_num += n;
1679 }
1680
1681 /* Do the copy */
1682 s->src_cur = 0;
1683 s->src_cur_offset = 0;
1684 s->sector_next_status = 0;
1685
1686 sector_num = 0;
1687 allocated_done = 0;
1688
1689 while (sector_num < s->total_sectors) {
1690 n = convert_iteration_sectors(s, sector_num);
1691 if (n < 0) {
1692 ret = n;
1693 goto fail;
1694 }
Max Reitzaad15de2016-03-24 23:33:57 +01001695 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1696 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001697 allocated_done += n;
1698 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1699 0);
1700 }
1701
Max Reitzaad15de2016-03-24 23:33:57 +01001702 if (s->status == BLK_DATA) {
1703 ret = convert_read(s, sector_num, n, buf);
1704 if (ret < 0) {
1705 error_report("error while reading sector %" PRId64
1706 ": %s", sector_num, strerror(-ret));
1707 goto fail;
1708 }
1709 } else if (!s->min_sparse && s->status == BLK_ZERO) {
1710 n = MIN(n, s->buf_sectors);
1711 memset(buf, 0, n * BDRV_SECTOR_SIZE);
1712 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001713 }
1714
1715 ret = convert_write(s, sector_num, n, buf);
1716 if (ret < 0) {
1717 error_report("error while writing sector %" PRId64
1718 ": %s", sector_num, strerror(-ret));
1719 goto fail;
1720 }
1721
1722 sector_num += n;
1723 }
1724
1725 if (s->compressed) {
1726 /* signal EOF to align */
1727 ret = blk_write_compressed(s->target, 0, NULL, 0);
1728 if (ret < 0) {
1729 goto fail;
1730 }
1731 }
1732
1733 ret = 0;
1734fail:
1735 qemu_vfree(buf);
1736 return ret;
1737}
1738
bellardea2384d2004-08-01 21:59:26 +00001739static int img_convert(int argc, char **argv)
1740{
Kevin Wolf690c7302015-03-19 13:33:32 +01001741 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001742 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001743 int progress = 0, flags, src_flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001744 bool writethrough, src_writethrough;
Max Reitz40055952014-07-22 22:58:42 +02001745 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001746 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001747 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001748 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001749 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001750 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001751 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001752 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001753 QemuOpts *opts = NULL;
1754 QemuOptsList *create_opts = NULL;
1755 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001756 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001757 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001758 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001759 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001760 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001761 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001762 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001763 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001764
1765 fmt = NULL;
1766 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001767 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001768 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001769 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001770 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001771 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001772 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001773 static const struct option long_options[] = {
1774 {"help", no_argument, 0, 'h'},
1775 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001776 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001777 {0, 0, 0, 0}
1778 };
1779 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1780 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001781 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001782 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001783 }
bellardea2384d2004-08-01 21:59:26 +00001784 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001785 case '?':
bellardea2384d2004-08-01 21:59:26 +00001786 case 'h':
1787 help();
1788 break;
1789 case 'f':
1790 fmt = optarg;
1791 break;
1792 case 'O':
1793 out_fmt = optarg;
1794 break;
thsf58c7b32008-06-05 21:53:49 +00001795 case 'B':
1796 out_baseimg = optarg;
1797 break;
bellardea2384d2004-08-01 21:59:26 +00001798 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001799 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001800 break;
1801 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001802 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001803 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001804 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001805 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001806 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001807 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001808 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001809 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001810 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001811 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001812 if (!is_valid_option_list(optarg)) {
1813 error_report("Invalid option list: %s", optarg);
1814 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001815 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001816 }
1817 if (!options) {
1818 options = g_strdup(optarg);
1819 } else {
1820 char *old_options = options;
1821 options = g_strdup_printf("%s,%s", options, optarg);
1822 g_free(old_options);
1823 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001824 break;
edison51ef6722010-09-21 19:58:41 -07001825 case 's':
1826 snapshot_name = optarg;
1827 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001828 case 'l':
1829 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001830 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1831 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001832 if (!sn_opts) {
1833 error_report("Failed in parsing snapshot param '%s'",
1834 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001835 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001836 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001837 }
1838 } else {
1839 snapshot_name = optarg;
1840 }
1841 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001842 case 'S':
1843 {
1844 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001845 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001846 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001847 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001848 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001849 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001850 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001851 }
1852
1853 min_sparse = sval / BDRV_SECTOR_SIZE;
1854 break;
1855 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001856 case 'p':
1857 progress = 1;
1858 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001859 case 't':
1860 cache = optarg;
1861 break;
Max Reitz40055952014-07-22 22:58:42 +02001862 case 'T':
1863 src_cache = optarg;
1864 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001865 case 'q':
1866 quiet = true;
1867 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001868 case 'n':
1869 skip_create = 1;
1870 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001871 case OPTION_OBJECT:
1872 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1873 optarg, true);
1874 if (!opts) {
1875 goto fail_getopt;
1876 }
1877 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001878 case OPTION_IMAGE_OPTS:
1879 image_opts = true;
1880 break;
bellardea2384d2004-08-01 21:59:26 +00001881 }
1882 }
ths3b46e622007-09-17 08:09:54 +00001883
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001884 if (qemu_opts_foreach(&qemu_object_opts,
1885 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001886 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001887 goto fail_getopt;
1888 }
1889
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001890 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001891 if (quiet) {
1892 progress = 0;
1893 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001894 qemu_progress_init(progress, 1.0);
1895
balrog926c2d22007-10-31 01:11:44 +00001896 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001897 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001898
Kevin Wolf2dc83282014-02-21 16:24:05 +01001899 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001900 ret = print_block_option_help(out_filename, out_fmt);
1901 goto out;
1902 }
1903
bellardea2384d2004-08-01 21:59:26 +00001904 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001905 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001906 }
bellardea2384d2004-08-01 21:59:26 +00001907
thsf58c7b32008-06-05 21:53:49 +00001908
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001909 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001910 error_report("-B makes no sense when concatenating multiple input "
1911 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001912 ret = -1;
1913 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001914 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001915
Kevin Wolfce099542016-03-15 13:01:04 +01001916 src_flags = 0;
1917 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001918 if (ret < 0) {
1919 error_report("Invalid source cache option: %s", src_cache);
1920 goto out;
1921 }
1922
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001923 qemu_progress_print(0, 100);
1924
Markus Armbruster26f54e92014-10-07 13:59:04 +02001925 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001926 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001927 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001928
1929 total_sectors = 0;
1930 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001931 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Kevin Wolfce099542016-03-15 13:01:04 +01001932 fmt, src_flags, src_writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001933 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001934 ret = -1;
1935 goto out;
1936 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001937 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001938 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001939 if (bs_sectors[bs_i] < 0) {
1940 error_report("Could not get size of %s: %s",
1941 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1942 ret = -1;
1943 goto out;
1944 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001945 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001946 }
bellardea2384d2004-08-01 21:59:26 +00001947
Wenchao Xiaef806542013-12-04 17:10:57 +08001948 if (sn_opts) {
1949 ret = bdrv_snapshot_load_tmp(bs[0],
1950 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1951 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1952 &local_err);
1953 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001954 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001955 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001956 ret = -1;
1957 goto out;
1958 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001959
1960 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001961 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001962 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001963 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001964 ret = -1;
1965 goto out;
edison51ef6722010-09-21 19:58:41 -07001966 }
1967
Kevin Wolfefa84d42009-05-18 16:42:12 +02001968 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001969 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001970 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001971 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001972 ret = -1;
1973 goto out;
1974 }
balrog926c2d22007-10-31 01:11:44 +00001975
Max Reitzb65a5e12015-02-05 13:58:12 -05001976 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001977 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001978 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001979 ret = -1;
1980 goto out;
1981 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001982
Max Reitz2e024cd2015-02-11 09:58:46 -05001983 if (!skip_create) {
1984 if (!drv->create_opts) {
1985 error_report("Format driver '%s' does not support image creation",
1986 drv->format_name);
1987 ret = -1;
1988 goto out;
1989 }
Max Reitzf75613c2014-12-02 18:32:46 +01001990
Max Reitz2e024cd2015-02-11 09:58:46 -05001991 if (!proto_drv->create_opts) {
1992 error_report("Protocol driver '%s' does not support image creation",
1993 proto_drv->format_name);
1994 ret = -1;
1995 goto out;
1996 }
Max Reitzf75613c2014-12-02 18:32:46 +01001997
Max Reitz2e024cd2015-02-11 09:58:46 -05001998 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1999 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002000
Max Reitz2e024cd2015-02-11 09:58:46 -05002001 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002002 if (options) {
2003 qemu_opts_do_parse(opts, options, NULL, &local_err);
2004 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002005 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002006 ret = -1;
2007 goto out;
2008 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002009 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002010
Markus Armbruster39101f22015-02-12 16:46:36 +01002011 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
2012 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002013 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2014 if (ret < 0) {
2015 goto out;
2016 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002017 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002018
Kevin Wolfa18953f2010-10-14 15:46:04 +02002019 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002020 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002021 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002022 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002023 }
2024
Kevin Wolfefa84d42009-05-18 16:42:12 +02002025 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01002026 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002027 bool encryption =
2028 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2029 const char *preallocation =
2030 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002031
2032 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002033 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002034 ret = -1;
2035 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002036 }
2037
Chunyan Liu83d05212014-06-05 17:20:51 +08002038 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002039 error_report("Compression and encryption not supported at "
2040 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002041 ret = -1;
2042 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002043 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002044
Chunyan Liu83d05212014-06-05 17:20:51 +08002045 if (preallocation
2046 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002047 {
2048 error_report("Compression and preallocation not supported at "
2049 "the same time");
2050 ret = -1;
2051 goto out;
2052 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002053 }
2054
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002055 if (!skip_create) {
2056 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002057 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002058 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002059 error_reportf_err(local_err, "%s: error while converting %s: ",
2060 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002061 goto out;
bellardea2384d2004-08-01 21:59:26 +00002062 }
2063 }
ths3b46e622007-09-17 08:09:54 +00002064
Peter Lieven5a37b602013-10-24 12:07:06 +02002065 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002066 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002067 if (ret < 0) {
2068 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002069 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002070 }
2071
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002072 /* XXX we should allow --image-opts to trigger use of
2073 * img_open() here, but then we have trouble with
2074 * the bdrv_create() call which takes different params.
2075 * Not critical right now, so fix can wait...
2076 */
Kevin Wolfce099542016-03-15 13:01:04 +01002077 out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002078 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002079 ret = -1;
2080 goto out;
2081 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002082 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002083
Peter Lievenf2521c92013-11-27 11:07:06 +01002084 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
2085 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2086 * as maximum. */
2087 bufsectors = MIN(32768,
2088 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
2089 out_bs->bl.discard_alignment))
2090 );
2091
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002092 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002093 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002094 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002095 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002096 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002097 ret = -1;
2098 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002099 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002100 error_report("output file is smaller than input file");
2101 ret = -1;
2102 goto out;
2103 }
2104 }
2105
Peter Lieven24f833c2013-11-27 11:07:07 +01002106 cluster_sectors = 0;
2107 ret = bdrv_get_info(out_bs, &bdi);
2108 if (ret < 0) {
2109 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002110 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002111 goto out;
2112 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002113 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002114 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002115 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2116 }
2117
Kevin Wolf690c7302015-03-19 13:33:32 +01002118 state = (ImgConvertState) {
2119 .src = blk,
2120 .src_sectors = bs_sectors,
2121 .src_num = bs_n,
2122 .total_sectors = total_sectors,
2123 .target = out_blk,
2124 .compressed = compress,
2125 .target_has_backing = (bool) out_baseimg,
2126 .min_sparse = min_sparse,
2127 .cluster_sectors = cluster_sectors,
2128 .buf_sectors = bufsectors,
2129 };
2130 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002131
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002132out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002133 if (!ret) {
2134 qemu_progress_print(100, 0);
2135 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002136 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002137 qemu_opts_del(opts);
2138 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002139 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002140 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002141 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002142 if (blk) {
2143 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2144 blk_unref(blk[bs_i]);
2145 }
2146 g_free(blk);
2147 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002148 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002149fail_getopt:
2150 g_free(options);
2151
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002152 if (ret) {
2153 return 1;
2154 }
bellardea2384d2004-08-01 21:59:26 +00002155 return 0;
2156}
2157
bellard57d1a2b2004-08-03 21:15:11 +00002158
bellardfaea38e2006-08-05 21:31:00 +00002159static void dump_snapshots(BlockDriverState *bs)
2160{
2161 QEMUSnapshotInfo *sn_tab, *sn;
2162 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002163
2164 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2165 if (nb_sns <= 0)
2166 return;
2167 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002168 bdrv_snapshot_dump(fprintf, stdout, NULL);
2169 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002170 for(i = 0; i < nb_sns; i++) {
2171 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002172 bdrv_snapshot_dump(fprintf, stdout, sn);
2173 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002174 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002175 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002176}
2177
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002178static void dump_json_image_info_list(ImageInfoList *list)
2179{
Markus Armbruster4399c432014-04-25 16:50:32 +02002180 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002181 QString *str;
2182 QmpOutputVisitor *ov = qmp_output_visitor_new();
2183 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002184 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2185 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002186 obj = qmp_output_get_qobject(ov);
2187 str = qobject_to_json_pretty(obj);
2188 assert(str != NULL);
2189 printf("%s\n", qstring_get_str(str));
2190 qobject_decref(obj);
2191 qmp_output_visitor_cleanup(ov);
2192 QDECREF(str);
2193}
2194
Benoît Canetc054b3f2012-09-05 13:09:02 +02002195static void dump_json_image_info(ImageInfo *info)
2196{
Markus Armbruster4399c432014-04-25 16:50:32 +02002197 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002198 QString *str;
2199 QmpOutputVisitor *ov = qmp_output_visitor_new();
2200 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002201 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002202 obj = qmp_output_get_qobject(ov);
2203 str = qobject_to_json_pretty(obj);
2204 assert(str != NULL);
2205 printf("%s\n", qstring_get_str(str));
2206 qobject_decref(obj);
2207 qmp_output_visitor_cleanup(ov);
2208 QDECREF(str);
2209}
2210
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002211static void dump_human_image_info_list(ImageInfoList *list)
2212{
2213 ImageInfoList *elem;
2214 bool delim = false;
2215
2216 for (elem = list; elem; elem = elem->next) {
2217 if (delim) {
2218 printf("\n");
2219 }
2220 delim = true;
2221
Wenchao Xia5b917042013-05-25 11:09:45 +08002222 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002223 }
2224}
2225
2226static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2227{
2228 return strcmp(a, b) == 0;
2229}
2230
2231/**
2232 * Open an image file chain and return an ImageInfoList
2233 *
2234 * @filename: topmost image filename
2235 * @fmt: topmost image format (may be NULL to autodetect)
2236 * @chain: true - enumerate entire backing file chain
2237 * false - only topmost image file
2238 *
2239 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2240 * image file. If there was an error a message will have been printed to
2241 * stderr.
2242 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002243static ImageInfoList *collect_image_info_list(bool image_opts,
2244 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002245 const char *fmt,
2246 bool chain)
2247{
2248 ImageInfoList *head = NULL;
2249 ImageInfoList **last = &head;
2250 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002251 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002252
2253 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2254
2255 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002256 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002257 BlockDriverState *bs;
2258 ImageInfo *info;
2259 ImageInfoList *elem;
2260
2261 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2262 error_report("Backing file '%s' creates an infinite loop.",
2263 filename);
2264 goto err;
2265 }
2266 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2267
Max Reitzefaa7c42016-03-16 19:54:38 +01002268 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002269 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002270 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002271 goto err;
2272 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002273 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002274
Wenchao Xia43526ec2013-06-06 12:27:58 +08002275 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002276 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002277 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002278 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002279 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002280 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002281
2282 elem = g_new0(ImageInfoList, 1);
2283 elem->value = info;
2284 *last = elem;
2285 last = &elem->next;
2286
Markus Armbruster26f54e92014-10-07 13:59:04 +02002287 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002288
2289 filename = fmt = NULL;
2290 if (chain) {
2291 if (info->has_full_backing_filename) {
2292 filename = info->full_backing_filename;
2293 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002294 error_report("Could not determine absolute backing filename,"
2295 " but backing filename '%s' present",
2296 info->backing_filename);
2297 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002298 }
2299 if (info->has_backing_filename_format) {
2300 fmt = info->backing_filename_format;
2301 }
2302 }
2303 }
2304 g_hash_table_destroy(filenames);
2305 return head;
2306
2307err:
2308 qapi_free_ImageInfoList(head);
2309 g_hash_table_destroy(filenames);
2310 return NULL;
2311}
2312
Benoît Canetc054b3f2012-09-05 13:09:02 +02002313static int img_info(int argc, char **argv)
2314{
2315 int c;
2316 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002317 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002318 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002319 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002320 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002321
bellardea2384d2004-08-01 21:59:26 +00002322 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002323 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002324 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002325 int option_index = 0;
2326 static const struct option long_options[] = {
2327 {"help", no_argument, 0, 'h'},
2328 {"format", required_argument, 0, 'f'},
2329 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002330 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002331 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002332 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002333 {0, 0, 0, 0}
2334 };
2335 c = getopt_long(argc, argv, "f:h",
2336 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002337 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002338 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002339 }
bellardea2384d2004-08-01 21:59:26 +00002340 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002341 case '?':
bellardea2384d2004-08-01 21:59:26 +00002342 case 'h':
2343 help();
2344 break;
2345 case 'f':
2346 fmt = optarg;
2347 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002348 case OPTION_OUTPUT:
2349 output = optarg;
2350 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002351 case OPTION_BACKING_CHAIN:
2352 chain = true;
2353 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002354 case OPTION_OBJECT: {
2355 QemuOpts *opts;
2356 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2357 optarg, true);
2358 if (!opts) {
2359 return 1;
2360 }
2361 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002362 case OPTION_IMAGE_OPTS:
2363 image_opts = true;
2364 break;
bellardea2384d2004-08-01 21:59:26 +00002365 }
2366 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002367 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002368 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002369 }
bellardea2384d2004-08-01 21:59:26 +00002370 filename = argv[optind++];
2371
Benoît Canetc054b3f2012-09-05 13:09:02 +02002372 if (output && !strcmp(output, "json")) {
2373 output_format = OFORMAT_JSON;
2374 } else if (output && !strcmp(output, "human")) {
2375 output_format = OFORMAT_HUMAN;
2376 } else if (output) {
2377 error_report("--output must be used with human or json as argument.");
2378 return 1;
2379 }
2380
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002381 if (qemu_opts_foreach(&qemu_object_opts,
2382 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002383 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002384 return 1;
2385 }
2386
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002387 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002388 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002389 return 1;
2390 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002391
Benoît Canetc054b3f2012-09-05 13:09:02 +02002392 switch (output_format) {
2393 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002394 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002395 break;
2396 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002397 if (chain) {
2398 dump_json_image_info_list(list);
2399 } else {
2400 dump_json_image_info(list->value);
2401 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002402 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002403 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002404
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002405 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002406 return 0;
2407}
2408
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002409static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2410 MapEntry *next)
2411{
2412 switch (output_format) {
2413 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002414 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002415 error_report("File contains external, encrypted or compressed clusters.");
2416 exit(1);
2417 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002418 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002419 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002420 e->start, e->length,
2421 e->has_offset ? e->offset : 0,
2422 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002423 }
2424 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2425 * Modify the flags here to allow more coalescing.
2426 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002427 if (next && (!next->data || next->zero)) {
2428 next->data = false;
2429 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002430 }
2431 break;
2432 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002433 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2434 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002435 (e->start == 0 ? "[" : ",\n"),
2436 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002437 e->zero ? "true" : "false",
2438 e->data ? "true" : "false");
2439 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002440 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002441 }
2442 putchar('}');
2443
2444 if (!next) {
2445 printf("]\n");
2446 }
2447 break;
2448 }
2449}
2450
2451static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2452 int nb_sectors, MapEntry *e)
2453{
2454 int64_t ret;
2455 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002456 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002457 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002458
2459 /* As an optimization, we could cache the current range of unallocated
2460 * clusters in each file of the chain, and avoid querying the same
2461 * range repeatedly.
2462 */
2463
2464 depth = 0;
2465 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002466 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2467 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002468 if (ret < 0) {
2469 return ret;
2470 }
2471 assert(nb_sectors);
2472 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2473 break;
2474 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002475 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002476 if (bs == NULL) {
2477 ret = 0;
2478 break;
2479 }
2480
2481 depth++;
2482 }
2483
John Snow28756452016-02-05 13:12:33 -05002484 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2485
2486 *e = (MapEntry) {
2487 .start = sector_num * BDRV_SECTOR_SIZE,
2488 .length = nb_sectors * BDRV_SECTOR_SIZE,
2489 .data = !!(ret & BDRV_BLOCK_DATA),
2490 .zero = !!(ret & BDRV_BLOCK_ZERO),
2491 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2492 .has_offset = has_offset,
2493 .depth = depth,
2494 .has_filename = file && has_offset,
2495 .filename = file && has_offset ? file->filename : NULL,
2496 };
2497
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002498 return 0;
2499}
2500
Fam Zheng16b0d552016-01-26 11:59:02 +08002501static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2502{
2503 if (curr->length == 0) {
2504 return false;
2505 }
2506 if (curr->zero != next->zero ||
2507 curr->data != next->data ||
2508 curr->depth != next->depth ||
2509 curr->has_filename != next->has_filename ||
2510 curr->has_offset != next->has_offset) {
2511 return false;
2512 }
2513 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2514 return false;
2515 }
2516 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2517 return false;
2518 }
2519 return true;
2520}
2521
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002522static int img_map(int argc, char **argv)
2523{
2524 int c;
2525 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002526 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002527 BlockDriverState *bs;
2528 const char *filename, *fmt, *output;
2529 int64_t length;
2530 MapEntry curr = { .length = 0 }, next;
2531 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002532 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002533
2534 fmt = NULL;
2535 output = NULL;
2536 for (;;) {
2537 int option_index = 0;
2538 static const struct option long_options[] = {
2539 {"help", no_argument, 0, 'h'},
2540 {"format", required_argument, 0, 'f'},
2541 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002542 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002543 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002544 {0, 0, 0, 0}
2545 };
2546 c = getopt_long(argc, argv, "f:h",
2547 long_options, &option_index);
2548 if (c == -1) {
2549 break;
2550 }
2551 switch (c) {
2552 case '?':
2553 case 'h':
2554 help();
2555 break;
2556 case 'f':
2557 fmt = optarg;
2558 break;
2559 case OPTION_OUTPUT:
2560 output = optarg;
2561 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002562 case OPTION_OBJECT: {
2563 QemuOpts *opts;
2564 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2565 optarg, true);
2566 if (!opts) {
2567 return 1;
2568 }
2569 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002570 case OPTION_IMAGE_OPTS:
2571 image_opts = true;
2572 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002573 }
2574 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002575 if (optind != argc - 1) {
2576 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002577 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002578 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002579
2580 if (output && !strcmp(output, "json")) {
2581 output_format = OFORMAT_JSON;
2582 } else if (output && !strcmp(output, "human")) {
2583 output_format = OFORMAT_HUMAN;
2584 } else if (output) {
2585 error_report("--output must be used with human or json as argument.");
2586 return 1;
2587 }
2588
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002589 if (qemu_opts_foreach(&qemu_object_opts,
2590 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002591 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002592 return 1;
2593 }
2594
Kevin Wolfce099542016-03-15 13:01:04 +01002595 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002596 if (!blk) {
2597 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002598 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002599 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002600
2601 if (output_format == OFORMAT_HUMAN) {
2602 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2603 }
2604
Max Reitzf1d3cd72015-02-05 13:58:18 -05002605 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002606 while (curr.start + curr.length < length) {
2607 int64_t nsectors_left;
2608 int64_t sector_num;
2609 int n;
2610
2611 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2612
2613 /* Probe up to 1 GiB at a time. */
2614 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2615 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2616 ret = get_block_status(bs, sector_num, n, &next);
2617
2618 if (ret < 0) {
2619 error_report("Could not read file metadata: %s", strerror(-ret));
2620 goto out;
2621 }
2622
Fam Zheng16b0d552016-01-26 11:59:02 +08002623 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002624 curr.length += next.length;
2625 continue;
2626 }
2627
2628 if (curr.length > 0) {
2629 dump_map_entry(output_format, &curr, &next);
2630 }
2631 curr = next;
2632 }
2633
2634 dump_map_entry(output_format, &curr, NULL);
2635
2636out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002637 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002638 return ret < 0;
2639}
2640
aliguorif7b4a942009-01-07 17:40:15 +00002641#define SNAPSHOT_LIST 1
2642#define SNAPSHOT_CREATE 2
2643#define SNAPSHOT_APPLY 3
2644#define SNAPSHOT_DELETE 4
2645
Stuart Brady153859b2009-06-07 00:42:17 +01002646static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002647{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002648 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002649 BlockDriverState *bs;
2650 QEMUSnapshotInfo sn;
2651 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002652 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002653 int action = 0;
2654 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002655 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002656 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002657 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002658
Kevin Wolfce099542016-03-15 13:01:04 +01002659 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002660 /* Parse commandline parameters */
2661 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002662 static const struct option long_options[] = {
2663 {"help", no_argument, 0, 'h'},
2664 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002665 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002666 {0, 0, 0, 0}
2667 };
2668 c = getopt_long(argc, argv, "la:c:d:hq",
2669 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002670 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002671 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002672 }
aliguorif7b4a942009-01-07 17:40:15 +00002673 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002674 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002675 case 'h':
2676 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002677 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002678 case 'l':
2679 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002680 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002681 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002682 }
2683 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002684 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002685 break;
2686 case 'a':
2687 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002688 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002689 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002690 }
2691 action = SNAPSHOT_APPLY;
2692 snapshot_name = optarg;
2693 break;
2694 case 'c':
2695 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002696 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002697 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002698 }
2699 action = SNAPSHOT_CREATE;
2700 snapshot_name = optarg;
2701 break;
2702 case 'd':
2703 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002704 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002705 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002706 }
2707 action = SNAPSHOT_DELETE;
2708 snapshot_name = optarg;
2709 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002710 case 'q':
2711 quiet = true;
2712 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002713 case OPTION_OBJECT: {
2714 QemuOpts *opts;
2715 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2716 optarg, true);
2717 if (!opts) {
2718 return 1;
2719 }
2720 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002721 case OPTION_IMAGE_OPTS:
2722 image_opts = true;
2723 break;
aliguorif7b4a942009-01-07 17:40:15 +00002724 }
2725 }
2726
Kevin Wolffc11eb22013-08-05 10:53:04 +02002727 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002728 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002729 }
aliguorif7b4a942009-01-07 17:40:15 +00002730 filename = argv[optind++];
2731
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002732 if (qemu_opts_foreach(&qemu_object_opts,
2733 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002734 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002735 return 1;
2736 }
2737
aliguorif7b4a942009-01-07 17:40:15 +00002738 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002739 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002740 if (!blk) {
2741 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002742 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002743 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002744
2745 /* Perform the requested action */
2746 switch(action) {
2747 case SNAPSHOT_LIST:
2748 dump_snapshots(bs);
2749 break;
2750
2751 case SNAPSHOT_CREATE:
2752 memset(&sn, 0, sizeof(sn));
2753 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2754
2755 qemu_gettimeofday(&tv);
2756 sn.date_sec = tv.tv_sec;
2757 sn.date_nsec = tv.tv_usec * 1000;
2758
2759 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002760 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002761 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002762 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002763 }
aliguorif7b4a942009-01-07 17:40:15 +00002764 break;
2765
2766 case SNAPSHOT_APPLY:
2767 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002768 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002769 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002770 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002771 }
aliguorif7b4a942009-01-07 17:40:15 +00002772 break;
2773
2774 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002775 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002776 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002777 error_reportf_err(err, "Could not delete snapshot '%s': ",
2778 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002779 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002780 }
aliguorif7b4a942009-01-07 17:40:15 +00002781 break;
2782 }
2783
2784 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002785 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002786 if (ret) {
2787 return 1;
2788 }
Stuart Brady153859b2009-06-07 00:42:17 +01002789 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002790}
2791
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002792static int img_rebase(int argc, char **argv)
2793{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002794 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002795 uint8_t *buf_old = NULL;
2796 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002797 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002798 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002799 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2800 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002801 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002802 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002803 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002804 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002805 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002806 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002807
2808 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002809 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002810 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002811 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002812 out_baseimg = NULL;
2813 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002814 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002815 static const struct option long_options[] = {
2816 {"help", no_argument, 0, 'h'},
2817 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002818 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002819 {0, 0, 0, 0}
2820 };
2821 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2822 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002823 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002824 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002825 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002826 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002827 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002828 case 'h':
2829 help();
2830 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002831 case 'f':
2832 fmt = optarg;
2833 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002834 case 'F':
2835 out_basefmt = optarg;
2836 break;
2837 case 'b':
2838 out_baseimg = optarg;
2839 break;
2840 case 'u':
2841 unsafe = 1;
2842 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002843 case 'p':
2844 progress = 1;
2845 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002846 case 't':
2847 cache = optarg;
2848 break;
Max Reitz40055952014-07-22 22:58:42 +02002849 case 'T':
2850 src_cache = optarg;
2851 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002852 case 'q':
2853 quiet = true;
2854 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002855 case OPTION_OBJECT: {
2856 QemuOpts *opts;
2857 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2858 optarg, true);
2859 if (!opts) {
2860 return 1;
2861 }
2862 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002863 case OPTION_IMAGE_OPTS:
2864 image_opts = true;
2865 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002866 }
2867 }
2868
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002869 if (quiet) {
2870 progress = 0;
2871 }
2872
Fam Zhengac1307a2014-04-22 13:36:11 +08002873 if (optind != argc - 1) {
2874 error_exit("Expecting one image file name");
2875 }
2876 if (!unsafe && !out_baseimg) {
2877 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002878 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002879 filename = argv[optind++];
2880
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002881 if (qemu_opts_foreach(&qemu_object_opts,
2882 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002883 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002884 return 1;
2885 }
2886
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002887 qemu_progress_init(progress, 2.0);
2888 qemu_progress_print(0, 100);
2889
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002890 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01002891 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002892 if (ret < 0) {
2893 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002894 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002895 }
2896
Kevin Wolfce099542016-03-15 13:01:04 +01002897 src_flags = 0;
2898 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02002899 if (ret < 0) {
2900 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002901 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002902 }
2903
Kevin Wolfce099542016-03-15 13:01:04 +01002904 /* The source files are opened read-only, don't care about WCE */
2905 assert((src_flags & BDRV_O_RDWR) == 0);
2906 (void) src_writethrough;
2907
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002908 /*
2909 * Open the images.
2910 *
2911 * Ignore the old backing file for unsafe rebase in case we want to correct
2912 * the reference to a renamed or moved backing file.
2913 */
Kevin Wolfce099542016-03-15 13:01:04 +01002914 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002915 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002916 ret = -1;
2917 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002918 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002919 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002920
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002921 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002922 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002923 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002924 ret = -1;
2925 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002926 }
2927 }
2928
2929 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002930 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002931 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002932 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002933
Max Reitz644483d2015-02-05 13:58:17 -05002934 if (bs->backing_format[0] != '\0') {
2935 options = qdict_new();
2936 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2937 }
2938
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002939 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002940 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002941 options, src_flags, &local_err);
2942 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002943 error_reportf_err(local_err,
2944 "Could not open old backing file '%s': ",
2945 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002946 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002947 }
Max Reitz644483d2015-02-05 13:58:17 -05002948
Alex Bligha6166732012-10-16 13:46:18 +01002949 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002950 if (out_basefmt) {
2951 options = qdict_new();
2952 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2953 } else {
2954 options = NULL;
2955 }
2956
Max Reitzefaa7c42016-03-16 19:54:38 +01002957 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002958 options, src_flags, &local_err);
2959 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002960 error_reportf_err(local_err,
2961 "Could not open new backing file '%s': ",
2962 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002963 goto out;
2964 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002965 }
2966 }
2967
2968 /*
2969 * Check each unallocated cluster in the COW file. If it is unallocated,
2970 * accesses go to the backing file. We must therefore compare this cluster
2971 * in the old and new backing file, and if they differ we need to copy it
2972 * from the old backing file into the COW file.
2973 *
2974 * If qemu-img crashes during this step, no harm is done. The content of
2975 * the image is the same as the original one at any time.
2976 */
2977 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002978 int64_t num_sectors;
2979 int64_t old_backing_num_sectors;
2980 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002981 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002982 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002983 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002984
Max Reitzf1d3cd72015-02-05 13:58:18 -05002985 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2986 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002987
Max Reitzf1d3cd72015-02-05 13:58:18 -05002988 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002989 if (num_sectors < 0) {
2990 error_report("Could not get size of '%s': %s",
2991 filename, strerror(-num_sectors));
2992 ret = -1;
2993 goto out;
2994 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002995 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002996 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002997 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002998
2999 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3000 error_report("Could not get size of '%s': %s",
3001 backing_name, strerror(-old_backing_num_sectors));
3002 ret = -1;
3003 goto out;
3004 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003005 if (blk_new_backing) {
3006 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003007 if (new_backing_num_sectors < 0) {
3008 error_report("Could not get size of '%s': %s",
3009 out_baseimg, strerror(-new_backing_num_sectors));
3010 ret = -1;
3011 goto out;
3012 }
Alex Bligha6166732012-10-16 13:46:18 +01003013 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003014
Kevin Wolf1f710492012-10-12 14:29:18 +02003015 if (num_sectors != 0) {
3016 local_progress = (float)100 /
3017 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3018 }
3019
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003020 for (sector = 0; sector < num_sectors; sector += n) {
3021
3022 /* How many sectors can we handle with the next read? */
3023 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3024 n = (IO_BUF_SIZE / 512);
3025 } else {
3026 n = num_sectors - sector;
3027 }
3028
3029 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003030 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003031 if (ret < 0) {
3032 error_report("error while reading image metadata: %s",
3033 strerror(-ret));
3034 goto out;
3035 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003036 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003037 continue;
3038 }
3039
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003040 /*
3041 * Read old and new backing file and take into consideration that
3042 * backing files may be smaller than the COW image.
3043 */
3044 if (sector >= old_backing_num_sectors) {
3045 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3046 } else {
3047 if (sector + n > old_backing_num_sectors) {
3048 n = old_backing_num_sectors - sector;
3049 }
3050
Eric Blake91669202016-05-06 10:26:43 -06003051 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3052 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003053 if (ret < 0) {
3054 error_report("error while reading from old backing file");
3055 goto out;
3056 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003057 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003058
Max Reitzf1d3cd72015-02-05 13:58:18 -05003059 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003060 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3061 } else {
3062 if (sector + n > new_backing_num_sectors) {
3063 n = new_backing_num_sectors - sector;
3064 }
3065
Eric Blake91669202016-05-06 10:26:43 -06003066 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3067 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003068 if (ret < 0) {
3069 error_report("error while reading from new backing file");
3070 goto out;
3071 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003072 }
3073
3074 /* If they differ, we need to write to the COW file */
3075 uint64_t written = 0;
3076
3077 while (written < n) {
3078 int pnum;
3079
3080 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003081 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003082 {
Eric Blake91669202016-05-06 10:26:43 -06003083 ret = blk_pwrite(blk,
3084 (sector + written) << BDRV_SECTOR_BITS,
3085 buf_old + written * 512,
3086 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003087 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003088 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003089 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003090 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003091 }
3092 }
3093
3094 written += pnum;
3095 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003096 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003097 }
3098 }
3099
3100 /*
3101 * Change the backing file. All clusters that are different from the old
3102 * backing file are overwritten in the COW file now, so the visible content
3103 * doesn't change when we switch the backing file.
3104 */
Alex Bligha6166732012-10-16 13:46:18 +01003105 if (out_baseimg && *out_baseimg) {
3106 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3107 } else {
3108 ret = bdrv_change_backing_file(bs, NULL, NULL);
3109 }
3110
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003111 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003112 error_report("Could not change the backing file to '%s': No "
3113 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003114 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003115 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003116 out_baseimg, strerror(-ret));
3117 }
3118
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003119 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003120 /*
3121 * TODO At this point it is possible to check if any clusters that are
3122 * allocated in the COW file are the same in the backing file. If so, they
3123 * could be dropped from the COW file. Don't do this before switching the
3124 * backing file, in case of a crash this would lead to corruption.
3125 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003126out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003127 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003128 /* Cleanup */
3129 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003130 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003131 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003132 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003133 qemu_vfree(buf_old);
3134 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003135
Markus Armbruster26f54e92014-10-07 13:59:04 +02003136 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003137 if (ret) {
3138 return 1;
3139 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003140 return 0;
3141}
3142
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003143static int img_resize(int argc, char **argv)
3144{
Markus Armbruster6750e792015-02-12 17:43:08 +01003145 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003146 int c, ret, relative;
3147 const char *filename, *fmt, *size;
3148 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003149 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003150 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003151 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003152
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003153 static QemuOptsList resize_options = {
3154 .name = "resize_options",
3155 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3156 .desc = {
3157 {
3158 .name = BLOCK_OPT_SIZE,
3159 .type = QEMU_OPT_SIZE,
3160 .help = "Virtual disk size"
3161 }, {
3162 /* end of list */
3163 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003164 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003165 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003166 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003167
Kevin Wolfe80fec72011-04-29 10:58:12 +02003168 /* Remove size from argv manually so that negative numbers are not treated
3169 * as options by getopt. */
3170 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003171 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003172 return 1;
3173 }
3174
3175 size = argv[--argc];
3176
3177 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003178 fmt = NULL;
3179 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003180 static const struct option long_options[] = {
3181 {"help", no_argument, 0, 'h'},
3182 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003183 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003184 {0, 0, 0, 0}
3185 };
3186 c = getopt_long(argc, argv, "f:hq",
3187 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003188 if (c == -1) {
3189 break;
3190 }
3191 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003192 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003193 case 'h':
3194 help();
3195 break;
3196 case 'f':
3197 fmt = optarg;
3198 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003199 case 'q':
3200 quiet = true;
3201 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003202 case OPTION_OBJECT: {
3203 QemuOpts *opts;
3204 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3205 optarg, true);
3206 if (!opts) {
3207 return 1;
3208 }
3209 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003210 case OPTION_IMAGE_OPTS:
3211 image_opts = true;
3212 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003213 }
3214 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003215 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003216 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003217 }
3218 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003219
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003220 if (qemu_opts_foreach(&qemu_object_opts,
3221 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003222 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003223 return 1;
3224 }
3225
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003226 /* Choose grow, shrink, or absolute resize mode */
3227 switch (size[0]) {
3228 case '+':
3229 relative = 1;
3230 size++;
3231 break;
3232 case '-':
3233 relative = -1;
3234 size++;
3235 break;
3236 default:
3237 relative = 0;
3238 break;
3239 }
3240
3241 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003242 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003243 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003244 if (err) {
3245 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003246 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003247 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003248 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003249 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003250 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3251 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003252
Max Reitzefaa7c42016-03-16 19:54:38 +01003253 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01003254 BDRV_O_RDWR, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003255 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003256 ret = -1;
3257 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003258 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003259
3260 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003261 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003262 } else {
3263 total_size = n;
3264 }
3265 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003266 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003267 ret = -1;
3268 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003269 }
3270
Max Reitzf1d3cd72015-02-05 13:58:18 -05003271 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003272 switch (ret) {
3273 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003274 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003275 break;
3276 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003277 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003278 break;
3279 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003280 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003281 break;
3282 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003283 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003284 break;
3285 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003286out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003287 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003288 if (ret) {
3289 return 1;
3290 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003291 return 0;
3292}
3293
Max Reitz76a3a342014-10-27 11:12:51 +01003294static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003295 int64_t offset, int64_t total_work_size,
3296 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003297{
3298 qemu_progress_print(100.f * offset / total_work_size, 0);
3299}
3300
Max Reitz6f176b42013-09-03 10:09:50 +02003301static int img_amend(int argc, char **argv)
3302{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003303 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003304 int c, ret = 0;
3305 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003306 QemuOptsList *create_opts = NULL;
3307 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003308 const char *fmt = NULL, *filename, *cache;
3309 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003310 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003311 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003312 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003313 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003314 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003315
Max Reitzbd39e6e2014-07-22 22:58:43 +02003316 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003317 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003318 static const struct option long_options[] = {
3319 {"help", no_argument, 0, 'h'},
3320 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003321 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003322 {0, 0, 0, 0}
3323 };
3324 c = getopt_long(argc, argv, "ho:f:t:pq",
3325 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003326 if (c == -1) {
3327 break;
3328 }
3329
3330 switch (c) {
3331 case 'h':
3332 case '?':
3333 help();
3334 break;
3335 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003336 if (!is_valid_option_list(optarg)) {
3337 error_report("Invalid option list: %s", optarg);
3338 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003339 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003340 }
3341 if (!options) {
3342 options = g_strdup(optarg);
3343 } else {
3344 char *old_options = options;
3345 options = g_strdup_printf("%s,%s", options, optarg);
3346 g_free(old_options);
3347 }
Max Reitz6f176b42013-09-03 10:09:50 +02003348 break;
3349 case 'f':
3350 fmt = optarg;
3351 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003352 case 't':
3353 cache = optarg;
3354 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003355 case 'p':
3356 progress = true;
3357 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003358 case 'q':
3359 quiet = true;
3360 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003361 case OPTION_OBJECT:
3362 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3363 optarg, true);
3364 if (!opts) {
3365 ret = -1;
3366 goto out_no_progress;
3367 }
3368 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003369 case OPTION_IMAGE_OPTS:
3370 image_opts = true;
3371 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003372 }
3373 }
3374
Max Reitz6f176b42013-09-03 10:09:50 +02003375 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003376 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003377 }
3378
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003379 if (qemu_opts_foreach(&qemu_object_opts,
3380 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003381 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003382 ret = -1;
3383 goto out_no_progress;
3384 }
3385
Max Reitz76a3a342014-10-27 11:12:51 +01003386 if (quiet) {
3387 progress = false;
3388 }
3389 qemu_progress_init(progress, 1.0);
3390
Kevin Wolfa283cb62014-02-21 16:24:07 +01003391 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3392 if (fmt && has_help_option(options)) {
3393 /* If a format is explicitly specified (and possibly no filename is
3394 * given), print option help here */
3395 ret = print_block_option_help(filename, fmt);
3396 goto out;
3397 }
3398
3399 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003400 error_report("Expecting one image file name");
3401 ret = -1;
3402 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003403 }
Max Reitz6f176b42013-09-03 10:09:50 +02003404
Kevin Wolfce099542016-03-15 13:01:04 +01003405 flags = BDRV_O_RDWR;
3406 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003407 if (ret < 0) {
3408 error_report("Invalid cache option: %s", cache);
3409 goto out;
3410 }
3411
Kevin Wolfce099542016-03-15 13:01:04 +01003412 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003413 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003414 ret = -1;
3415 goto out;
3416 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003417 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003418
3419 fmt = bs->drv->format_name;
3420
Kevin Wolf626f84f2014-02-21 16:24:06 +01003421 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003422 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003423 ret = print_block_option_help(filename, fmt);
3424 goto out;
3425 }
3426
Max Reitzb2439d22014-12-02 18:32:47 +01003427 if (!bs->drv->create_opts) {
3428 error_report("Format driver '%s' does not support any options to amend",
3429 fmt);
3430 ret = -1;
3431 goto out;
3432 }
3433
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003434 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003435 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003436 if (options) {
3437 qemu_opts_do_parse(opts, options, NULL, &err);
3438 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003439 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003440 ret = -1;
3441 goto out;
3442 }
Max Reitz6f176b42013-09-03 10:09:50 +02003443 }
3444
Max Reitz76a3a342014-10-27 11:12:51 +01003445 /* In case the driver does not call amend_status_cb() */
3446 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003447 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003448 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003449 if (ret < 0) {
3450 error_report("Error while amending options: %s", strerror(-ret));
3451 goto out;
3452 }
3453
3454out:
Max Reitz76a3a342014-10-27 11:12:51 +01003455 qemu_progress_end();
3456
Max Reitze814dff2015-08-20 16:00:38 -07003457out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003458 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003459 qemu_opts_del(opts);
3460 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003461 g_free(options);
3462
Max Reitz6f176b42013-09-03 10:09:50 +02003463 if (ret) {
3464 return 1;
3465 }
3466 return 0;
3467}
3468
Kevin Wolfb6133b82014-08-05 14:17:13 +02003469typedef struct BenchData {
3470 BlockBackend *blk;
3471 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003472 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003473 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003474 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003475 int nrreq;
3476 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003477 int flush_interval;
3478 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003479 uint8_t *buf;
3480 QEMUIOVector *qiov;
3481
3482 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003483 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003484 uint64_t offset;
3485} BenchData;
3486
Kevin Wolf55d539c2016-06-03 13:59:41 +02003487static void bench_undrained_flush_cb(void *opaque, int ret)
3488{
3489 if (ret < 0) {
3490 error_report("Failed flush request: %s\n", strerror(-ret));
3491 exit(EXIT_FAILURE);
3492 }
3493}
3494
Kevin Wolfb6133b82014-08-05 14:17:13 +02003495static void bench_cb(void *opaque, int ret)
3496{
3497 BenchData *b = opaque;
3498 BlockAIOCB *acb;
3499
3500 if (ret < 0) {
3501 error_report("Failed request: %s\n", strerror(-ret));
3502 exit(EXIT_FAILURE);
3503 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003504
3505 if (b->in_flush) {
3506 /* Just finished a flush with drained queue: Start next requests */
3507 assert(b->in_flight == 0);
3508 b->in_flush = false;
3509 } else if (b->in_flight > 0) {
3510 int remaining = b->n - b->in_flight;
3511
Kevin Wolfb6133b82014-08-05 14:17:13 +02003512 b->n--;
3513 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003514
3515 /* Time for flush? Drain queue if requested, then flush */
3516 if (b->flush_interval && remaining % b->flush_interval == 0) {
3517 if (!b->in_flight || !b->drain_on_flush) {
3518 BlockCompletionFunc *cb;
3519
3520 if (b->drain_on_flush) {
3521 b->in_flush = true;
3522 cb = bench_cb;
3523 } else {
3524 cb = bench_undrained_flush_cb;
3525 }
3526
3527 acb = blk_aio_flush(b->blk, cb, b);
3528 if (!acb) {
3529 error_report("Failed to issue flush request");
3530 exit(EXIT_FAILURE);
3531 }
3532 }
3533 if (b->drain_on_flush) {
3534 return;
3535 }
3536 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003537 }
3538
3539 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003540 if (b->write) {
3541 acb = blk_aio_pwritev(b->blk, b->offset, b->qiov, 0,
3542 bench_cb, b);
3543 } else {
3544 acb = blk_aio_preadv(b->blk, b->offset, b->qiov, 0,
3545 bench_cb, b);
3546 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003547 if (!acb) {
3548 error_report("Failed to issue request");
3549 exit(EXIT_FAILURE);
3550 }
3551 b->in_flight++;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003552 b->offset += b->step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003553 b->offset %= b->image_size;
3554 }
3555}
3556
3557static int img_bench(int argc, char **argv)
3558{
3559 int c, ret = 0;
3560 const char *fmt = NULL, *filename;
3561 bool quiet = false;
3562 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003563 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003564 int count = 75000;
3565 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003566 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003567 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003568 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003569 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003570 int flush_interval = 0;
3571 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003572 int64_t image_size;
3573 BlockBackend *blk = NULL;
3574 BenchData data = {};
3575 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003576 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003577 struct timeval t1, t2;
3578 int i;
3579
3580 for (;;) {
3581 static const struct option long_options[] = {
3582 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003583 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003584 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003585 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003586 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003587 {0, 0, 0, 0}
3588 };
Kevin Wolf83de9be2015-07-13 13:13:17 +02003589 c = getopt_long(argc, argv, "hc:d:f:no:qs:S:t:w", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003590 if (c == -1) {
3591 break;
3592 }
3593
3594 switch (c) {
3595 case 'h':
3596 case '?':
3597 help();
3598 break;
3599 case 'c':
3600 {
3601 char *end;
3602 errno = 0;
3603 count = strtoul(optarg, &end, 0);
3604 if (errno || *end || count > INT_MAX) {
3605 error_report("Invalid request count specified");
3606 return 1;
3607 }
3608 break;
3609 }
3610 case 'd':
3611 {
3612 char *end;
3613 errno = 0;
3614 depth = strtoul(optarg, &end, 0);
3615 if (errno || *end || depth > INT_MAX) {
3616 error_report("Invalid queue depth specified");
3617 return 1;
3618 }
3619 break;
3620 }
3621 case 'f':
3622 fmt = optarg;
3623 break;
3624 case 'n':
3625 flags |= BDRV_O_NATIVE_AIO;
3626 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003627 case 'o':
3628 {
3629 char *end;
3630 errno = 0;
3631 offset = qemu_strtosz_suffix(optarg, &end,
3632 QEMU_STRTOSZ_DEFSUFFIX_B);
3633 if (offset < 0|| *end) {
3634 error_report("Invalid offset specified");
3635 return 1;
3636 }
3637 break;
3638 }
3639 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003640 case 'q':
3641 quiet = true;
3642 break;
3643 case 's':
3644 {
3645 int64_t sval;
3646 char *end;
3647
3648 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
3649 if (sval < 0 || sval > INT_MAX || *end) {
3650 error_report("Invalid buffer size specified");
3651 return 1;
3652 }
3653
3654 bufsize = sval;
3655 break;
3656 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003657 case 'S':
3658 {
3659 int64_t sval;
3660 char *end;
3661
3662 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
3663 if (sval < 0 || sval > INT_MAX || *end) {
3664 error_report("Invalid step size specified");
3665 return 1;
3666 }
3667
3668 step = sval;
3669 break;
3670 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003671 case 't':
3672 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3673 if (ret < 0) {
3674 error_report("Invalid cache mode");
3675 ret = -1;
3676 goto out;
3677 }
3678 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003679 case 'w':
3680 flags |= BDRV_O_RDWR;
3681 is_write = true;
3682 break;
3683 case OPTION_PATTERN:
3684 {
3685 char *end;
3686 errno = 0;
3687 pattern = strtoul(optarg, &end, 0);
3688 if (errno || *end || pattern > 0xff) {
3689 error_report("Invalid pattern byte specified");
3690 return 1;
3691 }
3692 break;
3693 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003694 case OPTION_FLUSH_INTERVAL:
3695 {
3696 char *end;
3697 errno = 0;
3698 flush_interval = strtoul(optarg, &end, 0);
3699 if (errno || *end || flush_interval > INT_MAX) {
3700 error_report("Invalid flush interval specified");
3701 return 1;
3702 }
3703 break;
3704 }
3705 case OPTION_NO_DRAIN:
3706 drain_on_flush = false;
3707 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003708 case OPTION_IMAGE_OPTS:
3709 image_opts = true;
3710 break;
3711 }
3712 }
3713
3714 if (optind != argc - 1) {
3715 error_exit("Expecting one image file name");
3716 }
3717 filename = argv[argc - 1];
3718
Kevin Wolf55d539c2016-06-03 13:59:41 +02003719 if (!is_write && flush_interval) {
3720 error_report("--flush-interval is only available in write tests");
3721 ret = -1;
3722 goto out;
3723 }
3724 if (flush_interval && flush_interval < depth) {
3725 error_report("Flush interval can't be smaller than depth");
3726 ret = -1;
3727 goto out;
3728 }
3729
Kevin Wolfb6133b82014-08-05 14:17:13 +02003730 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
3731 if (!blk) {
3732 ret = -1;
3733 goto out;
3734 }
3735
3736 image_size = blk_getlength(blk);
3737 if (image_size < 0) {
3738 ret = image_size;
3739 goto out;
3740 }
3741
3742 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003743 .blk = blk,
3744 .image_size = image_size,
3745 .bufsize = bufsize,
3746 .step = step ?: bufsize,
3747 .nrreq = depth,
3748 .n = count,
3749 .offset = offset,
3750 .write = is_write,
3751 .flush_interval = flush_interval,
3752 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02003753 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003754 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02003755 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02003756 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02003757 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02003758 if (flush_interval) {
3759 printf("Sending flush every %d requests\n", flush_interval);
3760 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003761
3762 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003763 memset(data.buf, pattern, data.nrreq * data.bufsize);
3764
Kevin Wolfb6133b82014-08-05 14:17:13 +02003765 data.qiov = g_new(QEMUIOVector, data.nrreq);
3766 for (i = 0; i < data.nrreq; i++) {
3767 qemu_iovec_init(&data.qiov[i], 1);
3768 qemu_iovec_add(&data.qiov[i],
3769 data.buf + i * data.bufsize, data.bufsize);
3770 }
3771
3772 gettimeofday(&t1, NULL);
3773 bench_cb(&data, 0);
3774
3775 while (data.n > 0) {
3776 main_loop_wait(false);
3777 }
3778 gettimeofday(&t2, NULL);
3779
3780 printf("Run completed in %3.3f seconds.\n",
3781 (t2.tv_sec - t1.tv_sec)
3782 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
3783
3784out:
3785 qemu_vfree(data.buf);
3786 blk_unref(blk);
3787
3788 if (ret) {
3789 return 1;
3790 }
3791 return 0;
3792}
3793
3794
Anthony Liguoric227f092009-10-01 16:12:16 -05003795static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003796#define DEF(option, callback, arg_string) \
3797 { option, callback },
3798#include "qemu-img-cmds.h"
3799#undef DEF
3800#undef GEN_DOCS
3801 { NULL, NULL, },
3802};
3803
bellardea2384d2004-08-01 21:59:26 +00003804int main(int argc, char **argv)
3805{
Anthony Liguoric227f092009-10-01 16:12:16 -05003806 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003807 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003808 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003809 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003810 static const struct option long_options[] = {
3811 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03003812 {"version", no_argument, 0, 'V'},
Jeff Cody7db16892014-04-25 17:02:32 -04003813 {0, 0, 0, 0}
3814 };
bellardea2384d2004-08-01 21:59:26 +00003815
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003816#ifdef CONFIG_POSIX
3817 signal(SIGPIPE, SIG_IGN);
3818#endif
3819
Kevin Wolf53f76e52010-12-16 15:10:32 +01003820 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003821 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003822
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003823 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003824 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003825 exit(EXIT_FAILURE);
3826 }
3827
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03003828 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01003829
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003830 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003831 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003832 if (argc < 2) {
3833 error_exit("Not enough arguments");
3834 }
Stuart Brady153859b2009-06-07 00:42:17 +01003835
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003836 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003837 qemu_add_opts(&qemu_source_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003838
Denis V. Lunev10985132016-06-17 17:44:13 +03003839 while ((c = getopt_long(argc, argv, "+hV", long_options, NULL)) != -1) {
3840 switch (c) {
3841 case 'h':
3842 help();
3843 return 0;
3844 case 'V':
3845 printf(QEMU_IMG_VERSION);
3846 return 0;
Stuart Brady153859b2009-06-07 00:42:17 +01003847 }
bellardea2384d2004-08-01 21:59:26 +00003848 }
Stuart Brady153859b2009-06-07 00:42:17 +01003849
Denis V. Lunev10985132016-06-17 17:44:13 +03003850 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04003851
Denis V. Lunev10985132016-06-17 17:44:13 +03003852 /* reset getopt_long scanning */
3853 argc -= optind;
3854 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04003855 return 0;
3856 }
Denis V. Lunev10985132016-06-17 17:44:13 +03003857 argv += optind;
3858 optind = 1;
3859
3860 /* find the command */
3861 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
3862 if (!strcmp(cmdname, cmd->name)) {
3863 return cmd->handler(argc, argv);
3864 }
3865 }
Jeff Cody7db16892014-04-25 17:02:32 -04003866
Stuart Brady153859b2009-06-07 00:42:17 +01003867 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003868 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003869}