blob: 29eae2a24b4e1ba6f5d8ee088e6570878ea711d3 [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"
Benoît Canetc054b3f2012-09-05 13:09:02 +020025#include "qapi-visit.h"
26#include "qapi/qmp-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010028#include "qapi/qmp/qjson.h"
pbrookfaf07962007-11-11 02:51:17 +000029#include "qemu-common.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000030#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/option.h"
32#include "qemu/error-report.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000033#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020035#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010036#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020037#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080038#include "block/qapi.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020039#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000040
Don Slutz61979a62015-01-09 10:17:35 -050041#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Jeff Cody5f6979c2014-04-28 14:37:18 -040042 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
43
Anthony Liguoric227f092009-10-01 16:12:16 -050044typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010045 const char *name;
46 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050047} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010048
Federico Simoncelli8599ea42013-01-28 06:59:47 -050049enum {
50 OPTION_OUTPUT = 256,
51 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000052 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000053 OPTION_IMAGE_OPTS = 259,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050054};
55
56typedef enum OutputFormat {
57 OFORMAT_JSON,
58 OFORMAT_HUMAN,
59} OutputFormat;
60
aurel32137519c2008-11-30 19:12:49 +000061/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010062#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040063#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000064
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010065static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000066{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010067 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000068}
69
Fam Zhengac1307a2014-04-22 13:36:11 +080070static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
71{
72 va_list ap;
73
74 error_printf("qemu-img: ");
75
76 va_start(ap, fmt);
77 error_vprintf(fmt, ap);
78 va_end(ap);
79
80 error_printf("\nTry 'qemu-img --help' for more information\n");
81 exit(EXIT_FAILURE);
82}
83
blueswir1d2c639d2009-01-24 18:19:25 +000084/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080085static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000086{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010087 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040088 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030089 "usage: qemu-img command [command options]\n"
90 "QEMU disk image utility\n"
91 "\n"
92 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010093#define DEF(option, callback, arg_string) \
94 " " arg_string "\n"
95#include "qemu-img-cmds.h"
96#undef DEF
97#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030098 "\n"
99 "Command parameters:\n"
100 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000101 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
102 " manual page for a description of the object properties. The most common\n"
103 " object type is a 'secret', which is used to supply passwords and/or\n"
104 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300105 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400106 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800107 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
108 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100109 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
110 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300111 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200112 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
113 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
114 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300115 " 'output_filename' is the destination disk image filename\n"
116 " 'output_fmt' is the destination format\n"
117 " 'options' is a comma separated list of format specific options in a\n"
118 " name=value format. Use -o ? for an overview of the options supported by the\n"
119 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800120 " 'snapshot_param' is param used for internal snapshot, format\n"
121 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
122 " '[ID_OR_NAME]'\n"
123 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
124 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300125 " '-c' indicates that target image must be compressed (qcow format only)\n"
126 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
127 " match exactly. The image doesn't need a working backing file before\n"
128 " rebasing in this case (useful for renaming the backing file)\n"
129 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200130 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100131 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200132 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
133 " contain only zeros for qemu-img to create a sparse image during\n"
134 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
135 " unallocated or zero sectors, and the destination image will always be\n"
136 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200137 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100138 " '-n' skips the target volume creation (useful if the volume is created\n"
139 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300140 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200141 "Parameters to check subcommand:\n"
142 " '-r' tries to repair any inconsistencies that are found during the check.\n"
143 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
144 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200145 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200146 "\n"
malc3f020d72010-02-08 12:04:56 +0300147 "Parameters to snapshot subcommand:\n"
148 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
149 " '-a' applies a snapshot (revert disk to saved state)\n"
150 " '-c' creates a snapshot\n"
151 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100152 " '-l' lists all snapshots in the given image\n"
153 "\n"
154 "Parameters to compare subcommand:\n"
155 " '-f' first image format\n"
156 " '-F' second image format\n"
157 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100158
159 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100160 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000161 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800162 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000163}
164
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000165static QemuOptsList qemu_object_opts = {
166 .name = "object",
167 .implied_opt_name = "qom-type",
168 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
169 .desc = {
170 { }
171 },
172};
173
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000174static QemuOptsList qemu_source_opts = {
175 .name = "source",
176 .implied_opt_name = "file",
177 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
178 .desc = {
179 { }
180 },
181};
182
Stefan Weil7c30f652013-06-16 17:01:05 +0200183static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100184{
185 int ret = 0;
186 if (!quiet) {
187 va_list args;
188 va_start(args, fmt);
189 ret = vprintf(fmt, args);
190 va_end(args);
191 }
192 return ret;
193}
194
bellardea2384d2004-08-01 21:59:26 +0000195
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100196static int print_block_option_help(const char *filename, const char *fmt)
197{
198 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800199 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500200 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100201
202 /* Find driver and parse its options */
203 drv = bdrv_find_format(fmt);
204 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100205 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100206 return 1;
207 }
208
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800209 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100210 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500211 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100212 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100213 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800214 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100215 return 1;
216 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800217 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100218 }
219
Chunyan Liu83d05212014-06-05 17:20:51 +0800220 qemu_opts_print_help(create_opts);
221 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100222 return 0;
223}
224
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000225
226static int img_open_password(BlockBackend *blk, const char *filename,
227 bool require_io, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000228{
229 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000230 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000231
232 bs = blk_bs(blk);
233 if (bdrv_is_encrypted(bs) && require_io) {
234 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
235 if (qemu_read_password(password, sizeof(password)) < 0) {
236 error_report("No password given");
237 return -1;
238 }
239 if (bdrv_set_key(bs, password) < 0) {
240 error_report("invalid password");
241 return -1;
242 }
243 }
244 return 0;
245}
246
247
Max Reitzefaa7c42016-03-16 19:54:38 +0100248static BlockBackend *img_open_opts(const char *optstr,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000249 QemuOpts *opts, int flags,
250 bool require_io, bool quiet)
251{
252 QDict *options;
253 Error *local_err = NULL;
254 BlockBackend *blk;
255 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100256 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000257 if (!blk) {
258 error_reportf_err(local_err, "Could not open '%s'", optstr);
259 return NULL;
260 }
261
262 if (img_open_password(blk, optstr, require_io, quiet) < 0) {
263 blk_unref(blk);
264 return NULL;
265 }
266 return blk;
267}
268
Max Reitzefaa7c42016-03-16 19:54:38 +0100269static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000270 const char *fmt, int flags,
271 bool require_io, bool quiet)
272{
273 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200274 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500275 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100276
bellard75c23802004-08-27 21:28:58 +0000277 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500278 options = qdict_new();
279 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000280 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100281
Max Reitzefaa7c42016-03-16 19:54:38 +0100282 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500283 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100284 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000285 return NULL;
bellard75c23802004-08-27 21:28:58 +0000286 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100287
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000288 if (img_open_password(blk, filename, require_io, quiet) < 0) {
289 blk_unref(blk);
290 return NULL;
bellard75c23802004-08-27 21:28:58 +0000291 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200292 return blk;
bellard75c23802004-08-27 21:28:58 +0000293}
294
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000295
Max Reitzefaa7c42016-03-16 19:54:38 +0100296static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000297 const char *filename,
298 const char *fmt, int flags,
299 bool require_io, bool quiet)
300{
301 BlockBackend *blk;
302 if (image_opts) {
303 QemuOpts *opts;
304 if (fmt) {
305 error_report("--image-opts and --format are mutually exclusive");
306 return NULL;
307 }
308 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
309 filename, true);
310 if (!opts) {
311 return NULL;
312 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100313 blk = img_open_opts(filename, opts, flags, true, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000314 } else {
Max Reitzefaa7c42016-03-16 19:54:38 +0100315 blk = img_open_file(filename, fmt, flags, true, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000316 }
317 return blk;
318}
319
320
Chunyan Liu83d05212014-06-05 17:20:51 +0800321static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100322 const char *base_filename,
323 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200324{
Markus Armbruster6750e792015-02-12 17:43:08 +0100325 Error *err = NULL;
326
Kevin Wolfefa84d42009-05-18 16:42:12 +0200327 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100328 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100329 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100330 error_report("Backing file not supported for file format '%s'",
331 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100332 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900333 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200334 }
335 }
336 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100337 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100338 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100339 error_report("Backing file format not supported for file "
340 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100341 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900342 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200343 }
344 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900345 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200346}
347
bellardea2384d2004-08-01 21:59:26 +0000348static int img_create(int argc, char **argv)
349{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200350 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100351 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000352 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000353 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000354 const char *filename;
355 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200356 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200357 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100358 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000359
bellardea2384d2004-08-01 21:59:26 +0000360 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000361 static const struct option long_options[] = {
362 {"help", no_argument, 0, 'h'},
363 {"object", required_argument, 0, OPTION_OBJECT},
364 {0, 0, 0, 0}
365 };
366 c = getopt_long(argc, argv, "F:b:f:he6o:q",
367 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100368 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000369 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100370 }
bellardea2384d2004-08-01 21:59:26 +0000371 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100372 case '?':
bellardea2384d2004-08-01 21:59:26 +0000373 case 'h':
374 help();
375 break;
aliguori9230eaf2009-03-28 17:55:19 +0000376 case 'F':
377 base_fmt = optarg;
378 break;
bellardea2384d2004-08-01 21:59:26 +0000379 case 'b':
380 base_filename = optarg;
381 break;
382 case 'f':
383 fmt = optarg;
384 break;
385 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200386 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100387 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100388 goto fail;
thsd8871c52007-10-24 16:11:42 +0000389 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200390 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100391 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100392 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200393 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100394 if (!is_valid_option_list(optarg)) {
395 error_report("Invalid option list: %s", optarg);
396 goto fail;
397 }
398 if (!options) {
399 options = g_strdup(optarg);
400 } else {
401 char *old_options = options;
402 options = g_strdup_printf("%s,%s", options, optarg);
403 g_free(old_options);
404 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200405 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100406 case 'q':
407 quiet = true;
408 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000409 case OPTION_OBJECT: {
410 QemuOpts *opts;
411 opts = qemu_opts_parse_noisily(&qemu_object_opts,
412 optarg, true);
413 if (!opts) {
414 goto fail;
415 }
416 } break;
bellardea2384d2004-08-01 21:59:26 +0000417 }
418 }
aliguori9230eaf2009-03-28 17:55:19 +0000419
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900420 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100421 filename = (optind < argc) ? argv[optind] : NULL;
422 if (options && has_help_option(options)) {
423 g_free(options);
424 return print_block_option_help(filename, fmt);
425 }
426
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100427 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800428 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100429 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100430 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900431
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000432 if (qemu_opts_foreach(&qemu_object_opts,
433 user_creatable_add_opts_foreach,
434 NULL, &local_err)) {
435 error_report_err(local_err);
436 goto fail;
437 }
438
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100439 /* Get image size, if specified */
440 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100441 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100442 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200443 sval = qemu_strtosz_suffix(argv[optind++], &end,
444 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100445 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800446 if (sval == -ERANGE) {
447 error_report("Image size must be less than 8 EiB!");
448 } else {
449 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200450 "G, T, P or E suffixes for ");
451 error_report("kilobytes, megabytes, gigabytes, terabytes, "
452 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800453 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100454 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100455 }
456 img_size = (uint64_t)sval;
457 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200458 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800459 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200460 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100461
Luiz Capitulino9b375252012-11-30 10:52:05 -0200462 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100463 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100464 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100465 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100466 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900467 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200468
Kevin Wolf77386bf2014-02-21 16:24:04 +0100469 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000470 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100471
472fail:
473 g_free(options);
474 return 1;
bellardea2384d2004-08-01 21:59:26 +0000475}
476
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100477static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500478{
Markus Armbruster4399c432014-04-25 16:50:32 +0200479 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500480 QString *str;
481 QmpOutputVisitor *ov = qmp_output_visitor_new();
482 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -0700483 visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
484 &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500485 obj = qmp_output_get_qobject(ov);
486 str = qobject_to_json_pretty(obj);
487 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100488 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500489 qobject_decref(obj);
490 qmp_output_visitor_cleanup(ov);
491 QDECREF(str);
492}
493
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100494static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500495{
496 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100497 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500498 } else {
499 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100500 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
501 "Data may be corrupted, or further writes to the image "
502 "may corrupt it.\n",
503 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500504 }
505
506 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100507 qprintf(quiet,
508 "\n%" PRId64 " leaked clusters were found on the image.\n"
509 "This means waste of disk space, but no harm to data.\n",
510 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500511 }
512
513 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100514 qprintf(quiet,
515 "\n%" PRId64
516 " internal errors have occurred during the check.\n",
517 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500518 }
519 }
520
521 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100522 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
523 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
524 check->allocated_clusters, check->total_clusters,
525 check->allocated_clusters * 100.0 / check->total_clusters,
526 check->fragmented_clusters * 100.0 / check->allocated_clusters,
527 check->compressed_clusters * 100.0 /
528 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500529 }
530
531 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100532 qprintf(quiet,
533 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500534 }
535}
536
537static int collect_image_check(BlockDriverState *bs,
538 ImageCheck *check,
539 const char *filename,
540 const char *fmt,
541 int fix)
542{
543 int ret;
544 BdrvCheckResult result;
545
546 ret = bdrv_check(bs, &result, fix);
547 if (ret < 0) {
548 return ret;
549 }
550
551 check->filename = g_strdup(filename);
552 check->format = g_strdup(bdrv_get_format_name(bs));
553 check->check_errors = result.check_errors;
554 check->corruptions = result.corruptions;
555 check->has_corruptions = result.corruptions != 0;
556 check->leaks = result.leaks;
557 check->has_leaks = result.leaks != 0;
558 check->corruptions_fixed = result.corruptions_fixed;
559 check->has_corruptions_fixed = result.corruptions != 0;
560 check->leaks_fixed = result.leaks_fixed;
561 check->has_leaks_fixed = result.leaks != 0;
562 check->image_end_offset = result.image_end_offset;
563 check->has_image_end_offset = result.image_end_offset != 0;
564 check->total_clusters = result.bfi.total_clusters;
565 check->has_total_clusters = result.bfi.total_clusters != 0;
566 check->allocated_clusters = result.bfi.allocated_clusters;
567 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
568 check->fragmented_clusters = result.bfi.fragmented_clusters;
569 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100570 check->compressed_clusters = result.bfi.compressed_clusters;
571 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500572
573 return 0;
574}
575
Kevin Wolfe076f332010-06-29 11:43:13 +0200576/*
577 * Checks an image for consistency. Exit codes:
578 *
Max Reitzd6635c42014-06-02 22:15:21 +0200579 * 0 - Check completed, image is good
580 * 1 - Check not completed because of internal errors
581 * 2 - Check completed, image is corrupted
582 * 3 - Check completed, image has leaked clusters, but is good otherwise
583 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200584 */
aliguori15859692009-04-21 23:11:53 +0000585static int img_check(int argc, char **argv)
586{
587 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500588 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200589 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200590 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000591 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200592 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100593 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200594 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100595 bool quiet = false;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000596 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000597 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000598
599 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500600 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200601 cache = BDRV_DEFAULT_CACHE;
aliguori15859692009-04-21 23:11:53 +0000602 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500603 int option_index = 0;
604 static const struct option long_options[] = {
605 {"help", no_argument, 0, 'h'},
606 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530607 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500608 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000609 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000610 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500611 {0, 0, 0, 0}
612 };
Max Reitz40055952014-07-22 22:58:42 +0200613 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500614 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100615 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000616 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100617 }
aliguori15859692009-04-21 23:11:53 +0000618 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100619 case '?':
aliguori15859692009-04-21 23:11:53 +0000620 case 'h':
621 help();
622 break;
623 case 'f':
624 fmt = optarg;
625 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200626 case 'r':
627 flags |= BDRV_O_RDWR;
628
629 if (!strcmp(optarg, "leaks")) {
630 fix = BDRV_FIX_LEAKS;
631 } else if (!strcmp(optarg, "all")) {
632 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
633 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800634 error_exit("Unknown option value for -r "
635 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200636 }
637 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500638 case OPTION_OUTPUT:
639 output = optarg;
640 break;
Max Reitz40055952014-07-22 22:58:42 +0200641 case 'T':
642 cache = optarg;
643 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100644 case 'q':
645 quiet = true;
646 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000647 case OPTION_OBJECT: {
648 QemuOpts *opts;
649 opts = qemu_opts_parse_noisily(&qemu_object_opts,
650 optarg, true);
651 if (!opts) {
652 return 1;
653 }
654 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000655 case OPTION_IMAGE_OPTS:
656 image_opts = true;
657 break;
aliguori15859692009-04-21 23:11:53 +0000658 }
659 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200660 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800661 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100662 }
aliguori15859692009-04-21 23:11:53 +0000663 filename = argv[optind++];
664
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500665 if (output && !strcmp(output, "json")) {
666 output_format = OFORMAT_JSON;
667 } else if (output && !strcmp(output, "human")) {
668 output_format = OFORMAT_HUMAN;
669 } else if (output) {
670 error_report("--output must be used with human or json as argument.");
671 return 1;
672 }
673
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000674 if (qemu_opts_foreach(&qemu_object_opts,
675 user_creatable_add_opts_foreach,
676 NULL, &local_err)) {
677 error_report_err(local_err);
678 return 1;
679 }
680
Max Reitz40055952014-07-22 22:58:42 +0200681 ret = bdrv_parse_cache_flags(cache, &flags);
682 if (ret < 0) {
683 error_report("Invalid source cache option: %s", cache);
684 return 1;
685 }
686
Max Reitzefaa7c42016-03-16 19:54:38 +0100687 blk = img_open(image_opts, filename, fmt, flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200688 if (!blk) {
689 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900690 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200691 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500692
693 check = g_new0(ImageCheck, 1);
694 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200695
696 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200697 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200698 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500699 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200700 }
701
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500702 if (check->corruptions_fixed || check->leaks_fixed) {
703 int corruptions_fixed, leaks_fixed;
704
705 leaks_fixed = check->leaks_fixed;
706 corruptions_fixed = check->corruptions_fixed;
707
708 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100709 qprintf(quiet,
710 "The following inconsistencies were found and repaired:\n\n"
711 " %" PRId64 " leaked clusters\n"
712 " %" PRId64 " corruptions\n\n"
713 "Double checking the fixed image now...\n",
714 check->leaks_fixed,
715 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500716 }
717
718 ret = collect_image_check(bs, check, filename, fmt, 0);
719
720 check->leaks_fixed = leaks_fixed;
721 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200722 }
723
Max Reitz832390a2014-10-23 15:29:12 +0200724 if (!ret) {
725 switch (output_format) {
726 case OFORMAT_HUMAN:
727 dump_human_image_check(check, quiet);
728 break;
729 case OFORMAT_JSON:
730 dump_json_image_check(check, quiet);
731 break;
732 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500733 }
734
735 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200736 if (ret) {
737 error_report("Check failed: %s", strerror(-ret));
738 } else {
739 error_report("Check failed");
740 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500741 ret = 1;
742 goto fail;
743 }
744
745 if (check->corruptions) {
746 ret = 2;
747 } else if (check->leaks) {
748 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200749 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500750 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000751 }
752
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500753fail:
754 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200755 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500756 return ret;
aliguori15859692009-04-21 23:11:53 +0000757}
758
Max Reitzd4a32382014-10-24 15:57:37 +0200759typedef struct CommonBlockJobCBInfo {
760 BlockDriverState *bs;
761 Error **errp;
762} CommonBlockJobCBInfo;
763
764static void common_block_job_cb(void *opaque, int ret)
765{
766 CommonBlockJobCBInfo *cbi = opaque;
767
768 if (ret < 0) {
769 error_setg_errno(cbi->errp, -ret, "Block job failed");
770 }
Max Reitzd4a32382014-10-24 15:57:37 +0200771}
772
773static void run_block_job(BlockJob *job, Error **errp)
774{
775 AioContext *aio_context = bdrv_get_aio_context(job->bs);
776
777 do {
778 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500779 qemu_progress_print(job->len ?
780 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200781 } while (!job->ready);
782
783 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200784
785 /* A block job may finish instantaneously without publishing any progress,
786 * so just signal completion here */
787 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200788}
789
bellardea2384d2004-08-01 21:59:26 +0000790static int img_commit(int argc, char **argv)
791{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400792 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200793 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200794 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200795 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200796 bool progress = false, quiet = false, drop = false;
Max Reitzd4a32382014-10-24 15:57:37 +0200797 Error *local_err = NULL;
798 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000799 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000800
801 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400802 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200803 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000804 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000805 static const struct option long_options[] = {
806 {"help", no_argument, 0, 'h'},
807 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000808 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000809 {0, 0, 0, 0}
810 };
811 c = getopt_long(argc, argv, "f:ht:b:dpq",
812 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100813 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000814 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100815 }
bellardea2384d2004-08-01 21:59:26 +0000816 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100817 case '?':
bellardea2384d2004-08-01 21:59:26 +0000818 case 'h':
819 help();
820 break;
821 case 'f':
822 fmt = optarg;
823 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400824 case 't':
825 cache = optarg;
826 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200827 case 'b':
828 base = optarg;
829 /* -b implies -d */
830 drop = true;
831 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200832 case 'd':
833 drop = true;
834 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200835 case 'p':
836 progress = true;
837 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100838 case 'q':
839 quiet = true;
840 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000841 case OPTION_OBJECT: {
842 QemuOpts *opts;
843 opts = qemu_opts_parse_noisily(&qemu_object_opts,
844 optarg, true);
845 if (!opts) {
846 return 1;
847 }
848 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000849 case OPTION_IMAGE_OPTS:
850 image_opts = true;
851 break;
bellardea2384d2004-08-01 21:59:26 +0000852 }
853 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200854
855 /* Progress is not shown in Quiet mode */
856 if (quiet) {
857 progress = false;
858 }
859
Kevin Wolffc11eb22013-08-05 10:53:04 +0200860 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800861 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100862 }
bellardea2384d2004-08-01 21:59:26 +0000863 filename = argv[optind++];
864
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000865 if (qemu_opts_foreach(&qemu_object_opts,
866 user_creatable_add_opts_foreach,
867 NULL, &local_err)) {
868 error_report_err(local_err);
869 return 1;
870 }
871
Max Reitz9a86fe42014-10-24 15:57:38 +0200872 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100873 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400874 if (ret < 0) {
875 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100876 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400877 }
878
Max Reitzefaa7c42016-03-16 19:54:38 +0100879 blk = img_open(image_opts, filename, fmt, flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200880 if (!blk) {
881 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900882 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200883 bs = blk_bs(blk);
884
Max Reitz687fa1d2014-10-24 15:57:39 +0200885 qemu_progress_init(progress, 1.f);
886 qemu_progress_print(0.f, 100);
887
Max Reitz1b22bff2014-10-24 15:57:40 +0200888 if (base) {
889 base_bs = bdrv_find_backing_image(bs, base);
890 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100891 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200892 goto done;
893 }
894 } else {
895 /* This is different from QMP, which by default uses the deepest file in
896 * the backing chain (i.e., the very base); however, the traditional
897 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200898 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200899 if (!base_bs) {
900 error_setg(&local_err, "Image does not have a backing file");
901 goto done;
902 }
bellardea2384d2004-08-01 21:59:26 +0000903 }
904
Max Reitzd4a32382014-10-24 15:57:37 +0200905 cbi = (CommonBlockJobCBInfo){
906 .errp = &local_err,
907 .bs = bs,
908 };
909
910 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
911 common_block_job_cb, &cbi, &local_err);
912 if (local_err) {
913 goto done;
914 }
915
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200916 /* When the block job completes, the BlockBackend reference will point to
917 * the old backing file. In order to avoid that the top image is already
918 * deleted, so we can still empty it afterwards, increment the reference
919 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200920 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200921 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200922 }
923
Max Reitzd4a32382014-10-24 15:57:37 +0200924 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200925 if (local_err) {
926 goto unref_backing;
927 }
928
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200929 if (!drop && bs->drv->bdrv_make_empty) {
930 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200931 if (ret) {
932 error_setg_errno(&local_err, -ret, "Could not empty %s",
933 filename);
934 goto unref_backing;
935 }
936 }
937
938unref_backing:
939 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200940 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200941 }
Max Reitzd4a32382014-10-24 15:57:37 +0200942
943done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200944 qemu_progress_end();
945
Markus Armbruster26f54e92014-10-07 13:59:04 +0200946 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200947
948 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100949 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900950 return 1;
951 }
Max Reitzd4a32382014-10-24 15:57:37 +0200952
953 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000954 return 0;
955}
956
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400957/*
thsf58c7b32008-06-05 21:53:49 +0000958 * Returns true iff the first sector pointed to by 'buf' contains at least
959 * a non-NUL byte.
960 *
961 * 'pnum' is set to the number of sectors (including and immediately following
962 * the first one) that are known to be in the same allocated/unallocated state.
963 */
bellardea2384d2004-08-01 21:59:26 +0000964static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
965{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000966 bool is_zero;
967 int i;
bellardea2384d2004-08-01 21:59:26 +0000968
969 if (n <= 0) {
970 *pnum = 0;
971 return 0;
972 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000973 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000974 for(i = 1; i < n; i++) {
975 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000976 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000977 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000978 }
bellardea2384d2004-08-01 21:59:26 +0000979 }
980 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000981 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000982}
983
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100984/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200985 * Like is_allocated_sectors, but if the buffer starts with a used sector,
986 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
987 * breaking up write requests for only small sparse areas.
988 */
989static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
990 int min)
991{
992 int ret;
993 int num_checked, num_used;
994
995 if (n < min) {
996 min = n;
997 }
998
999 ret = is_allocated_sectors(buf, n, pnum);
1000 if (!ret) {
1001 return ret;
1002 }
1003
1004 num_used = *pnum;
1005 buf += BDRV_SECTOR_SIZE * *pnum;
1006 n -= *pnum;
1007 num_checked = num_used;
1008
1009 while (n > 0) {
1010 ret = is_allocated_sectors(buf, n, pnum);
1011
1012 buf += BDRV_SECTOR_SIZE * *pnum;
1013 n -= *pnum;
1014 num_checked += *pnum;
1015 if (ret) {
1016 num_used = num_checked;
1017 } else if (*pnum >= min) {
1018 break;
1019 }
1020 }
1021
1022 *pnum = num_used;
1023 return 1;
1024}
1025
1026/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001027 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1028 * buffers matches, non-zero otherwise.
1029 *
1030 * pnum is set to the number of sectors (including and immediately following
1031 * the first one) that are known to have the same comparison result
1032 */
1033static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1034 int *pnum)
1035{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001036 bool res;
1037 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001038
1039 if (n <= 0) {
1040 *pnum = 0;
1041 return 0;
1042 }
1043
1044 res = !!memcmp(buf1, buf2, 512);
1045 for(i = 1; i < n; i++) {
1046 buf1 += 512;
1047 buf2 += 512;
1048
1049 if (!!memcmp(buf1, buf2, 512) != res) {
1050 break;
1051 }
1052 }
1053
1054 *pnum = i;
1055 return res;
1056}
1057
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001058#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001059
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001060static int64_t sectors_to_bytes(int64_t sectors)
1061{
1062 return sectors << BDRV_SECTOR_BITS;
1063}
1064
1065static int64_t sectors_to_process(int64_t total, int64_t from)
1066{
1067 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1068}
1069
1070/*
1071 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1072 *
1073 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1074 * data and negative value on error.
1075 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001076 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001077 * @param sect_num: Number of first sector to check
1078 * @param sect_count: Number of sectors to check
1079 * @param filename: Name of disk file we are checking (logging purpose)
1080 * @param buffer: Allocated buffer for storing read data
1081 * @param quiet: Flag for quiet mode
1082 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001083static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001084 int sect_count, const char *filename,
1085 uint8_t *buffer, bool quiet)
1086{
1087 int pnum, ret = 0;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001088 ret = blk_read(blk, sect_num, buffer, sect_count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001089 if (ret < 0) {
1090 error_report("Error while reading offset %" PRId64 " of %s: %s",
1091 sectors_to_bytes(sect_num), filename, strerror(-ret));
1092 return ret;
1093 }
1094 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1095 if (ret || pnum != sect_count) {
1096 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1097 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1098 return 1;
1099 }
1100
1101 return 0;
1102}
1103
1104/*
1105 * Compares two images. Exit codes:
1106 *
1107 * 0 - Images are identical
1108 * 1 - Images differ
1109 * >1 - Error occurred
1110 */
1111static int img_compare(int argc, char **argv)
1112{
Max Reitz40055952014-07-22 22:58:42 +02001113 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001114 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001115 BlockDriverState *bs1, *bs2;
1116 int64_t total_sectors1, total_sectors2;
1117 uint8_t *buf1 = NULL, *buf2 = NULL;
1118 int pnum1, pnum2;
1119 int allocated1, allocated2;
1120 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1121 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001122 int flags;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001123 int64_t total_sectors;
1124 int64_t sector_num = 0;
1125 int64_t nb_sectors;
1126 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001127 uint64_t progress_base;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001128 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001129 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001130
Max Reitz40055952014-07-22 22:58:42 +02001131 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001132 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001133 static const struct option long_options[] = {
1134 {"help", no_argument, 0, 'h'},
1135 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001136 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001137 {0, 0, 0, 0}
1138 };
1139 c = getopt_long(argc, argv, "hf:F:T:pqs",
1140 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001141 if (c == -1) {
1142 break;
1143 }
1144 switch (c) {
1145 case '?':
1146 case 'h':
1147 help();
1148 break;
1149 case 'f':
1150 fmt1 = optarg;
1151 break;
1152 case 'F':
1153 fmt2 = optarg;
1154 break;
Max Reitz40055952014-07-22 22:58:42 +02001155 case 'T':
1156 cache = optarg;
1157 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001158 case 'p':
1159 progress = true;
1160 break;
1161 case 'q':
1162 quiet = true;
1163 break;
1164 case 's':
1165 strict = true;
1166 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001167 case OPTION_OBJECT: {
1168 QemuOpts *opts;
1169 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1170 optarg, true);
1171 if (!opts) {
1172 ret = 2;
1173 goto out4;
1174 }
1175 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001176 case OPTION_IMAGE_OPTS:
1177 image_opts = true;
1178 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001179 }
1180 }
1181
1182 /* Progress is not shown in Quiet mode */
1183 if (quiet) {
1184 progress = false;
1185 }
1186
1187
Kevin Wolffc11eb22013-08-05 10:53:04 +02001188 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001189 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001190 }
1191 filename1 = argv[optind++];
1192 filename2 = argv[optind++];
1193
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001194 if (qemu_opts_foreach(&qemu_object_opts,
1195 user_creatable_add_opts_foreach,
1196 NULL, &local_err)) {
1197 error_report_err(local_err);
1198 ret = 2;
1199 goto out4;
1200 }
1201
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001202 /* Initialize before goto out */
1203 qemu_progress_init(progress, 2.0);
1204
Max Reitz40055952014-07-22 22:58:42 +02001205 flags = BDRV_O_FLAGS;
1206 ret = bdrv_parse_cache_flags(cache, &flags);
1207 if (ret < 0) {
1208 error_report("Invalid source cache option: %s", cache);
1209 ret = 2;
1210 goto out3;
1211 }
1212
Max Reitzefaa7c42016-03-16 19:54:38 +01001213 blk1 = img_open(image_opts, filename1, fmt1, flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001214 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001215 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001216 goto out3;
1217 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001218
Max Reitzefaa7c42016-03-16 19:54:38 +01001219 blk2 = img_open(image_opts, filename2, fmt2, flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001220 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001221 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001222 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001223 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001224 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001225 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001226
Max Reitzf1d3cd72015-02-05 13:58:18 -05001227 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1228 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1229 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001230 if (total_sectors1 < 0) {
1231 error_report("Can't get size of %s: %s",
1232 filename1, strerror(-total_sectors1));
1233 ret = 4;
1234 goto out;
1235 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001236 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001237 if (total_sectors2 < 0) {
1238 error_report("Can't get size of %s: %s",
1239 filename2, strerror(-total_sectors2));
1240 ret = 4;
1241 goto out;
1242 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001243 total_sectors = MIN(total_sectors1, total_sectors2);
1244 progress_base = MAX(total_sectors1, total_sectors2);
1245
1246 qemu_progress_print(0, 100);
1247
1248 if (strict && total_sectors1 != total_sectors2) {
1249 ret = 1;
1250 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1251 goto out;
1252 }
1253
1254 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001255 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001256 BlockDriverState *file;
1257
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001258 nb_sectors = sectors_to_process(total_sectors, sector_num);
1259 if (nb_sectors <= 0) {
1260 break;
1261 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001262 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1263 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001264 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001265 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001266 ret = 3;
1267 error_report("Sector allocation test failed for %s", filename1);
1268 goto out;
1269 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001270 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001271
Fam Zheng25ad8e62016-01-13 16:37:41 +08001272 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1273 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001274 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001275 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001276 ret = 3;
1277 error_report("Sector allocation test failed for %s", filename2);
1278 goto out;
1279 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001280 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1281 if (pnum1) {
1282 nb_sectors = MIN(nb_sectors, pnum1);
1283 }
1284 if (pnum2) {
1285 nb_sectors = MIN(nb_sectors, pnum2);
1286 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001287
Fam Zheng25ad8e62016-01-13 16:37:41 +08001288 if (strict) {
1289 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1290 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1291 ret = 1;
1292 qprintf(quiet, "Strict mode: Offset %" PRId64
1293 " block status mismatch!\n",
1294 sectors_to_bytes(sector_num));
1295 goto out;
1296 }
1297 }
1298 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1299 nb_sectors = MIN(pnum1, pnum2);
1300 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001301 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001302 ret = blk_read(blk1, sector_num, buf1, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001303 if (ret < 0) {
1304 error_report("Error while reading offset %" PRId64 " of %s:"
1305 " %s", sectors_to_bytes(sector_num), filename1,
1306 strerror(-ret));
1307 ret = 4;
1308 goto out;
1309 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001310 ret = blk_read(blk2, sector_num, buf2, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001311 if (ret < 0) {
1312 error_report("Error while reading offset %" PRId64
1313 " of %s: %s", sectors_to_bytes(sector_num),
1314 filename2, strerror(-ret));
1315 ret = 4;
1316 goto out;
1317 }
1318 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1319 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001320 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1321 sectors_to_bytes(
1322 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001323 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001324 goto out;
1325 }
1326 }
1327 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001328
1329 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001330 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001331 filename1, buf1, quiet);
1332 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001333 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001334 filename2, buf1, quiet);
1335 }
1336 if (ret) {
1337 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001338 error_report("Error while reading offset %" PRId64 ": %s",
1339 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001340 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001341 }
1342 goto out;
1343 }
1344 }
1345 sector_num += nb_sectors;
1346 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1347 }
1348
1349 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001350 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001351 int64_t total_sectors_over;
1352 const char *filename_over;
1353
1354 qprintf(quiet, "Warning: Image size mismatch!\n");
1355 if (total_sectors1 > total_sectors2) {
1356 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001357 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001358 filename_over = filename1;
1359 } else {
1360 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001361 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001362 filename_over = filename2;
1363 }
1364
1365 for (;;) {
1366 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1367 if (nb_sectors <= 0) {
1368 break;
1369 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001370 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001371 nb_sectors, &pnum);
1372 if (ret < 0) {
1373 ret = 3;
1374 error_report("Sector allocation test failed for %s",
1375 filename_over);
1376 goto out;
1377
1378 }
1379 nb_sectors = pnum;
1380 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001381 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001382 filename_over, buf1, quiet);
1383 if (ret) {
1384 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001385 error_report("Error while reading offset %" PRId64
1386 " of %s: %s", sectors_to_bytes(sector_num),
1387 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001388 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001389 }
1390 goto out;
1391 }
1392 }
1393 sector_num += nb_sectors;
1394 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1395 }
1396 }
1397
1398 qprintf(quiet, "Images are identical.\n");
1399 ret = 0;
1400
1401out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001402 qemu_vfree(buf1);
1403 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001404 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001405out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001406 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001407out3:
1408 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001409out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001410 return ret;
1411}
1412
Kevin Wolf690c7302015-03-19 13:33:32 +01001413enum ImgConvertBlockStatus {
1414 BLK_DATA,
1415 BLK_ZERO,
1416 BLK_BACKING_FILE,
1417};
1418
1419typedef struct ImgConvertState {
1420 BlockBackend **src;
1421 int64_t *src_sectors;
1422 int src_cur, src_num;
1423 int64_t src_cur_offset;
1424 int64_t total_sectors;
1425 int64_t allocated_sectors;
1426 enum ImgConvertBlockStatus status;
1427 int64_t sector_next_status;
1428 BlockBackend *target;
1429 bool has_zero_init;
1430 bool compressed;
1431 bool target_has_backing;
1432 int min_sparse;
1433 size_t cluster_sectors;
1434 size_t buf_sectors;
1435} ImgConvertState;
1436
1437static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1438{
1439 assert(sector_num >= s->src_cur_offset);
1440 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1441 s->src_cur_offset += s->src_sectors[s->src_cur];
1442 s->src_cur++;
1443 assert(s->src_cur < s->src_num);
1444 }
1445}
1446
1447static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1448{
1449 int64_t ret;
1450 int n;
1451
1452 convert_select_part(s, sector_num);
1453
1454 assert(s->total_sectors > sector_num);
1455 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1456
1457 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001458 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001459 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1460 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001461 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001462 if (ret < 0) {
1463 return ret;
1464 }
1465
1466 if (ret & BDRV_BLOCK_ZERO) {
1467 s->status = BLK_ZERO;
1468 } else if (ret & BDRV_BLOCK_DATA) {
1469 s->status = BLK_DATA;
1470 } else if (!s->target_has_backing) {
1471 /* Without a target backing file we must copy over the contents of
1472 * the backing file as well. */
1473 /* TODO Check block status of the backing file chain to avoid
1474 * needlessly reading zeroes and limiting the iteration to the
1475 * buffer size */
1476 s->status = BLK_DATA;
1477 } else {
1478 s->status = BLK_BACKING_FILE;
1479 }
1480
1481 s->sector_next_status = sector_num + n;
1482 }
1483
1484 n = MIN(n, s->sector_next_status - sector_num);
1485 if (s->status == BLK_DATA) {
1486 n = MIN(n, s->buf_sectors);
1487 }
1488
1489 /* We need to write complete clusters for compressed images, so if an
1490 * unallocated area is shorter than that, we must consider the whole
1491 * cluster allocated. */
1492 if (s->compressed) {
1493 if (n < s->cluster_sectors) {
1494 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1495 s->status = BLK_DATA;
1496 } else {
1497 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1498 }
1499 }
1500
1501 return n;
1502}
1503
1504static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1505 uint8_t *buf)
1506{
1507 int n;
1508 int ret;
1509
1510 if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) {
1511 return 0;
1512 }
1513
1514 assert(nb_sectors <= s->buf_sectors);
1515 while (nb_sectors > 0) {
1516 BlockBackend *blk;
1517 int64_t bs_sectors;
1518
1519 /* In the case of compression with multiple source files, we can get a
1520 * nb_sectors that spreads into the next part. So we must be able to
1521 * read across multiple BDSes for one convert_read() call. */
1522 convert_select_part(s, sector_num);
1523 blk = s->src[s->src_cur];
1524 bs_sectors = s->src_sectors[s->src_cur];
1525
1526 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
1527 ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n);
1528 if (ret < 0) {
1529 return ret;
1530 }
1531
1532 sector_num += n;
1533 nb_sectors -= n;
1534 buf += n * BDRV_SECTOR_SIZE;
1535 }
1536
1537 return 0;
1538}
1539
1540static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1541 const uint8_t *buf)
1542{
1543 int ret;
1544
1545 while (nb_sectors > 0) {
1546 int n = nb_sectors;
1547
1548 switch (s->status) {
1549 case BLK_BACKING_FILE:
1550 /* If we have a backing file, leave clusters unallocated that are
1551 * unallocated in the source image, so that the backing file is
1552 * visible at the respective offset. */
1553 assert(s->target_has_backing);
1554 break;
1555
1556 case BLK_DATA:
1557 /* We must always write compressed clusters as a whole, so don't
1558 * try to find zeroed parts in the buffer. We can only save the
1559 * write if the buffer is completely zeroed and we're allowed to
1560 * keep the target sparse. */
1561 if (s->compressed) {
1562 if (s->has_zero_init && s->min_sparse &&
1563 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1564 {
1565 assert(!s->target_has_backing);
1566 break;
1567 }
1568
1569 ret = blk_write_compressed(s->target, sector_num, buf, n);
1570 if (ret < 0) {
1571 return ret;
1572 }
1573 break;
1574 }
1575
1576 /* If there is real non-zero data or we're told to keep the target
1577 * fully allocated (-S 0), we must write it. Otherwise we can treat
1578 * it as zero sectors. */
1579 if (!s->min_sparse ||
1580 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1581 {
1582 ret = blk_write(s->target, sector_num, buf, n);
1583 if (ret < 0) {
1584 return ret;
1585 }
1586 break;
1587 }
1588 /* fall-through */
1589
1590 case BLK_ZERO:
1591 if (s->has_zero_init) {
1592 break;
1593 }
1594 ret = blk_write_zeroes(s->target, sector_num, n, 0);
1595 if (ret < 0) {
1596 return ret;
1597 }
1598 break;
1599 }
1600
1601 sector_num += n;
1602 nb_sectors -= n;
1603 buf += n * BDRV_SECTOR_SIZE;
1604 }
1605
1606 return 0;
1607}
1608
1609static int convert_do_copy(ImgConvertState *s)
1610{
1611 uint8_t *buf = NULL;
1612 int64_t sector_num, allocated_done;
1613 int ret;
1614 int n;
1615
1616 /* Check whether we have zero initialisation or can get it efficiently */
1617 s->has_zero_init = s->min_sparse && !s->target_has_backing
1618 ? bdrv_has_zero_init(blk_bs(s->target))
1619 : false;
1620
1621 if (!s->has_zero_init && !s->target_has_backing &&
1622 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1623 {
1624 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1625 if (ret == 0) {
1626 s->has_zero_init = true;
1627 }
1628 }
1629
1630 /* Allocate buffer for copied data. For compressed images, only one cluster
1631 * can be copied at a time. */
1632 if (s->compressed) {
1633 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1634 error_report("invalid cluster size");
1635 ret = -EINVAL;
1636 goto fail;
1637 }
1638 s->buf_sectors = s->cluster_sectors;
1639 }
1640 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1641
1642 /* Calculate allocated sectors for progress */
1643 s->allocated_sectors = 0;
1644 sector_num = 0;
1645 while (sector_num < s->total_sectors) {
1646 n = convert_iteration_sectors(s, sector_num);
1647 if (n < 0) {
1648 ret = n;
1649 goto fail;
1650 }
1651 if (s->status == BLK_DATA) {
1652 s->allocated_sectors += n;
1653 }
1654 sector_num += n;
1655 }
1656
1657 /* Do the copy */
1658 s->src_cur = 0;
1659 s->src_cur_offset = 0;
1660 s->sector_next_status = 0;
1661
1662 sector_num = 0;
1663 allocated_done = 0;
1664
1665 while (sector_num < s->total_sectors) {
1666 n = convert_iteration_sectors(s, sector_num);
1667 if (n < 0) {
1668 ret = n;
1669 goto fail;
1670 }
1671 if (s->status == BLK_DATA) {
1672 allocated_done += n;
1673 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1674 0);
1675 }
1676
1677 ret = convert_read(s, sector_num, n, buf);
1678 if (ret < 0) {
1679 error_report("error while reading sector %" PRId64
1680 ": %s", sector_num, strerror(-ret));
1681 goto fail;
1682 }
1683
1684 ret = convert_write(s, sector_num, n, buf);
1685 if (ret < 0) {
1686 error_report("error while writing sector %" PRId64
1687 ": %s", sector_num, strerror(-ret));
1688 goto fail;
1689 }
1690
1691 sector_num += n;
1692 }
1693
1694 if (s->compressed) {
1695 /* signal EOF to align */
1696 ret = blk_write_compressed(s->target, 0, NULL, 0);
1697 if (ret < 0) {
1698 goto fail;
1699 }
1700 }
1701
1702 ret = 0;
1703fail:
1704 qemu_vfree(buf);
1705 return ret;
1706}
1707
bellardea2384d2004-08-01 21:59:26 +00001708static int img_convert(int argc, char **argv)
1709{
Kevin Wolf690c7302015-03-19 13:33:32 +01001710 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001711 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001712 int progress = 0, flags, src_flags;
1713 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001714 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001715 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001716 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001717 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001718 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001719 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001720 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001721 QemuOpts *opts = NULL;
1722 QemuOptsList *create_opts = NULL;
1723 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001724 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001725 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001726 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001727 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001728 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001729 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001730 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001731 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001732
1733 fmt = NULL;
1734 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001735 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001736 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001737 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001738 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001739 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001740 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001741 static const struct option long_options[] = {
1742 {"help", no_argument, 0, 'h'},
1743 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001744 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001745 {0, 0, 0, 0}
1746 };
1747 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1748 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001749 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001750 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001751 }
bellardea2384d2004-08-01 21:59:26 +00001752 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001753 case '?':
bellardea2384d2004-08-01 21:59:26 +00001754 case 'h':
1755 help();
1756 break;
1757 case 'f':
1758 fmt = optarg;
1759 break;
1760 case 'O':
1761 out_fmt = optarg;
1762 break;
thsf58c7b32008-06-05 21:53:49 +00001763 case 'B':
1764 out_baseimg = optarg;
1765 break;
bellardea2384d2004-08-01 21:59:26 +00001766 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001767 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001768 break;
1769 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001770 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001771 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001772 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001773 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001774 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001775 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001776 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001777 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001778 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001779 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001780 if (!is_valid_option_list(optarg)) {
1781 error_report("Invalid option list: %s", optarg);
1782 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001783 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001784 }
1785 if (!options) {
1786 options = g_strdup(optarg);
1787 } else {
1788 char *old_options = options;
1789 options = g_strdup_printf("%s,%s", options, optarg);
1790 g_free(old_options);
1791 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001792 break;
edison51ef6722010-09-21 19:58:41 -07001793 case 's':
1794 snapshot_name = optarg;
1795 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001796 case 'l':
1797 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001798 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1799 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001800 if (!sn_opts) {
1801 error_report("Failed in parsing snapshot param '%s'",
1802 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001803 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001804 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001805 }
1806 } else {
1807 snapshot_name = optarg;
1808 }
1809 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001810 case 'S':
1811 {
1812 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001813 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001814 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001815 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001816 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001817 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001818 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001819 }
1820
1821 min_sparse = sval / BDRV_SECTOR_SIZE;
1822 break;
1823 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001824 case 'p':
1825 progress = 1;
1826 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001827 case 't':
1828 cache = optarg;
1829 break;
Max Reitz40055952014-07-22 22:58:42 +02001830 case 'T':
1831 src_cache = optarg;
1832 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001833 case 'q':
1834 quiet = true;
1835 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001836 case 'n':
1837 skip_create = 1;
1838 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001839 case OPTION_OBJECT:
1840 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1841 optarg, true);
1842 if (!opts) {
1843 goto fail_getopt;
1844 }
1845 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001846 case OPTION_IMAGE_OPTS:
1847 image_opts = true;
1848 break;
bellardea2384d2004-08-01 21:59:26 +00001849 }
1850 }
ths3b46e622007-09-17 08:09:54 +00001851
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001852 if (qemu_opts_foreach(&qemu_object_opts,
1853 user_creatable_add_opts_foreach,
1854 NULL, &local_err)) {
1855 error_report_err(local_err);
1856 goto fail_getopt;
1857 }
1858
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001859 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001860 if (quiet) {
1861 progress = 0;
1862 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001863 qemu_progress_init(progress, 1.0);
1864
balrog926c2d22007-10-31 01:11:44 +00001865 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001866 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001867
Kevin Wolf2dc83282014-02-21 16:24:05 +01001868 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001869 ret = print_block_option_help(out_filename, out_fmt);
1870 goto out;
1871 }
1872
bellardea2384d2004-08-01 21:59:26 +00001873 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001874 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001875 }
bellardea2384d2004-08-01 21:59:26 +00001876
thsf58c7b32008-06-05 21:53:49 +00001877
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001878 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001879 error_report("-B makes no sense when concatenating multiple input "
1880 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001881 ret = -1;
1882 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001883 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001884
Max Reitz40055952014-07-22 22:58:42 +02001885 src_flags = BDRV_O_FLAGS;
1886 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
1887 if (ret < 0) {
1888 error_report("Invalid source cache option: %s", src_cache);
1889 goto out;
1890 }
1891
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001892 qemu_progress_print(0, 100);
1893
Markus Armbruster26f54e92014-10-07 13:59:04 +02001894 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001895 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001896 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001897
1898 total_sectors = 0;
1899 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001900 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001901 fmt, src_flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001902 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001903 ret = -1;
1904 goto out;
1905 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001906 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001907 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001908 if (bs_sectors[bs_i] < 0) {
1909 error_report("Could not get size of %s: %s",
1910 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1911 ret = -1;
1912 goto out;
1913 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001914 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001915 }
bellardea2384d2004-08-01 21:59:26 +00001916
Wenchao Xiaef806542013-12-04 17:10:57 +08001917 if (sn_opts) {
1918 ret = bdrv_snapshot_load_tmp(bs[0],
1919 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1920 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1921 &local_err);
1922 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001923 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001924 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001925 ret = -1;
1926 goto out;
1927 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001928
1929 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001930 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001931 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001932 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001933 ret = -1;
1934 goto out;
edison51ef6722010-09-21 19:58:41 -07001935 }
1936
Kevin Wolfefa84d42009-05-18 16:42:12 +02001937 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001938 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001939 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001940 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001941 ret = -1;
1942 goto out;
1943 }
balrog926c2d22007-10-31 01:11:44 +00001944
Max Reitzb65a5e12015-02-05 13:58:12 -05001945 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001946 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001947 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001948 ret = -1;
1949 goto out;
1950 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001951
Max Reitz2e024cd2015-02-11 09:58:46 -05001952 if (!skip_create) {
1953 if (!drv->create_opts) {
1954 error_report("Format driver '%s' does not support image creation",
1955 drv->format_name);
1956 ret = -1;
1957 goto out;
1958 }
Max Reitzf75613c2014-12-02 18:32:46 +01001959
Max Reitz2e024cd2015-02-11 09:58:46 -05001960 if (!proto_drv->create_opts) {
1961 error_report("Protocol driver '%s' does not support image creation",
1962 proto_drv->format_name);
1963 ret = -1;
1964 goto out;
1965 }
Max Reitzf75613c2014-12-02 18:32:46 +01001966
Max Reitz2e024cd2015-02-11 09:58:46 -05001967 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1968 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001969
Max Reitz2e024cd2015-02-11 09:58:46 -05001970 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001971 if (options) {
1972 qemu_opts_do_parse(opts, options, NULL, &local_err);
1973 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01001974 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001975 ret = -1;
1976 goto out;
1977 }
Max Reitz2e024cd2015-02-11 09:58:46 -05001978 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001979
Markus Armbruster39101f22015-02-12 16:46:36 +01001980 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
1981 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05001982 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
1983 if (ret < 0) {
1984 goto out;
1985 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001986 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001987
Kevin Wolfa18953f2010-10-14 15:46:04 +02001988 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001989 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001990 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001991 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001992 }
1993
Kevin Wolfefa84d42009-05-18 16:42:12 +02001994 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001995 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001996 bool encryption =
1997 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
1998 const char *preallocation =
1999 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002000
2001 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002002 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002003 ret = -1;
2004 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002005 }
2006
Chunyan Liu83d05212014-06-05 17:20:51 +08002007 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002008 error_report("Compression and encryption not supported at "
2009 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002010 ret = -1;
2011 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002012 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002013
Chunyan Liu83d05212014-06-05 17:20:51 +08002014 if (preallocation
2015 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002016 {
2017 error_report("Compression and preallocation not supported at "
2018 "the same time");
2019 ret = -1;
2020 goto out;
2021 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002022 }
2023
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002024 if (!skip_create) {
2025 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002026 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002027 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002028 error_reportf_err(local_err, "%s: error while converting %s: ",
2029 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002030 goto out;
bellardea2384d2004-08-01 21:59:26 +00002031 }
2032 }
ths3b46e622007-09-17 08:09:54 +00002033
Peter Lieven5a37b602013-10-24 12:07:06 +02002034 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002035 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002036 if (ret < 0) {
2037 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002038 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002039 }
2040
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002041 /* XXX we should allow --image-opts to trigger use of
2042 * img_open() here, but then we have trouble with
2043 * the bdrv_create() call which takes different params.
2044 * Not critical right now, so fix can wait...
2045 */
Max Reitzefaa7c42016-03-16 19:54:38 +01002046 out_blk = img_open_file(out_filename, out_fmt, flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002047 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002048 ret = -1;
2049 goto out;
2050 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002051 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002052
Peter Lievenf2521c92013-11-27 11:07:06 +01002053 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
2054 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2055 * as maximum. */
2056 bufsectors = MIN(32768,
2057 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
2058 out_bs->bl.discard_alignment))
2059 );
2060
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002061 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002062 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002063 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002064 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002065 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002066 ret = -1;
2067 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002068 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002069 error_report("output file is smaller than input file");
2070 ret = -1;
2071 goto out;
2072 }
2073 }
2074
Peter Lieven24f833c2013-11-27 11:07:07 +01002075 cluster_sectors = 0;
2076 ret = bdrv_get_info(out_bs, &bdi);
2077 if (ret < 0) {
2078 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002079 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002080 goto out;
2081 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002082 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002083 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002084 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2085 }
2086
Kevin Wolf690c7302015-03-19 13:33:32 +01002087 state = (ImgConvertState) {
2088 .src = blk,
2089 .src_sectors = bs_sectors,
2090 .src_num = bs_n,
2091 .total_sectors = total_sectors,
2092 .target = out_blk,
2093 .compressed = compress,
2094 .target_has_backing = (bool) out_baseimg,
2095 .min_sparse = min_sparse,
2096 .cluster_sectors = cluster_sectors,
2097 .buf_sectors = bufsectors,
2098 };
2099 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002100
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002101out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002102 if (!ret) {
2103 qemu_progress_print(100, 0);
2104 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002105 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002106 qemu_opts_del(opts);
2107 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002108 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002109 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002110 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002111 if (blk) {
2112 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2113 blk_unref(blk[bs_i]);
2114 }
2115 g_free(blk);
2116 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002117 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002118fail_getopt:
2119 g_free(options);
2120
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002121 if (ret) {
2122 return 1;
2123 }
bellardea2384d2004-08-01 21:59:26 +00002124 return 0;
2125}
2126
bellard57d1a2b2004-08-03 21:15:11 +00002127
bellardfaea38e2006-08-05 21:31:00 +00002128static void dump_snapshots(BlockDriverState *bs)
2129{
2130 QEMUSnapshotInfo *sn_tab, *sn;
2131 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002132
2133 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2134 if (nb_sns <= 0)
2135 return;
2136 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002137 bdrv_snapshot_dump(fprintf, stdout, NULL);
2138 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002139 for(i = 0; i < nb_sns; i++) {
2140 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002141 bdrv_snapshot_dump(fprintf, stdout, sn);
2142 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002143 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002144 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002145}
2146
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002147static void dump_json_image_info_list(ImageInfoList *list)
2148{
Markus Armbruster4399c432014-04-25 16:50:32 +02002149 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002150 QString *str;
2151 QmpOutputVisitor *ov = qmp_output_visitor_new();
2152 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002153 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2154 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002155 obj = qmp_output_get_qobject(ov);
2156 str = qobject_to_json_pretty(obj);
2157 assert(str != NULL);
2158 printf("%s\n", qstring_get_str(str));
2159 qobject_decref(obj);
2160 qmp_output_visitor_cleanup(ov);
2161 QDECREF(str);
2162}
2163
Benoît Canetc054b3f2012-09-05 13:09:02 +02002164static void dump_json_image_info(ImageInfo *info)
2165{
Markus Armbruster4399c432014-04-25 16:50:32 +02002166 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002167 QString *str;
2168 QmpOutputVisitor *ov = qmp_output_visitor_new();
2169 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002170 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002171 obj = qmp_output_get_qobject(ov);
2172 str = qobject_to_json_pretty(obj);
2173 assert(str != NULL);
2174 printf("%s\n", qstring_get_str(str));
2175 qobject_decref(obj);
2176 qmp_output_visitor_cleanup(ov);
2177 QDECREF(str);
2178}
2179
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002180static void dump_human_image_info_list(ImageInfoList *list)
2181{
2182 ImageInfoList *elem;
2183 bool delim = false;
2184
2185 for (elem = list; elem; elem = elem->next) {
2186 if (delim) {
2187 printf("\n");
2188 }
2189 delim = true;
2190
Wenchao Xia5b917042013-05-25 11:09:45 +08002191 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002192 }
2193}
2194
2195static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2196{
2197 return strcmp(a, b) == 0;
2198}
2199
2200/**
2201 * Open an image file chain and return an ImageInfoList
2202 *
2203 * @filename: topmost image filename
2204 * @fmt: topmost image format (may be NULL to autodetect)
2205 * @chain: true - enumerate entire backing file chain
2206 * false - only topmost image file
2207 *
2208 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2209 * image file. If there was an error a message will have been printed to
2210 * stderr.
2211 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002212static ImageInfoList *collect_image_info_list(bool image_opts,
2213 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002214 const char *fmt,
2215 bool chain)
2216{
2217 ImageInfoList *head = NULL;
2218 ImageInfoList **last = &head;
2219 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002220 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002221
2222 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2223
2224 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002225 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002226 BlockDriverState *bs;
2227 ImageInfo *info;
2228 ImageInfoList *elem;
2229
2230 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2231 error_report("Backing file '%s' creates an infinite loop.",
2232 filename);
2233 goto err;
2234 }
2235 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2236
Max Reitzefaa7c42016-03-16 19:54:38 +01002237 blk = img_open(image_opts, filename, fmt,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002238 BDRV_O_FLAGS | BDRV_O_NO_BACKING,
2239 false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002240 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002241 goto err;
2242 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002243 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002244
Wenchao Xia43526ec2013-06-06 12:27:58 +08002245 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002246 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002247 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002248 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002249 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002250 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002251
2252 elem = g_new0(ImageInfoList, 1);
2253 elem->value = info;
2254 *last = elem;
2255 last = &elem->next;
2256
Markus Armbruster26f54e92014-10-07 13:59:04 +02002257 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002258
2259 filename = fmt = NULL;
2260 if (chain) {
2261 if (info->has_full_backing_filename) {
2262 filename = info->full_backing_filename;
2263 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002264 error_report("Could not determine absolute backing filename,"
2265 " but backing filename '%s' present",
2266 info->backing_filename);
2267 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002268 }
2269 if (info->has_backing_filename_format) {
2270 fmt = info->backing_filename_format;
2271 }
2272 }
2273 }
2274 g_hash_table_destroy(filenames);
2275 return head;
2276
2277err:
2278 qapi_free_ImageInfoList(head);
2279 g_hash_table_destroy(filenames);
2280 return NULL;
2281}
2282
Benoît Canetc054b3f2012-09-05 13:09:02 +02002283static int img_info(int argc, char **argv)
2284{
2285 int c;
2286 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002287 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002288 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002289 ImageInfoList *list;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002290 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002291 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002292
bellardea2384d2004-08-01 21:59:26 +00002293 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002294 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002295 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002296 int option_index = 0;
2297 static const struct option long_options[] = {
2298 {"help", no_argument, 0, 'h'},
2299 {"format", required_argument, 0, 'f'},
2300 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002301 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002302 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002303 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002304 {0, 0, 0, 0}
2305 };
2306 c = getopt_long(argc, argv, "f:h",
2307 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002308 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002309 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002310 }
bellardea2384d2004-08-01 21:59:26 +00002311 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002312 case '?':
bellardea2384d2004-08-01 21:59:26 +00002313 case 'h':
2314 help();
2315 break;
2316 case 'f':
2317 fmt = optarg;
2318 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002319 case OPTION_OUTPUT:
2320 output = optarg;
2321 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002322 case OPTION_BACKING_CHAIN:
2323 chain = true;
2324 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002325 case OPTION_OBJECT: {
2326 QemuOpts *opts;
2327 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2328 optarg, true);
2329 if (!opts) {
2330 return 1;
2331 }
2332 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002333 case OPTION_IMAGE_OPTS:
2334 image_opts = true;
2335 break;
bellardea2384d2004-08-01 21:59:26 +00002336 }
2337 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002338 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002339 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002340 }
bellardea2384d2004-08-01 21:59:26 +00002341 filename = argv[optind++];
2342
Benoît Canetc054b3f2012-09-05 13:09:02 +02002343 if (output && !strcmp(output, "json")) {
2344 output_format = OFORMAT_JSON;
2345 } else if (output && !strcmp(output, "human")) {
2346 output_format = OFORMAT_HUMAN;
2347 } else if (output) {
2348 error_report("--output must be used with human or json as argument.");
2349 return 1;
2350 }
2351
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002352 if (qemu_opts_foreach(&qemu_object_opts,
2353 user_creatable_add_opts_foreach,
2354 NULL, &local_err)) {
2355 error_report_err(local_err);
2356 return 1;
2357 }
2358
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002359 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002360 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002361 return 1;
2362 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002363
Benoît Canetc054b3f2012-09-05 13:09:02 +02002364 switch (output_format) {
2365 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002366 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002367 break;
2368 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002369 if (chain) {
2370 dump_json_image_info_list(list);
2371 } else {
2372 dump_json_image_info(list->value);
2373 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002374 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002375 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002376
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002377 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002378 return 0;
2379}
2380
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002381static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2382 MapEntry *next)
2383{
2384 switch (output_format) {
2385 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002386 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002387 error_report("File contains external, encrypted or compressed clusters.");
2388 exit(1);
2389 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002390 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002391 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002392 e->start, e->length,
2393 e->has_offset ? e->offset : 0,
2394 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002395 }
2396 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2397 * Modify the flags here to allow more coalescing.
2398 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002399 if (next && (!next->data || next->zero)) {
2400 next->data = false;
2401 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002402 }
2403 break;
2404 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002405 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2406 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002407 (e->start == 0 ? "[" : ",\n"),
2408 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002409 e->zero ? "true" : "false",
2410 e->data ? "true" : "false");
2411 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002412 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002413 }
2414 putchar('}');
2415
2416 if (!next) {
2417 printf("]\n");
2418 }
2419 break;
2420 }
2421}
2422
2423static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2424 int nb_sectors, MapEntry *e)
2425{
2426 int64_t ret;
2427 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002428 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002429 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002430
2431 /* As an optimization, we could cache the current range of unallocated
2432 * clusters in each file of the chain, and avoid querying the same
2433 * range repeatedly.
2434 */
2435
2436 depth = 0;
2437 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002438 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2439 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002440 if (ret < 0) {
2441 return ret;
2442 }
2443 assert(nb_sectors);
2444 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2445 break;
2446 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002447 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002448 if (bs == NULL) {
2449 ret = 0;
2450 break;
2451 }
2452
2453 depth++;
2454 }
2455
John Snow28756452016-02-05 13:12:33 -05002456 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2457
2458 *e = (MapEntry) {
2459 .start = sector_num * BDRV_SECTOR_SIZE,
2460 .length = nb_sectors * BDRV_SECTOR_SIZE,
2461 .data = !!(ret & BDRV_BLOCK_DATA),
2462 .zero = !!(ret & BDRV_BLOCK_ZERO),
2463 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2464 .has_offset = has_offset,
2465 .depth = depth,
2466 .has_filename = file && has_offset,
2467 .filename = file && has_offset ? file->filename : NULL,
2468 };
2469
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002470 return 0;
2471}
2472
Fam Zheng16b0d552016-01-26 11:59:02 +08002473static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2474{
2475 if (curr->length == 0) {
2476 return false;
2477 }
2478 if (curr->zero != next->zero ||
2479 curr->data != next->data ||
2480 curr->depth != next->depth ||
2481 curr->has_filename != next->has_filename ||
2482 curr->has_offset != next->has_offset) {
2483 return false;
2484 }
2485 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2486 return false;
2487 }
2488 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2489 return false;
2490 }
2491 return true;
2492}
2493
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002494static int img_map(int argc, char **argv)
2495{
2496 int c;
2497 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002498 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002499 BlockDriverState *bs;
2500 const char *filename, *fmt, *output;
2501 int64_t length;
2502 MapEntry curr = { .length = 0 }, next;
2503 int ret = 0;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002504 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002505 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002506
2507 fmt = NULL;
2508 output = NULL;
2509 for (;;) {
2510 int option_index = 0;
2511 static const struct option long_options[] = {
2512 {"help", no_argument, 0, 'h'},
2513 {"format", required_argument, 0, 'f'},
2514 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002515 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002516 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002517 {0, 0, 0, 0}
2518 };
2519 c = getopt_long(argc, argv, "f:h",
2520 long_options, &option_index);
2521 if (c == -1) {
2522 break;
2523 }
2524 switch (c) {
2525 case '?':
2526 case 'h':
2527 help();
2528 break;
2529 case 'f':
2530 fmt = optarg;
2531 break;
2532 case OPTION_OUTPUT:
2533 output = optarg;
2534 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002535 case OPTION_OBJECT: {
2536 QemuOpts *opts;
2537 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2538 optarg, true);
2539 if (!opts) {
2540 return 1;
2541 }
2542 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002543 case OPTION_IMAGE_OPTS:
2544 image_opts = true;
2545 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002546 }
2547 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002548 if (optind != argc - 1) {
2549 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002550 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002551 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002552
2553 if (output && !strcmp(output, "json")) {
2554 output_format = OFORMAT_JSON;
2555 } else if (output && !strcmp(output, "human")) {
2556 output_format = OFORMAT_HUMAN;
2557 } else if (output) {
2558 error_report("--output must be used with human or json as argument.");
2559 return 1;
2560 }
2561
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002562 if (qemu_opts_foreach(&qemu_object_opts,
2563 user_creatable_add_opts_foreach,
2564 NULL, &local_err)) {
2565 error_report_err(local_err);
2566 return 1;
2567 }
2568
Max Reitzefaa7c42016-03-16 19:54:38 +01002569 blk = img_open(image_opts, filename, fmt, BDRV_O_FLAGS, true, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002570 if (!blk) {
2571 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002572 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002573 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002574
2575 if (output_format == OFORMAT_HUMAN) {
2576 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2577 }
2578
Max Reitzf1d3cd72015-02-05 13:58:18 -05002579 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002580 while (curr.start + curr.length < length) {
2581 int64_t nsectors_left;
2582 int64_t sector_num;
2583 int n;
2584
2585 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2586
2587 /* Probe up to 1 GiB at a time. */
2588 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2589 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2590 ret = get_block_status(bs, sector_num, n, &next);
2591
2592 if (ret < 0) {
2593 error_report("Could not read file metadata: %s", strerror(-ret));
2594 goto out;
2595 }
2596
Fam Zheng16b0d552016-01-26 11:59:02 +08002597 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002598 curr.length += next.length;
2599 continue;
2600 }
2601
2602 if (curr.length > 0) {
2603 dump_map_entry(output_format, &curr, &next);
2604 }
2605 curr = next;
2606 }
2607
2608 dump_map_entry(output_format, &curr, NULL);
2609
2610out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002611 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002612 return ret < 0;
2613}
2614
aliguorif7b4a942009-01-07 17:40:15 +00002615#define SNAPSHOT_LIST 1
2616#define SNAPSHOT_CREATE 2
2617#define SNAPSHOT_APPLY 3
2618#define SNAPSHOT_DELETE 4
2619
Stuart Brady153859b2009-06-07 00:42:17 +01002620static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002621{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002622 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002623 BlockDriverState *bs;
2624 QEMUSnapshotInfo sn;
2625 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002626 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002627 int action = 0;
2628 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002629 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002630 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002631 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002632
Kevin Wolf710da702011-01-10 12:33:02 +01002633 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002634 /* Parse commandline parameters */
2635 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002636 static const struct option long_options[] = {
2637 {"help", no_argument, 0, 'h'},
2638 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002639 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002640 {0, 0, 0, 0}
2641 };
2642 c = getopt_long(argc, argv, "la:c:d:hq",
2643 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002644 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002645 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002646 }
aliguorif7b4a942009-01-07 17:40:15 +00002647 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002648 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002649 case 'h':
2650 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002651 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002652 case 'l':
2653 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002654 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002655 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002656 }
2657 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002658 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002659 break;
2660 case 'a':
2661 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002662 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002663 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002664 }
2665 action = SNAPSHOT_APPLY;
2666 snapshot_name = optarg;
2667 break;
2668 case 'c':
2669 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002670 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002671 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002672 }
2673 action = SNAPSHOT_CREATE;
2674 snapshot_name = optarg;
2675 break;
2676 case 'd':
2677 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002678 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002679 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002680 }
2681 action = SNAPSHOT_DELETE;
2682 snapshot_name = optarg;
2683 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002684 case 'q':
2685 quiet = true;
2686 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002687 case OPTION_OBJECT: {
2688 QemuOpts *opts;
2689 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2690 optarg, true);
2691 if (!opts) {
2692 return 1;
2693 }
2694 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002695 case OPTION_IMAGE_OPTS:
2696 image_opts = true;
2697 break;
aliguorif7b4a942009-01-07 17:40:15 +00002698 }
2699 }
2700
Kevin Wolffc11eb22013-08-05 10:53:04 +02002701 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002702 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002703 }
aliguorif7b4a942009-01-07 17:40:15 +00002704 filename = argv[optind++];
2705
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002706 if (qemu_opts_foreach(&qemu_object_opts,
2707 user_creatable_add_opts_foreach,
2708 NULL, &err)) {
2709 error_report_err(err);
2710 return 1;
2711 }
2712
aliguorif7b4a942009-01-07 17:40:15 +00002713 /* Open the image */
Max Reitzefaa7c42016-03-16 19:54:38 +01002714 blk = img_open(image_opts, filename, NULL, bdrv_oflags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002715 if (!blk) {
2716 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002717 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002718 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002719
2720 /* Perform the requested action */
2721 switch(action) {
2722 case SNAPSHOT_LIST:
2723 dump_snapshots(bs);
2724 break;
2725
2726 case SNAPSHOT_CREATE:
2727 memset(&sn, 0, sizeof(sn));
2728 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2729
2730 qemu_gettimeofday(&tv);
2731 sn.date_sec = tv.tv_sec;
2732 sn.date_nsec = tv.tv_usec * 1000;
2733
2734 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002735 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002736 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002737 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002738 }
aliguorif7b4a942009-01-07 17:40:15 +00002739 break;
2740
2741 case SNAPSHOT_APPLY:
2742 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002743 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002744 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002745 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002746 }
aliguorif7b4a942009-01-07 17:40:15 +00002747 break;
2748
2749 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002750 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002751 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002752 error_reportf_err(err, "Could not delete snapshot '%s': ",
2753 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002754 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002755 }
aliguorif7b4a942009-01-07 17:40:15 +00002756 break;
2757 }
2758
2759 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002760 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002761 if (ret) {
2762 return 1;
2763 }
Stuart Brady153859b2009-06-07 00:42:17 +01002764 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002765}
2766
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002767static int img_rebase(int argc, char **argv)
2768{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002769 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002770 uint8_t *buf_old = NULL;
2771 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002772 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002773 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002774 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2775 int c, flags, src_flags, ret;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002776 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002777 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002778 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002779 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002780 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002781
2782 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002783 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002784 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002785 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002786 out_baseimg = NULL;
2787 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002788 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002789 static const struct option long_options[] = {
2790 {"help", no_argument, 0, 'h'},
2791 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002792 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002793 {0, 0, 0, 0}
2794 };
2795 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2796 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002797 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002798 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002799 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002800 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002801 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002802 case 'h':
2803 help();
2804 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002805 case 'f':
2806 fmt = optarg;
2807 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002808 case 'F':
2809 out_basefmt = optarg;
2810 break;
2811 case 'b':
2812 out_baseimg = optarg;
2813 break;
2814 case 'u':
2815 unsafe = 1;
2816 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002817 case 'p':
2818 progress = 1;
2819 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002820 case 't':
2821 cache = optarg;
2822 break;
Max Reitz40055952014-07-22 22:58:42 +02002823 case 'T':
2824 src_cache = optarg;
2825 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002826 case 'q':
2827 quiet = true;
2828 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002829 case OPTION_OBJECT: {
2830 QemuOpts *opts;
2831 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2832 optarg, true);
2833 if (!opts) {
2834 return 1;
2835 }
2836 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002837 case OPTION_IMAGE_OPTS:
2838 image_opts = true;
2839 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002840 }
2841 }
2842
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002843 if (quiet) {
2844 progress = 0;
2845 }
2846
Fam Zhengac1307a2014-04-22 13:36:11 +08002847 if (optind != argc - 1) {
2848 error_exit("Expecting one image file name");
2849 }
2850 if (!unsafe && !out_baseimg) {
2851 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002852 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002853 filename = argv[optind++];
2854
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002855 if (qemu_opts_foreach(&qemu_object_opts,
2856 user_creatable_add_opts_foreach,
2857 NULL, &local_err)) {
2858 error_report_err(local_err);
2859 return 1;
2860 }
2861
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002862 qemu_progress_init(progress, 2.0);
2863 qemu_progress_print(0, 100);
2864
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002865 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002866 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002867 if (ret < 0) {
2868 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002869 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002870 }
2871
Max Reitz40055952014-07-22 22:58:42 +02002872 src_flags = BDRV_O_FLAGS;
2873 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
2874 if (ret < 0) {
2875 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002876 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002877 }
2878
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002879 /*
2880 * Open the images.
2881 *
2882 * Ignore the old backing file for unsafe rebase in case we want to correct
2883 * the reference to a renamed or moved backing file.
2884 */
Max Reitzefaa7c42016-03-16 19:54:38 +01002885 blk = img_open(image_opts, filename, fmt, flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002886 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002887 ret = -1;
2888 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002889 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002890 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002891
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002892 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002893 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002894 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002895 ret = -1;
2896 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002897 }
2898 }
2899
2900 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002901 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002902 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002903 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002904
Max Reitz644483d2015-02-05 13:58:17 -05002905 if (bs->backing_format[0] != '\0') {
2906 options = qdict_new();
2907 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2908 }
2909
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002910 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002911 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002912 options, src_flags, &local_err);
2913 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002914 error_reportf_err(local_err,
2915 "Could not open old backing file '%s': ",
2916 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002917 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002918 }
Max Reitz644483d2015-02-05 13:58:17 -05002919
Alex Bligha6166732012-10-16 13:46:18 +01002920 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002921 if (out_basefmt) {
2922 options = qdict_new();
2923 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2924 } else {
2925 options = NULL;
2926 }
2927
Max Reitzefaa7c42016-03-16 19:54:38 +01002928 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002929 options, src_flags, &local_err);
2930 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002931 error_reportf_err(local_err,
2932 "Could not open new backing file '%s': ",
2933 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002934 goto out;
2935 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002936 }
2937 }
2938
2939 /*
2940 * Check each unallocated cluster in the COW file. If it is unallocated,
2941 * accesses go to the backing file. We must therefore compare this cluster
2942 * in the old and new backing file, and if they differ we need to copy it
2943 * from the old backing file into the COW file.
2944 *
2945 * If qemu-img crashes during this step, no harm is done. The content of
2946 * the image is the same as the original one at any time.
2947 */
2948 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002949 int64_t num_sectors;
2950 int64_t old_backing_num_sectors;
2951 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002952 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002953 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002954 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002955
Max Reitzf1d3cd72015-02-05 13:58:18 -05002956 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2957 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002958
Max Reitzf1d3cd72015-02-05 13:58:18 -05002959 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002960 if (num_sectors < 0) {
2961 error_report("Could not get size of '%s': %s",
2962 filename, strerror(-num_sectors));
2963 ret = -1;
2964 goto out;
2965 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002966 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002967 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002968 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002969
2970 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2971 error_report("Could not get size of '%s': %s",
2972 backing_name, strerror(-old_backing_num_sectors));
2973 ret = -1;
2974 goto out;
2975 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002976 if (blk_new_backing) {
2977 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002978 if (new_backing_num_sectors < 0) {
2979 error_report("Could not get size of '%s': %s",
2980 out_baseimg, strerror(-new_backing_num_sectors));
2981 ret = -1;
2982 goto out;
2983 }
Alex Bligha6166732012-10-16 13:46:18 +01002984 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002985
Kevin Wolf1f710492012-10-12 14:29:18 +02002986 if (num_sectors != 0) {
2987 local_progress = (float)100 /
2988 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2989 }
2990
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002991 for (sector = 0; sector < num_sectors; sector += n) {
2992
2993 /* How many sectors can we handle with the next read? */
2994 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2995 n = (IO_BUF_SIZE / 512);
2996 } else {
2997 n = num_sectors - sector;
2998 }
2999
3000 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003001 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003002 if (ret < 0) {
3003 error_report("error while reading image metadata: %s",
3004 strerror(-ret));
3005 goto out;
3006 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003007 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003008 continue;
3009 }
3010
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003011 /*
3012 * Read old and new backing file and take into consideration that
3013 * backing files may be smaller than the COW image.
3014 */
3015 if (sector >= old_backing_num_sectors) {
3016 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3017 } else {
3018 if (sector + n > old_backing_num_sectors) {
3019 n = old_backing_num_sectors - sector;
3020 }
3021
Max Reitzf1d3cd72015-02-05 13:58:18 -05003022 ret = blk_read(blk_old_backing, sector, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003023 if (ret < 0) {
3024 error_report("error while reading from old backing file");
3025 goto out;
3026 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003027 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003028
Max Reitzf1d3cd72015-02-05 13:58:18 -05003029 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003030 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3031 } else {
3032 if (sector + n > new_backing_num_sectors) {
3033 n = new_backing_num_sectors - sector;
3034 }
3035
Max Reitzf1d3cd72015-02-05 13:58:18 -05003036 ret = blk_read(blk_new_backing, sector, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003037 if (ret < 0) {
3038 error_report("error while reading from new backing file");
3039 goto out;
3040 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003041 }
3042
3043 /* If they differ, we need to write to the COW file */
3044 uint64_t written = 0;
3045
3046 while (written < n) {
3047 int pnum;
3048
3049 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003050 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003051 {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003052 ret = blk_write(blk, sector + written,
3053 buf_old + written * 512, pnum);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003054 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003055 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003056 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003057 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003058 }
3059 }
3060
3061 written += pnum;
3062 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003063 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003064 }
3065 }
3066
3067 /*
3068 * Change the backing file. All clusters that are different from the old
3069 * backing file are overwritten in the COW file now, so the visible content
3070 * doesn't change when we switch the backing file.
3071 */
Alex Bligha6166732012-10-16 13:46:18 +01003072 if (out_baseimg && *out_baseimg) {
3073 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3074 } else {
3075 ret = bdrv_change_backing_file(bs, NULL, NULL);
3076 }
3077
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003078 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003079 error_report("Could not change the backing file to '%s': No "
3080 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003081 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003082 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003083 out_baseimg, strerror(-ret));
3084 }
3085
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003086 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003087 /*
3088 * TODO At this point it is possible to check if any clusters that are
3089 * allocated in the COW file are the same in the backing file. If so, they
3090 * could be dropped from the COW file. Don't do this before switching the
3091 * backing file, in case of a crash this would lead to corruption.
3092 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003093out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003094 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003095 /* Cleanup */
3096 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003097 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003098 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003099 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003100 qemu_vfree(buf_old);
3101 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003102
Markus Armbruster26f54e92014-10-07 13:59:04 +02003103 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003104 if (ret) {
3105 return 1;
3106 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003107 return 0;
3108}
3109
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003110static int img_resize(int argc, char **argv)
3111{
Markus Armbruster6750e792015-02-12 17:43:08 +01003112 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003113 int c, ret, relative;
3114 const char *filename, *fmt, *size;
3115 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003116 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003117 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003118 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003119 Error *local_err = NULL;
3120
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003121 static QemuOptsList resize_options = {
3122 .name = "resize_options",
3123 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3124 .desc = {
3125 {
3126 .name = BLOCK_OPT_SIZE,
3127 .type = QEMU_OPT_SIZE,
3128 .help = "Virtual disk size"
3129 }, {
3130 /* end of list */
3131 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003132 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003133 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003134 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003135
Kevin Wolfe80fec72011-04-29 10:58:12 +02003136 /* Remove size from argv manually so that negative numbers are not treated
3137 * as options by getopt. */
3138 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003139 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003140 return 1;
3141 }
3142
3143 size = argv[--argc];
3144
3145 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003146 fmt = NULL;
3147 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003148 static const struct option long_options[] = {
3149 {"help", no_argument, 0, 'h'},
3150 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003151 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003152 {0, 0, 0, 0}
3153 };
3154 c = getopt_long(argc, argv, "f:hq",
3155 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003156 if (c == -1) {
3157 break;
3158 }
3159 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003160 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003161 case 'h':
3162 help();
3163 break;
3164 case 'f':
3165 fmt = optarg;
3166 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003167 case 'q':
3168 quiet = true;
3169 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003170 case OPTION_OBJECT: {
3171 QemuOpts *opts;
3172 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3173 optarg, true);
3174 if (!opts) {
3175 return 1;
3176 }
3177 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003178 case OPTION_IMAGE_OPTS:
3179 image_opts = true;
3180 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003181 }
3182 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003183 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003184 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003185 }
3186 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003187
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003188 if (qemu_opts_foreach(&qemu_object_opts,
3189 user_creatable_add_opts_foreach,
3190 NULL, &local_err)) {
3191 error_report_err(local_err);
3192 return 1;
3193 }
3194
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003195 /* Choose grow, shrink, or absolute resize mode */
3196 switch (size[0]) {
3197 case '+':
3198 relative = 1;
3199 size++;
3200 break;
3201 case '-':
3202 relative = -1;
3203 size++;
3204 break;
3205 default:
3206 relative = 0;
3207 break;
3208 }
3209
3210 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003211 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003212 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003213 if (err) {
3214 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003215 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003216 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003217 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003218 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003219 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3220 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003221
Max Reitzefaa7c42016-03-16 19:54:38 +01003222 blk = img_open(image_opts, filename, fmt,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003223 BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003224 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003225 ret = -1;
3226 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003227 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003228
3229 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003230 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003231 } else {
3232 total_size = n;
3233 }
3234 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003235 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003236 ret = -1;
3237 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003238 }
3239
Max Reitzf1d3cd72015-02-05 13:58:18 -05003240 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003241 switch (ret) {
3242 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003243 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003244 break;
3245 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003246 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003247 break;
3248 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003249 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003250 break;
3251 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003252 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003253 break;
3254 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003255out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003256 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003257 if (ret) {
3258 return 1;
3259 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003260 return 0;
3261}
3262
Max Reitz76a3a342014-10-27 11:12:51 +01003263static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003264 int64_t offset, int64_t total_work_size,
3265 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003266{
3267 qemu_progress_print(100.f * offset / total_work_size, 0);
3268}
3269
Max Reitz6f176b42013-09-03 10:09:50 +02003270static int img_amend(int argc, char **argv)
3271{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003272 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003273 int c, ret = 0;
3274 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003275 QemuOptsList *create_opts = NULL;
3276 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003277 const char *fmt = NULL, *filename, *cache;
3278 int flags;
Max Reitz76a3a342014-10-27 11:12:51 +01003279 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003280 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003281 BlockDriverState *bs = NULL;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003282 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003283 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003284
Max Reitzbd39e6e2014-07-22 22:58:43 +02003285 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003286 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003287 static const struct option long_options[] = {
3288 {"help", no_argument, 0, 'h'},
3289 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003290 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003291 {0, 0, 0, 0}
3292 };
3293 c = getopt_long(argc, argv, "ho:f:t:pq",
3294 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003295 if (c == -1) {
3296 break;
3297 }
3298
3299 switch (c) {
3300 case 'h':
3301 case '?':
3302 help();
3303 break;
3304 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003305 if (!is_valid_option_list(optarg)) {
3306 error_report("Invalid option list: %s", optarg);
3307 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003308 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003309 }
3310 if (!options) {
3311 options = g_strdup(optarg);
3312 } else {
3313 char *old_options = options;
3314 options = g_strdup_printf("%s,%s", options, optarg);
3315 g_free(old_options);
3316 }
Max Reitz6f176b42013-09-03 10:09:50 +02003317 break;
3318 case 'f':
3319 fmt = optarg;
3320 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003321 case 't':
3322 cache = optarg;
3323 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003324 case 'p':
3325 progress = true;
3326 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003327 case 'q':
3328 quiet = true;
3329 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003330 case OPTION_OBJECT:
3331 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3332 optarg, true);
3333 if (!opts) {
3334 ret = -1;
3335 goto out_no_progress;
3336 }
3337 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003338 case OPTION_IMAGE_OPTS:
3339 image_opts = true;
3340 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003341 }
3342 }
3343
Max Reitz6f176b42013-09-03 10:09:50 +02003344 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003345 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003346 }
3347
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003348 if (qemu_opts_foreach(&qemu_object_opts,
3349 user_creatable_add_opts_foreach,
3350 NULL, &local_err)) {
3351 error_report_err(local_err);
3352 ret = -1;
3353 goto out_no_progress;
3354 }
3355
Max Reitz76a3a342014-10-27 11:12:51 +01003356 if (quiet) {
3357 progress = false;
3358 }
3359 qemu_progress_init(progress, 1.0);
3360
Kevin Wolfa283cb62014-02-21 16:24:07 +01003361 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3362 if (fmt && has_help_option(options)) {
3363 /* If a format is explicitly specified (and possibly no filename is
3364 * given), print option help here */
3365 ret = print_block_option_help(filename, fmt);
3366 goto out;
3367 }
3368
3369 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003370 error_report("Expecting one image file name");
3371 ret = -1;
3372 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003373 }
Max Reitz6f176b42013-09-03 10:09:50 +02003374
Max Reitzbd39e6e2014-07-22 22:58:43 +02003375 flags = BDRV_O_FLAGS | BDRV_O_RDWR;
3376 ret = bdrv_parse_cache_flags(cache, &flags);
3377 if (ret < 0) {
3378 error_report("Invalid cache option: %s", cache);
3379 goto out;
3380 }
3381
Max Reitzefaa7c42016-03-16 19:54:38 +01003382 blk = img_open(image_opts, filename, fmt, flags, true, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003383 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003384 ret = -1;
3385 goto out;
3386 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003387 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003388
3389 fmt = bs->drv->format_name;
3390
Kevin Wolf626f84f2014-02-21 16:24:06 +01003391 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003392 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003393 ret = print_block_option_help(filename, fmt);
3394 goto out;
3395 }
3396
Max Reitzb2439d22014-12-02 18:32:47 +01003397 if (!bs->drv->create_opts) {
3398 error_report("Format driver '%s' does not support any options to amend",
3399 fmt);
3400 ret = -1;
3401 goto out;
3402 }
3403
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003404 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003405 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003406 if (options) {
3407 qemu_opts_do_parse(opts, options, NULL, &err);
3408 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003409 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003410 ret = -1;
3411 goto out;
3412 }
Max Reitz6f176b42013-09-03 10:09:50 +02003413 }
3414
Max Reitz76a3a342014-10-27 11:12:51 +01003415 /* In case the driver does not call amend_status_cb() */
3416 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003417 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003418 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003419 if (ret < 0) {
3420 error_report("Error while amending options: %s", strerror(-ret));
3421 goto out;
3422 }
3423
3424out:
Max Reitz76a3a342014-10-27 11:12:51 +01003425 qemu_progress_end();
3426
Max Reitze814dff2015-08-20 16:00:38 -07003427out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003428 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003429 qemu_opts_del(opts);
3430 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003431 g_free(options);
3432
Max Reitz6f176b42013-09-03 10:09:50 +02003433 if (ret) {
3434 return 1;
3435 }
3436 return 0;
3437}
3438
Anthony Liguoric227f092009-10-01 16:12:16 -05003439static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003440#define DEF(option, callback, arg_string) \
3441 { option, callback },
3442#include "qemu-img-cmds.h"
3443#undef DEF
3444#undef GEN_DOCS
3445 { NULL, NULL, },
3446};
3447
bellardea2384d2004-08-01 21:59:26 +00003448int main(int argc, char **argv)
3449{
Anthony Liguoric227f092009-10-01 16:12:16 -05003450 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003451 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003452 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003453 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003454 static const struct option long_options[] = {
3455 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04003456 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04003457 {0, 0, 0, 0}
3458 };
bellardea2384d2004-08-01 21:59:26 +00003459
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003460#ifdef CONFIG_POSIX
3461 signal(SIGPIPE, SIG_IGN);
3462#endif
3463
Kevin Wolf53f76e52010-12-16 15:10:32 +01003464 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003465 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003466
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003467 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003468 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003469 exit(EXIT_FAILURE);
3470 }
3471
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003472 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003473 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003474 if (argc < 2) {
3475 error_exit("Not enough arguments");
3476 }
Stuart Brady153859b2009-06-07 00:42:17 +01003477 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01003478
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003479 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003480 qemu_add_opts(&qemu_source_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003481
Stuart Brady153859b2009-06-07 00:42:17 +01003482 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04003483 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01003484 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04003485 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01003486 }
bellardea2384d2004-08-01 21:59:26 +00003487 }
Stuart Brady153859b2009-06-07 00:42:17 +01003488
Jeff Cody5f6979c2014-04-28 14:37:18 -04003489 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04003490
3491 if (c == 'h') {
3492 help();
3493 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04003494 if (c == 'v') {
3495 printf(QEMU_IMG_VERSION);
3496 return 0;
3497 }
Jeff Cody7db16892014-04-25 17:02:32 -04003498
Stuart Brady153859b2009-06-07 00:42:17 +01003499 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003500 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003501}