blob: 7ef90af88c95a25d69f41c083299087d91888874 [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"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020026#include "qapi-visit.h"
27#include "qapi/qmp-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010028#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010029#include "qapi/qmp/qjson.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020030#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000031#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/option.h"
33#include "qemu/error-report.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000034#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020036#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010037#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020038#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080039#include "block/qapi.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020040#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000041
Don Slutz61979a62015-01-09 10:17:35 -050042#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Jeff Cody5f6979c2014-04-28 14:37:18 -040043 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
44
Anthony Liguoric227f092009-10-01 16:12:16 -050045typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010046 const char *name;
47 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050048} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010049
Federico Simoncelli8599ea42013-01-28 06:59:47 -050050enum {
51 OPTION_OUTPUT = 256,
52 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000053 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000054 OPTION_IMAGE_OPTS = 259,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050055};
56
57typedef enum OutputFormat {
58 OFORMAT_JSON,
59 OFORMAT_HUMAN,
60} OutputFormat;
61
Kevin Wolfe6996142016-03-15 13:03:11 +010062/* Default to cache=writeback as data integrity is not important for qemu-img */
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,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000227 int flags, 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);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000233 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
234 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000235 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
236 if (qemu_read_password(password, sizeof(password)) < 0) {
237 error_report("No password given");
238 return -1;
239 }
240 if (bdrv_set_key(bs, password) < 0) {
241 error_report("invalid password");
242 return -1;
243 }
244 }
245 return 0;
246}
247
248
Max Reitzefaa7c42016-03-16 19:54:38 +0100249static BlockBackend *img_open_opts(const char *optstr,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000250 QemuOpts *opts, int flags,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000251 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000252{
253 QDict *options;
254 Error *local_err = NULL;
255 BlockBackend *blk;
256 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100257 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000258 if (!blk) {
259 error_reportf_err(local_err, "Could not open '%s'", optstr);
260 return NULL;
261 }
262
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000263 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000264 blk_unref(blk);
265 return NULL;
266 }
267 return blk;
268}
269
Max Reitzefaa7c42016-03-16 19:54:38 +0100270static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000271 const char *fmt, int flags,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000272 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000273{
274 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200275 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500276 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100277
bellard75c23802004-08-27 21:28:58 +0000278 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500279 options = qdict_new();
280 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000281 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100282
Max Reitzefaa7c42016-03-16 19:54:38 +0100283 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500284 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100285 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000286 return NULL;
bellard75c23802004-08-27 21:28:58 +0000287 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100288
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000289 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000290 blk_unref(blk);
291 return NULL;
bellard75c23802004-08-27 21:28:58 +0000292 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200293 return blk;
bellard75c23802004-08-27 21:28:58 +0000294}
295
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000296
Max Reitzefaa7c42016-03-16 19:54:38 +0100297static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298 const char *filename,
299 const char *fmt, int flags,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000300 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000301{
302 BlockBackend *blk;
303 if (image_opts) {
304 QemuOpts *opts;
305 if (fmt) {
306 error_report("--image-opts and --format are mutually exclusive");
307 return NULL;
308 }
309 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
310 filename, true);
311 if (!opts) {
312 return NULL;
313 }
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000314 blk = img_open_opts(filename, opts, flags, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000315 } else {
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000316 blk = img_open_file(filename, fmt, flags, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000317 }
318 return blk;
319}
320
321
Chunyan Liu83d05212014-06-05 17:20:51 +0800322static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100323 const char *base_filename,
324 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200325{
Markus Armbruster6750e792015-02-12 17:43:08 +0100326 Error *err = NULL;
327
Kevin Wolfefa84d42009-05-18 16:42:12 +0200328 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100329 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100330 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100331 error_report("Backing file not supported for file format '%s'",
332 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100333 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900334 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200335 }
336 }
337 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100338 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100339 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100340 error_report("Backing file format not supported for file "
341 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100342 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900343 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200344 }
345 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900346 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200347}
348
bellardea2384d2004-08-01 21:59:26 +0000349static int img_create(int argc, char **argv)
350{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200351 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100352 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000353 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000354 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000355 const char *filename;
356 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200357 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200358 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100359 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000360
bellardea2384d2004-08-01 21:59:26 +0000361 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000362 static const struct option long_options[] = {
363 {"help", no_argument, 0, 'h'},
364 {"object", required_argument, 0, OPTION_OBJECT},
365 {0, 0, 0, 0}
366 };
367 c = getopt_long(argc, argv, "F:b:f:he6o:q",
368 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100369 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000370 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100371 }
bellardea2384d2004-08-01 21:59:26 +0000372 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100373 case '?':
bellardea2384d2004-08-01 21:59:26 +0000374 case 'h':
375 help();
376 break;
aliguori9230eaf2009-03-28 17:55:19 +0000377 case 'F':
378 base_fmt = optarg;
379 break;
bellardea2384d2004-08-01 21:59:26 +0000380 case 'b':
381 base_filename = optarg;
382 break;
383 case 'f':
384 fmt = optarg;
385 break;
386 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200387 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100388 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100389 goto fail;
thsd8871c52007-10-24 16:11:42 +0000390 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200391 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100392 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100393 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200394 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100395 if (!is_valid_option_list(optarg)) {
396 error_report("Invalid option list: %s", optarg);
397 goto fail;
398 }
399 if (!options) {
400 options = g_strdup(optarg);
401 } else {
402 char *old_options = options;
403 options = g_strdup_printf("%s,%s", options, optarg);
404 g_free(old_options);
405 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200406 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100407 case 'q':
408 quiet = true;
409 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000410 case OPTION_OBJECT: {
411 QemuOpts *opts;
412 opts = qemu_opts_parse_noisily(&qemu_object_opts,
413 optarg, true);
414 if (!opts) {
415 goto fail;
416 }
417 } break;
bellardea2384d2004-08-01 21:59:26 +0000418 }
419 }
aliguori9230eaf2009-03-28 17:55:19 +0000420
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900421 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100422 filename = (optind < argc) ? argv[optind] : NULL;
423 if (options && has_help_option(options)) {
424 g_free(options);
425 return print_block_option_help(filename, fmt);
426 }
427
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100428 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800429 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100430 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100431 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900432
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000433 if (qemu_opts_foreach(&qemu_object_opts,
434 user_creatable_add_opts_foreach,
435 NULL, &local_err)) {
436 error_report_err(local_err);
437 goto fail;
438 }
439
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100440 /* Get image size, if specified */
441 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100442 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100443 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200444 sval = qemu_strtosz_suffix(argv[optind++], &end,
445 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100446 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800447 if (sval == -ERANGE) {
448 error_report("Image size must be less than 8 EiB!");
449 } else {
450 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200451 "G, T, P or E suffixes for ");
452 error_report("kilobytes, megabytes, gigabytes, terabytes, "
453 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800454 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100455 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100456 }
457 img_size = (uint64_t)sval;
458 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200459 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800460 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200461 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100462
Luiz Capitulino9b375252012-11-30 10:52:05 -0200463 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Kevin Wolfe6996142016-03-15 13:03:11 +0100464 options, img_size, BDRV_O_CACHE_WB, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100465 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100466 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100467 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900468 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200469
Kevin Wolf77386bf2014-02-21 16:24:04 +0100470 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000471 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100472
473fail:
474 g_free(options);
475 return 1;
bellardea2384d2004-08-01 21:59:26 +0000476}
477
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100478static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500479{
Markus Armbruster4399c432014-04-25 16:50:32 +0200480 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500481 QString *str;
482 QmpOutputVisitor *ov = qmp_output_visitor_new();
483 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -0700484 visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
485 &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500486 obj = qmp_output_get_qobject(ov);
487 str = qobject_to_json_pretty(obj);
488 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100489 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500490 qobject_decref(obj);
491 qmp_output_visitor_cleanup(ov);
492 QDECREF(str);
493}
494
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100495static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500496{
497 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100498 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500499 } else {
500 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100501 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
502 "Data may be corrupted, or further writes to the image "
503 "may corrupt it.\n",
504 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500505 }
506
507 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100508 qprintf(quiet,
509 "\n%" PRId64 " leaked clusters were found on the image.\n"
510 "This means waste of disk space, but no harm to data.\n",
511 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500512 }
513
514 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100515 qprintf(quiet,
516 "\n%" PRId64
517 " internal errors have occurred during the check.\n",
518 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500519 }
520 }
521
522 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100523 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
524 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
525 check->allocated_clusters, check->total_clusters,
526 check->allocated_clusters * 100.0 / check->total_clusters,
527 check->fragmented_clusters * 100.0 / check->allocated_clusters,
528 check->compressed_clusters * 100.0 /
529 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500530 }
531
532 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100533 qprintf(quiet,
534 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500535 }
536}
537
538static int collect_image_check(BlockDriverState *bs,
539 ImageCheck *check,
540 const char *filename,
541 const char *fmt,
542 int fix)
543{
544 int ret;
545 BdrvCheckResult result;
546
547 ret = bdrv_check(bs, &result, fix);
548 if (ret < 0) {
549 return ret;
550 }
551
552 check->filename = g_strdup(filename);
553 check->format = g_strdup(bdrv_get_format_name(bs));
554 check->check_errors = result.check_errors;
555 check->corruptions = result.corruptions;
556 check->has_corruptions = result.corruptions != 0;
557 check->leaks = result.leaks;
558 check->has_leaks = result.leaks != 0;
559 check->corruptions_fixed = result.corruptions_fixed;
560 check->has_corruptions_fixed = result.corruptions != 0;
561 check->leaks_fixed = result.leaks_fixed;
562 check->has_leaks_fixed = result.leaks != 0;
563 check->image_end_offset = result.image_end_offset;
564 check->has_image_end_offset = result.image_end_offset != 0;
565 check->total_clusters = result.bfi.total_clusters;
566 check->has_total_clusters = result.bfi.total_clusters != 0;
567 check->allocated_clusters = result.bfi.allocated_clusters;
568 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
569 check->fragmented_clusters = result.bfi.fragmented_clusters;
570 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100571 check->compressed_clusters = result.bfi.compressed_clusters;
572 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500573
574 return 0;
575}
576
Kevin Wolfe076f332010-06-29 11:43:13 +0200577/*
578 * Checks an image for consistency. Exit codes:
579 *
Max Reitzd6635c42014-06-02 22:15:21 +0200580 * 0 - Check completed, image is good
581 * 1 - Check not completed because of internal errors
582 * 2 - Check completed, image is corrupted
583 * 3 - Check completed, image has leaked clusters, but is good otherwise
584 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200585 */
aliguori15859692009-04-21 23:11:53 +0000586static int img_check(int argc, char **argv)
587{
588 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500589 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200590 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200591 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000592 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200593 int fix = 0;
Kevin Wolfe6996142016-03-15 13:03:11 +0100594 int flags = BDRV_O_CACHE_WB | BDRV_O_CHECK;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200595 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100596 bool quiet = false;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000597 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000598 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000599
600 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500601 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200602 cache = BDRV_DEFAULT_CACHE;
aliguori15859692009-04-21 23:11:53 +0000603 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500604 int option_index = 0;
605 static const struct option long_options[] = {
606 {"help", no_argument, 0, 'h'},
607 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530608 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500609 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000610 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000611 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500612 {0, 0, 0, 0}
613 };
Max Reitz40055952014-07-22 22:58:42 +0200614 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500615 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100616 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000617 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100618 }
aliguori15859692009-04-21 23:11:53 +0000619 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100620 case '?':
aliguori15859692009-04-21 23:11:53 +0000621 case 'h':
622 help();
623 break;
624 case 'f':
625 fmt = optarg;
626 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200627 case 'r':
628 flags |= BDRV_O_RDWR;
629
630 if (!strcmp(optarg, "leaks")) {
631 fix = BDRV_FIX_LEAKS;
632 } else if (!strcmp(optarg, "all")) {
633 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
634 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800635 error_exit("Unknown option value for -r "
636 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200637 }
638 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500639 case OPTION_OUTPUT:
640 output = optarg;
641 break;
Max Reitz40055952014-07-22 22:58:42 +0200642 case 'T':
643 cache = optarg;
644 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100645 case 'q':
646 quiet = true;
647 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000648 case OPTION_OBJECT: {
649 QemuOpts *opts;
650 opts = qemu_opts_parse_noisily(&qemu_object_opts,
651 optarg, true);
652 if (!opts) {
653 return 1;
654 }
655 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000656 case OPTION_IMAGE_OPTS:
657 image_opts = true;
658 break;
aliguori15859692009-04-21 23:11:53 +0000659 }
660 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200661 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800662 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100663 }
aliguori15859692009-04-21 23:11:53 +0000664 filename = argv[optind++];
665
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500666 if (output && !strcmp(output, "json")) {
667 output_format = OFORMAT_JSON;
668 } else if (output && !strcmp(output, "human")) {
669 output_format = OFORMAT_HUMAN;
670 } else if (output) {
671 error_report("--output must be used with human or json as argument.");
672 return 1;
673 }
674
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000675 if (qemu_opts_foreach(&qemu_object_opts,
676 user_creatable_add_opts_foreach,
677 NULL, &local_err)) {
678 error_report_err(local_err);
679 return 1;
680 }
681
Max Reitz40055952014-07-22 22:58:42 +0200682 ret = bdrv_parse_cache_flags(cache, &flags);
683 if (ret < 0) {
684 error_report("Invalid source cache option: %s", cache);
685 return 1;
686 }
687
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000688 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200689 if (!blk) {
690 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900691 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200692 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500693
694 check = g_new0(ImageCheck, 1);
695 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200696
697 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200698 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200699 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500700 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200701 }
702
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500703 if (check->corruptions_fixed || check->leaks_fixed) {
704 int corruptions_fixed, leaks_fixed;
705
706 leaks_fixed = check->leaks_fixed;
707 corruptions_fixed = check->corruptions_fixed;
708
709 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100710 qprintf(quiet,
711 "The following inconsistencies were found and repaired:\n\n"
712 " %" PRId64 " leaked clusters\n"
713 " %" PRId64 " corruptions\n\n"
714 "Double checking the fixed image now...\n",
715 check->leaks_fixed,
716 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500717 }
718
719 ret = collect_image_check(bs, check, filename, fmt, 0);
720
721 check->leaks_fixed = leaks_fixed;
722 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200723 }
724
Max Reitz832390a2014-10-23 15:29:12 +0200725 if (!ret) {
726 switch (output_format) {
727 case OFORMAT_HUMAN:
728 dump_human_image_check(check, quiet);
729 break;
730 case OFORMAT_JSON:
731 dump_json_image_check(check, quiet);
732 break;
733 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500734 }
735
736 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200737 if (ret) {
738 error_report("Check failed: %s", strerror(-ret));
739 } else {
740 error_report("Check failed");
741 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500742 ret = 1;
743 goto fail;
744 }
745
746 if (check->corruptions) {
747 ret = 2;
748 } else if (check->leaks) {
749 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200750 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500751 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000752 }
753
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500754fail:
755 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200756 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500757 return ret;
aliguori15859692009-04-21 23:11:53 +0000758}
759
Max Reitzd4a32382014-10-24 15:57:37 +0200760typedef struct CommonBlockJobCBInfo {
761 BlockDriverState *bs;
762 Error **errp;
763} CommonBlockJobCBInfo;
764
765static void common_block_job_cb(void *opaque, int ret)
766{
767 CommonBlockJobCBInfo *cbi = opaque;
768
769 if (ret < 0) {
770 error_setg_errno(cbi->errp, -ret, "Block job failed");
771 }
Max Reitzd4a32382014-10-24 15:57:37 +0200772}
773
774static void run_block_job(BlockJob *job, Error **errp)
775{
776 AioContext *aio_context = bdrv_get_aio_context(job->bs);
777
778 do {
779 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500780 qemu_progress_print(job->len ?
781 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200782 } while (!job->ready);
783
784 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200785
786 /* A block job may finish instantaneously without publishing any progress,
787 * so just signal completion here */
788 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200789}
790
bellardea2384d2004-08-01 21:59:26 +0000791static int img_commit(int argc, char **argv)
792{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400793 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200794 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200795 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200796 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200797 bool progress = false, quiet = false, drop = false;
Max Reitzd4a32382014-10-24 15:57:37 +0200798 Error *local_err = NULL;
799 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000800 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000801
802 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400803 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200804 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000805 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000806 static const struct option long_options[] = {
807 {"help", no_argument, 0, 'h'},
808 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000809 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000810 {0, 0, 0, 0}
811 };
812 c = getopt_long(argc, argv, "f:ht:b:dpq",
813 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100814 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000815 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100816 }
bellardea2384d2004-08-01 21:59:26 +0000817 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100818 case '?':
bellardea2384d2004-08-01 21:59:26 +0000819 case 'h':
820 help();
821 break;
822 case 'f':
823 fmt = optarg;
824 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400825 case 't':
826 cache = optarg;
827 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200828 case 'b':
829 base = optarg;
830 /* -b implies -d */
831 drop = true;
832 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200833 case 'd':
834 drop = true;
835 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200836 case 'p':
837 progress = true;
838 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100839 case 'q':
840 quiet = true;
841 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000842 case OPTION_OBJECT: {
843 QemuOpts *opts;
844 opts = qemu_opts_parse_noisily(&qemu_object_opts,
845 optarg, true);
846 if (!opts) {
847 return 1;
848 }
849 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000850 case OPTION_IMAGE_OPTS:
851 image_opts = true;
852 break;
bellardea2384d2004-08-01 21:59:26 +0000853 }
854 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200855
856 /* Progress is not shown in Quiet mode */
857 if (quiet) {
858 progress = false;
859 }
860
Kevin Wolffc11eb22013-08-05 10:53:04 +0200861 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800862 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100863 }
bellardea2384d2004-08-01 21:59:26 +0000864 filename = argv[optind++];
865
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000866 if (qemu_opts_foreach(&qemu_object_opts,
867 user_creatable_add_opts_foreach,
868 NULL, &local_err)) {
869 error_report_err(local_err);
870 return 1;
871 }
872
Max Reitz9a86fe42014-10-24 15:57:38 +0200873 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100874 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400875 if (ret < 0) {
876 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100877 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400878 }
879
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000880 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200881 if (!blk) {
882 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900883 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200884 bs = blk_bs(blk);
885
Max Reitz687fa1d2014-10-24 15:57:39 +0200886 qemu_progress_init(progress, 1.f);
887 qemu_progress_print(0.f, 100);
888
Max Reitz1b22bff2014-10-24 15:57:40 +0200889 if (base) {
890 base_bs = bdrv_find_backing_image(bs, base);
891 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100892 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200893 goto done;
894 }
895 } else {
896 /* This is different from QMP, which by default uses the deepest file in
897 * the backing chain (i.e., the very base); however, the traditional
898 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200899 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200900 if (!base_bs) {
901 error_setg(&local_err, "Image does not have a backing file");
902 goto done;
903 }
bellardea2384d2004-08-01 21:59:26 +0000904 }
905
Max Reitzd4a32382014-10-24 15:57:37 +0200906 cbi = (CommonBlockJobCBInfo){
907 .errp = &local_err,
908 .bs = bs,
909 };
910
911 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
912 common_block_job_cb, &cbi, &local_err);
913 if (local_err) {
914 goto done;
915 }
916
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200917 /* When the block job completes, the BlockBackend reference will point to
918 * the old backing file. In order to avoid that the top image is already
919 * deleted, so we can still empty it afterwards, increment the reference
920 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200921 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200922 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200923 }
924
Max Reitzd4a32382014-10-24 15:57:37 +0200925 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200926 if (local_err) {
927 goto unref_backing;
928 }
929
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200930 if (!drop && bs->drv->bdrv_make_empty) {
931 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200932 if (ret) {
933 error_setg_errno(&local_err, -ret, "Could not empty %s",
934 filename);
935 goto unref_backing;
936 }
937 }
938
939unref_backing:
940 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200941 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200942 }
Max Reitzd4a32382014-10-24 15:57:37 +0200943
944done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200945 qemu_progress_end();
946
Markus Armbruster26f54e92014-10-07 13:59:04 +0200947 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200948
949 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100950 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900951 return 1;
952 }
Max Reitzd4a32382014-10-24 15:57:37 +0200953
954 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000955 return 0;
956}
957
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400958/*
thsf58c7b32008-06-05 21:53:49 +0000959 * Returns true iff the first sector pointed to by 'buf' contains at least
960 * a non-NUL byte.
961 *
962 * 'pnum' is set to the number of sectors (including and immediately following
963 * the first one) that are known to be in the same allocated/unallocated state.
964 */
bellardea2384d2004-08-01 21:59:26 +0000965static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
966{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000967 bool is_zero;
968 int i;
bellardea2384d2004-08-01 21:59:26 +0000969
970 if (n <= 0) {
971 *pnum = 0;
972 return 0;
973 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000974 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000975 for(i = 1; i < n; i++) {
976 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000977 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000978 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000979 }
bellardea2384d2004-08-01 21:59:26 +0000980 }
981 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000982 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000983}
984
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100985/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200986 * Like is_allocated_sectors, but if the buffer starts with a used sector,
987 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
988 * breaking up write requests for only small sparse areas.
989 */
990static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
991 int min)
992{
993 int ret;
994 int num_checked, num_used;
995
996 if (n < min) {
997 min = n;
998 }
999
1000 ret = is_allocated_sectors(buf, n, pnum);
1001 if (!ret) {
1002 return ret;
1003 }
1004
1005 num_used = *pnum;
1006 buf += BDRV_SECTOR_SIZE * *pnum;
1007 n -= *pnum;
1008 num_checked = num_used;
1009
1010 while (n > 0) {
1011 ret = is_allocated_sectors(buf, n, pnum);
1012
1013 buf += BDRV_SECTOR_SIZE * *pnum;
1014 n -= *pnum;
1015 num_checked += *pnum;
1016 if (ret) {
1017 num_used = num_checked;
1018 } else if (*pnum >= min) {
1019 break;
1020 }
1021 }
1022
1023 *pnum = num_used;
1024 return 1;
1025}
1026
1027/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001028 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1029 * buffers matches, non-zero otherwise.
1030 *
1031 * pnum is set to the number of sectors (including and immediately following
1032 * the first one) that are known to have the same comparison result
1033 */
1034static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1035 int *pnum)
1036{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001037 bool res;
1038 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001039
1040 if (n <= 0) {
1041 *pnum = 0;
1042 return 0;
1043 }
1044
1045 res = !!memcmp(buf1, buf2, 512);
1046 for(i = 1; i < n; i++) {
1047 buf1 += 512;
1048 buf2 += 512;
1049
1050 if (!!memcmp(buf1, buf2, 512) != res) {
1051 break;
1052 }
1053 }
1054
1055 *pnum = i;
1056 return res;
1057}
1058
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001059#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001060
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001061static int64_t sectors_to_bytes(int64_t sectors)
1062{
1063 return sectors << BDRV_SECTOR_BITS;
1064}
1065
1066static int64_t sectors_to_process(int64_t total, int64_t from)
1067{
1068 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1069}
1070
1071/*
1072 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1073 *
1074 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1075 * data and negative value on error.
1076 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001077 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001078 * @param sect_num: Number of first sector to check
1079 * @param sect_count: Number of sectors to check
1080 * @param filename: Name of disk file we are checking (logging purpose)
1081 * @param buffer: Allocated buffer for storing read data
1082 * @param quiet: Flag for quiet mode
1083 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001084static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001085 int sect_count, const char *filename,
1086 uint8_t *buffer, bool quiet)
1087{
1088 int pnum, ret = 0;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001089 ret = blk_read(blk, sect_num, buffer, sect_count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001090 if (ret < 0) {
1091 error_report("Error while reading offset %" PRId64 " of %s: %s",
1092 sectors_to_bytes(sect_num), filename, strerror(-ret));
1093 return ret;
1094 }
1095 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1096 if (ret || pnum != sect_count) {
1097 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1098 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1099 return 1;
1100 }
1101
1102 return 0;
1103}
1104
1105/*
1106 * Compares two images. Exit codes:
1107 *
1108 * 0 - Images are identical
1109 * 1 - Images differ
1110 * >1 - Error occurred
1111 */
1112static int img_compare(int argc, char **argv)
1113{
Max Reitz40055952014-07-22 22:58:42 +02001114 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001115 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001116 BlockDriverState *bs1, *bs2;
1117 int64_t total_sectors1, total_sectors2;
1118 uint8_t *buf1 = NULL, *buf2 = NULL;
1119 int pnum1, pnum2;
1120 int allocated1, allocated2;
1121 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1122 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001123 int flags;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001124 int64_t total_sectors;
1125 int64_t sector_num = 0;
1126 int64_t nb_sectors;
1127 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001128 uint64_t progress_base;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001129 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001130 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001131
Max Reitz40055952014-07-22 22:58:42 +02001132 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001133 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001134 static const struct option long_options[] = {
1135 {"help", no_argument, 0, 'h'},
1136 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001137 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001138 {0, 0, 0, 0}
1139 };
1140 c = getopt_long(argc, argv, "hf:F:T:pqs",
1141 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001142 if (c == -1) {
1143 break;
1144 }
1145 switch (c) {
1146 case '?':
1147 case 'h':
1148 help();
1149 break;
1150 case 'f':
1151 fmt1 = optarg;
1152 break;
1153 case 'F':
1154 fmt2 = optarg;
1155 break;
Max Reitz40055952014-07-22 22:58:42 +02001156 case 'T':
1157 cache = optarg;
1158 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001159 case 'p':
1160 progress = true;
1161 break;
1162 case 'q':
1163 quiet = true;
1164 break;
1165 case 's':
1166 strict = true;
1167 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001168 case OPTION_OBJECT: {
1169 QemuOpts *opts;
1170 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1171 optarg, true);
1172 if (!opts) {
1173 ret = 2;
1174 goto out4;
1175 }
1176 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001177 case OPTION_IMAGE_OPTS:
1178 image_opts = true;
1179 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001180 }
1181 }
1182
1183 /* Progress is not shown in Quiet mode */
1184 if (quiet) {
1185 progress = false;
1186 }
1187
1188
Kevin Wolffc11eb22013-08-05 10:53:04 +02001189 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001190 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001191 }
1192 filename1 = argv[optind++];
1193 filename2 = argv[optind++];
1194
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001195 if (qemu_opts_foreach(&qemu_object_opts,
1196 user_creatable_add_opts_foreach,
1197 NULL, &local_err)) {
1198 error_report_err(local_err);
1199 ret = 2;
1200 goto out4;
1201 }
1202
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001203 /* Initialize before goto out */
1204 qemu_progress_init(progress, 2.0);
1205
Kevin Wolfe6996142016-03-15 13:03:11 +01001206 flags = BDRV_O_CACHE_WB;
Max Reitz40055952014-07-22 22:58:42 +02001207 ret = bdrv_parse_cache_flags(cache, &flags);
1208 if (ret < 0) {
1209 error_report("Invalid source cache option: %s", cache);
1210 ret = 2;
1211 goto out3;
1212 }
1213
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00001214 blk1 = img_open(image_opts, filename1, fmt1, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001215 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001216 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001217 goto out3;
1218 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001219
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00001220 blk2 = img_open(image_opts, filename2, fmt2, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001221 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001222 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001223 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001224 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001225 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001226 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001227
Max Reitzf1d3cd72015-02-05 13:58:18 -05001228 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1229 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1230 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001231 if (total_sectors1 < 0) {
1232 error_report("Can't get size of %s: %s",
1233 filename1, strerror(-total_sectors1));
1234 ret = 4;
1235 goto out;
1236 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001237 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001238 if (total_sectors2 < 0) {
1239 error_report("Can't get size of %s: %s",
1240 filename2, strerror(-total_sectors2));
1241 ret = 4;
1242 goto out;
1243 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001244 total_sectors = MIN(total_sectors1, total_sectors2);
1245 progress_base = MAX(total_sectors1, total_sectors2);
1246
1247 qemu_progress_print(0, 100);
1248
1249 if (strict && total_sectors1 != total_sectors2) {
1250 ret = 1;
1251 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1252 goto out;
1253 }
1254
1255 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001256 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001257 BlockDriverState *file;
1258
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001259 nb_sectors = sectors_to_process(total_sectors, sector_num);
1260 if (nb_sectors <= 0) {
1261 break;
1262 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001263 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1264 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001265 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001266 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001267 ret = 3;
1268 error_report("Sector allocation test failed for %s", filename1);
1269 goto out;
1270 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001271 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001272
Fam Zheng25ad8e62016-01-13 16:37:41 +08001273 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1274 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001275 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001276 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001277 ret = 3;
1278 error_report("Sector allocation test failed for %s", filename2);
1279 goto out;
1280 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001281 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1282 if (pnum1) {
1283 nb_sectors = MIN(nb_sectors, pnum1);
1284 }
1285 if (pnum2) {
1286 nb_sectors = MIN(nb_sectors, pnum2);
1287 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001288
Fam Zheng25ad8e62016-01-13 16:37:41 +08001289 if (strict) {
1290 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1291 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1292 ret = 1;
1293 qprintf(quiet, "Strict mode: Offset %" PRId64
1294 " block status mismatch!\n",
1295 sectors_to_bytes(sector_num));
1296 goto out;
1297 }
1298 }
1299 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1300 nb_sectors = MIN(pnum1, pnum2);
1301 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001302 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001303 ret = blk_read(blk1, sector_num, buf1, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001304 if (ret < 0) {
1305 error_report("Error while reading offset %" PRId64 " of %s:"
1306 " %s", sectors_to_bytes(sector_num), filename1,
1307 strerror(-ret));
1308 ret = 4;
1309 goto out;
1310 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001311 ret = blk_read(blk2, sector_num, buf2, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001312 if (ret < 0) {
1313 error_report("Error while reading offset %" PRId64
1314 " of %s: %s", sectors_to_bytes(sector_num),
1315 filename2, strerror(-ret));
1316 ret = 4;
1317 goto out;
1318 }
1319 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1320 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001321 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1322 sectors_to_bytes(
1323 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001324 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001325 goto out;
1326 }
1327 }
1328 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001329
1330 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001331 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001332 filename1, buf1, quiet);
1333 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001334 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001335 filename2, buf1, quiet);
1336 }
1337 if (ret) {
1338 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001339 error_report("Error while reading offset %" PRId64 ": %s",
1340 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001341 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001342 }
1343 goto out;
1344 }
1345 }
1346 sector_num += nb_sectors;
1347 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1348 }
1349
1350 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001351 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001352 int64_t total_sectors_over;
1353 const char *filename_over;
1354
1355 qprintf(quiet, "Warning: Image size mismatch!\n");
1356 if (total_sectors1 > total_sectors2) {
1357 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001358 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001359 filename_over = filename1;
1360 } else {
1361 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001362 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001363 filename_over = filename2;
1364 }
1365
1366 for (;;) {
1367 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1368 if (nb_sectors <= 0) {
1369 break;
1370 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001371 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001372 nb_sectors, &pnum);
1373 if (ret < 0) {
1374 ret = 3;
1375 error_report("Sector allocation test failed for %s",
1376 filename_over);
1377 goto out;
1378
1379 }
1380 nb_sectors = pnum;
1381 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001382 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001383 filename_over, buf1, quiet);
1384 if (ret) {
1385 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386 error_report("Error while reading offset %" PRId64
1387 " of %s: %s", sectors_to_bytes(sector_num),
1388 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001389 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001390 }
1391 goto out;
1392 }
1393 }
1394 sector_num += nb_sectors;
1395 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1396 }
1397 }
1398
1399 qprintf(quiet, "Images are identical.\n");
1400 ret = 0;
1401
1402out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001403 qemu_vfree(buf1);
1404 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001405 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001406out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001407 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001408out3:
1409 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001410out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001411 return ret;
1412}
1413
Kevin Wolf690c7302015-03-19 13:33:32 +01001414enum ImgConvertBlockStatus {
1415 BLK_DATA,
1416 BLK_ZERO,
1417 BLK_BACKING_FILE,
1418};
1419
1420typedef struct ImgConvertState {
1421 BlockBackend **src;
1422 int64_t *src_sectors;
1423 int src_cur, src_num;
1424 int64_t src_cur_offset;
1425 int64_t total_sectors;
1426 int64_t allocated_sectors;
1427 enum ImgConvertBlockStatus status;
1428 int64_t sector_next_status;
1429 BlockBackend *target;
1430 bool has_zero_init;
1431 bool compressed;
1432 bool target_has_backing;
1433 int min_sparse;
1434 size_t cluster_sectors;
1435 size_t buf_sectors;
1436} ImgConvertState;
1437
1438static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1439{
1440 assert(sector_num >= s->src_cur_offset);
1441 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1442 s->src_cur_offset += s->src_sectors[s->src_cur];
1443 s->src_cur++;
1444 assert(s->src_cur < s->src_num);
1445 }
1446}
1447
1448static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1449{
1450 int64_t ret;
1451 int n;
1452
1453 convert_select_part(s, sector_num);
1454
1455 assert(s->total_sectors > sector_num);
1456 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1457
1458 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001459 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001460 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1461 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001462 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001463 if (ret < 0) {
1464 return ret;
1465 }
1466
1467 if (ret & BDRV_BLOCK_ZERO) {
1468 s->status = BLK_ZERO;
1469 } else if (ret & BDRV_BLOCK_DATA) {
1470 s->status = BLK_DATA;
1471 } else if (!s->target_has_backing) {
1472 /* Without a target backing file we must copy over the contents of
1473 * the backing file as well. */
1474 /* TODO Check block status of the backing file chain to avoid
1475 * needlessly reading zeroes and limiting the iteration to the
1476 * buffer size */
1477 s->status = BLK_DATA;
1478 } else {
1479 s->status = BLK_BACKING_FILE;
1480 }
1481
1482 s->sector_next_status = sector_num + n;
1483 }
1484
1485 n = MIN(n, s->sector_next_status - sector_num);
1486 if (s->status == BLK_DATA) {
1487 n = MIN(n, s->buf_sectors);
1488 }
1489
1490 /* We need to write complete clusters for compressed images, so if an
1491 * unallocated area is shorter than that, we must consider the whole
1492 * cluster allocated. */
1493 if (s->compressed) {
1494 if (n < s->cluster_sectors) {
1495 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1496 s->status = BLK_DATA;
1497 } else {
1498 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1499 }
1500 }
1501
1502 return n;
1503}
1504
1505static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1506 uint8_t *buf)
1507{
1508 int n;
1509 int ret;
1510
1511 if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) {
1512 return 0;
1513 }
1514
1515 assert(nb_sectors <= s->buf_sectors);
1516 while (nb_sectors > 0) {
1517 BlockBackend *blk;
1518 int64_t bs_sectors;
1519
1520 /* In the case of compression with multiple source files, we can get a
1521 * nb_sectors that spreads into the next part. So we must be able to
1522 * read across multiple BDSes for one convert_read() call. */
1523 convert_select_part(s, sector_num);
1524 blk = s->src[s->src_cur];
1525 bs_sectors = s->src_sectors[s->src_cur];
1526
1527 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
1528 ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n);
1529 if (ret < 0) {
1530 return ret;
1531 }
1532
1533 sector_num += n;
1534 nb_sectors -= n;
1535 buf += n * BDRV_SECTOR_SIZE;
1536 }
1537
1538 return 0;
1539}
1540
1541static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1542 const uint8_t *buf)
1543{
1544 int ret;
1545
1546 while (nb_sectors > 0) {
1547 int n = nb_sectors;
1548
1549 switch (s->status) {
1550 case BLK_BACKING_FILE:
1551 /* If we have a backing file, leave clusters unallocated that are
1552 * unallocated in the source image, so that the backing file is
1553 * visible at the respective offset. */
1554 assert(s->target_has_backing);
1555 break;
1556
1557 case BLK_DATA:
1558 /* We must always write compressed clusters as a whole, so don't
1559 * try to find zeroed parts in the buffer. We can only save the
1560 * write if the buffer is completely zeroed and we're allowed to
1561 * keep the target sparse. */
1562 if (s->compressed) {
1563 if (s->has_zero_init && s->min_sparse &&
1564 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1565 {
1566 assert(!s->target_has_backing);
1567 break;
1568 }
1569
1570 ret = blk_write_compressed(s->target, sector_num, buf, n);
1571 if (ret < 0) {
1572 return ret;
1573 }
1574 break;
1575 }
1576
1577 /* If there is real non-zero data or we're told to keep the target
1578 * fully allocated (-S 0), we must write it. Otherwise we can treat
1579 * it as zero sectors. */
1580 if (!s->min_sparse ||
1581 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1582 {
1583 ret = blk_write(s->target, sector_num, buf, n);
1584 if (ret < 0) {
1585 return ret;
1586 }
1587 break;
1588 }
1589 /* fall-through */
1590
1591 case BLK_ZERO:
1592 if (s->has_zero_init) {
1593 break;
1594 }
1595 ret = blk_write_zeroes(s->target, sector_num, n, 0);
1596 if (ret < 0) {
1597 return ret;
1598 }
1599 break;
1600 }
1601
1602 sector_num += n;
1603 nb_sectors -= n;
1604 buf += n * BDRV_SECTOR_SIZE;
1605 }
1606
1607 return 0;
1608}
1609
1610static int convert_do_copy(ImgConvertState *s)
1611{
1612 uint8_t *buf = NULL;
1613 int64_t sector_num, allocated_done;
1614 int ret;
1615 int n;
1616
1617 /* Check whether we have zero initialisation or can get it efficiently */
1618 s->has_zero_init = s->min_sparse && !s->target_has_backing
1619 ? bdrv_has_zero_init(blk_bs(s->target))
1620 : false;
1621
1622 if (!s->has_zero_init && !s->target_has_backing &&
1623 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1624 {
1625 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1626 if (ret == 0) {
1627 s->has_zero_init = true;
1628 }
1629 }
1630
1631 /* Allocate buffer for copied data. For compressed images, only one cluster
1632 * can be copied at a time. */
1633 if (s->compressed) {
1634 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1635 error_report("invalid cluster size");
1636 ret = -EINVAL;
1637 goto fail;
1638 }
1639 s->buf_sectors = s->cluster_sectors;
1640 }
1641 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1642
1643 /* Calculate allocated sectors for progress */
1644 s->allocated_sectors = 0;
1645 sector_num = 0;
1646 while (sector_num < s->total_sectors) {
1647 n = convert_iteration_sectors(s, sector_num);
1648 if (n < 0) {
1649 ret = n;
1650 goto fail;
1651 }
1652 if (s->status == BLK_DATA) {
1653 s->allocated_sectors += n;
1654 }
1655 sector_num += n;
1656 }
1657
1658 /* Do the copy */
1659 s->src_cur = 0;
1660 s->src_cur_offset = 0;
1661 s->sector_next_status = 0;
1662
1663 sector_num = 0;
1664 allocated_done = 0;
1665
1666 while (sector_num < s->total_sectors) {
1667 n = convert_iteration_sectors(s, sector_num);
1668 if (n < 0) {
1669 ret = n;
1670 goto fail;
1671 }
1672 if (s->status == BLK_DATA) {
1673 allocated_done += n;
1674 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1675 0);
1676 }
1677
1678 ret = convert_read(s, sector_num, n, buf);
1679 if (ret < 0) {
1680 error_report("error while reading sector %" PRId64
1681 ": %s", sector_num, strerror(-ret));
1682 goto fail;
1683 }
1684
1685 ret = convert_write(s, sector_num, n, buf);
1686 if (ret < 0) {
1687 error_report("error while writing sector %" PRId64
1688 ": %s", sector_num, strerror(-ret));
1689 goto fail;
1690 }
1691
1692 sector_num += n;
1693 }
1694
1695 if (s->compressed) {
1696 /* signal EOF to align */
1697 ret = blk_write_compressed(s->target, 0, NULL, 0);
1698 if (ret < 0) {
1699 goto fail;
1700 }
1701 }
1702
1703 ret = 0;
1704fail:
1705 qemu_vfree(buf);
1706 return ret;
1707}
1708
bellardea2384d2004-08-01 21:59:26 +00001709static int img_convert(int argc, char **argv)
1710{
Kevin Wolf690c7302015-03-19 13:33:32 +01001711 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001712 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001713 int progress = 0, flags, src_flags;
1714 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001715 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001716 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001717 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001718 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001719 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001720 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001721 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001722 QemuOpts *opts = NULL;
1723 QemuOptsList *create_opts = NULL;
1724 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001725 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001726 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001727 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001728 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001729 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001730 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001731 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001732 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001733
1734 fmt = NULL;
1735 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001736 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001737 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001738 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001739 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001740 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001741 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001742 static const struct option long_options[] = {
1743 {"help", no_argument, 0, 'h'},
1744 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001745 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001746 {0, 0, 0, 0}
1747 };
1748 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1749 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001750 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001751 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001752 }
bellardea2384d2004-08-01 21:59:26 +00001753 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001754 case '?':
bellardea2384d2004-08-01 21:59:26 +00001755 case 'h':
1756 help();
1757 break;
1758 case 'f':
1759 fmt = optarg;
1760 break;
1761 case 'O':
1762 out_fmt = optarg;
1763 break;
thsf58c7b32008-06-05 21:53:49 +00001764 case 'B':
1765 out_baseimg = optarg;
1766 break;
bellardea2384d2004-08-01 21:59:26 +00001767 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001768 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001769 break;
1770 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001771 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001772 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001773 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001774 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001775 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001776 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001777 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001778 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001779 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001780 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001781 if (!is_valid_option_list(optarg)) {
1782 error_report("Invalid option list: %s", optarg);
1783 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001784 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001785 }
1786 if (!options) {
1787 options = g_strdup(optarg);
1788 } else {
1789 char *old_options = options;
1790 options = g_strdup_printf("%s,%s", options, optarg);
1791 g_free(old_options);
1792 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001793 break;
edison51ef6722010-09-21 19:58:41 -07001794 case 's':
1795 snapshot_name = optarg;
1796 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001797 case 'l':
1798 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001799 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1800 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001801 if (!sn_opts) {
1802 error_report("Failed in parsing snapshot param '%s'",
1803 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001804 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001805 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001806 }
1807 } else {
1808 snapshot_name = optarg;
1809 }
1810 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001811 case 'S':
1812 {
1813 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001814 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001815 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001816 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001817 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001818 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001819 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001820 }
1821
1822 min_sparse = sval / BDRV_SECTOR_SIZE;
1823 break;
1824 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001825 case 'p':
1826 progress = 1;
1827 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001828 case 't':
1829 cache = optarg;
1830 break;
Max Reitz40055952014-07-22 22:58:42 +02001831 case 'T':
1832 src_cache = optarg;
1833 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001834 case 'q':
1835 quiet = true;
1836 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001837 case 'n':
1838 skip_create = 1;
1839 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001840 case OPTION_OBJECT:
1841 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1842 optarg, true);
1843 if (!opts) {
1844 goto fail_getopt;
1845 }
1846 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001847 case OPTION_IMAGE_OPTS:
1848 image_opts = true;
1849 break;
bellardea2384d2004-08-01 21:59:26 +00001850 }
1851 }
ths3b46e622007-09-17 08:09:54 +00001852
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001853 if (qemu_opts_foreach(&qemu_object_opts,
1854 user_creatable_add_opts_foreach,
1855 NULL, &local_err)) {
1856 error_report_err(local_err);
1857 goto fail_getopt;
1858 }
1859
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001860 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001861 if (quiet) {
1862 progress = 0;
1863 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001864 qemu_progress_init(progress, 1.0);
1865
balrog926c2d22007-10-31 01:11:44 +00001866 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001867 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001868
Kevin Wolf2dc83282014-02-21 16:24:05 +01001869 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001870 ret = print_block_option_help(out_filename, out_fmt);
1871 goto out;
1872 }
1873
bellardea2384d2004-08-01 21:59:26 +00001874 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001875 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001876 }
bellardea2384d2004-08-01 21:59:26 +00001877
thsf58c7b32008-06-05 21:53:49 +00001878
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001879 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001880 error_report("-B makes no sense when concatenating multiple input "
1881 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001882 ret = -1;
1883 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001884 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001885
Kevin Wolfe6996142016-03-15 13:03:11 +01001886 src_flags = BDRV_O_CACHE_WB;
Max Reitz40055952014-07-22 22:58:42 +02001887 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
1888 if (ret < 0) {
1889 error_report("Invalid source cache option: %s", src_cache);
1890 goto out;
1891 }
1892
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001893 qemu_progress_print(0, 100);
1894
Markus Armbruster26f54e92014-10-07 13:59:04 +02001895 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001896 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001897 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001898
1899 total_sectors = 0;
1900 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001901 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00001902 fmt, src_flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001903 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001904 ret = -1;
1905 goto out;
1906 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001907 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001908 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001909 if (bs_sectors[bs_i] < 0) {
1910 error_report("Could not get size of %s: %s",
1911 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1912 ret = -1;
1913 goto out;
1914 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001915 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001916 }
bellardea2384d2004-08-01 21:59:26 +00001917
Wenchao Xiaef806542013-12-04 17:10:57 +08001918 if (sn_opts) {
1919 ret = bdrv_snapshot_load_tmp(bs[0],
1920 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1921 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1922 &local_err);
1923 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001924 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001925 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001926 ret = -1;
1927 goto out;
1928 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001929
1930 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001931 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001932 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001933 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001934 ret = -1;
1935 goto out;
edison51ef6722010-09-21 19:58:41 -07001936 }
1937
Kevin Wolfefa84d42009-05-18 16:42:12 +02001938 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001939 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001940 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001941 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001942 ret = -1;
1943 goto out;
1944 }
balrog926c2d22007-10-31 01:11:44 +00001945
Max Reitzb65a5e12015-02-05 13:58:12 -05001946 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001947 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001948 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001949 ret = -1;
1950 goto out;
1951 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001952
Max Reitz2e024cd2015-02-11 09:58:46 -05001953 if (!skip_create) {
1954 if (!drv->create_opts) {
1955 error_report("Format driver '%s' does not support image creation",
1956 drv->format_name);
1957 ret = -1;
1958 goto out;
1959 }
Max Reitzf75613c2014-12-02 18:32:46 +01001960
Max Reitz2e024cd2015-02-11 09:58:46 -05001961 if (!proto_drv->create_opts) {
1962 error_report("Protocol driver '%s' does not support image creation",
1963 proto_drv->format_name);
1964 ret = -1;
1965 goto out;
1966 }
Max Reitzf75613c2014-12-02 18:32:46 +01001967
Max Reitz2e024cd2015-02-11 09:58:46 -05001968 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1969 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001970
Max Reitz2e024cd2015-02-11 09:58:46 -05001971 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001972 if (options) {
1973 qemu_opts_do_parse(opts, options, NULL, &local_err);
1974 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01001975 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001976 ret = -1;
1977 goto out;
1978 }
Max Reitz2e024cd2015-02-11 09:58:46 -05001979 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001980
Markus Armbruster39101f22015-02-12 16:46:36 +01001981 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
1982 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05001983 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
1984 if (ret < 0) {
1985 goto out;
1986 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001987 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001988
Kevin Wolfa18953f2010-10-14 15:46:04 +02001989 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001990 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001991 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001992 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001993 }
1994
Kevin Wolfefa84d42009-05-18 16:42:12 +02001995 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001996 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001997 bool encryption =
1998 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
1999 const char *preallocation =
2000 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002001
2002 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002003 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002004 ret = -1;
2005 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002006 }
2007
Chunyan Liu83d05212014-06-05 17:20:51 +08002008 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002009 error_report("Compression and encryption not supported at "
2010 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002011 ret = -1;
2012 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002013 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002014
Chunyan Liu83d05212014-06-05 17:20:51 +08002015 if (preallocation
2016 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002017 {
2018 error_report("Compression and preallocation not supported at "
2019 "the same time");
2020 ret = -1;
2021 goto out;
2022 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002023 }
2024
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002025 if (!skip_create) {
2026 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002027 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002028 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002029 error_reportf_err(local_err, "%s: error while converting %s: ",
2030 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002031 goto out;
bellardea2384d2004-08-01 21:59:26 +00002032 }
2033 }
ths3b46e622007-09-17 08:09:54 +00002034
Peter Lieven5a37b602013-10-24 12:07:06 +02002035 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002036 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002037 if (ret < 0) {
2038 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002039 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002040 }
2041
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002042 /* XXX we should allow --image-opts to trigger use of
2043 * img_open() here, but then we have trouble with
2044 * the bdrv_create() call which takes different params.
2045 * Not critical right now, so fix can wait...
2046 */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002047 out_blk = img_open_file(out_filename, out_fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002048 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002049 ret = -1;
2050 goto out;
2051 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002052 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002053
Peter Lievenf2521c92013-11-27 11:07:06 +01002054 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
2055 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2056 * as maximum. */
2057 bufsectors = MIN(32768,
2058 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
2059 out_bs->bl.discard_alignment))
2060 );
2061
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002062 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002063 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002064 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002065 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002066 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002067 ret = -1;
2068 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002069 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002070 error_report("output file is smaller than input file");
2071 ret = -1;
2072 goto out;
2073 }
2074 }
2075
Peter Lieven24f833c2013-11-27 11:07:07 +01002076 cluster_sectors = 0;
2077 ret = bdrv_get_info(out_bs, &bdi);
2078 if (ret < 0) {
2079 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002080 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002081 goto out;
2082 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002083 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002084 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002085 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2086 }
2087
Kevin Wolf690c7302015-03-19 13:33:32 +01002088 state = (ImgConvertState) {
2089 .src = blk,
2090 .src_sectors = bs_sectors,
2091 .src_num = bs_n,
2092 .total_sectors = total_sectors,
2093 .target = out_blk,
2094 .compressed = compress,
2095 .target_has_backing = (bool) out_baseimg,
2096 .min_sparse = min_sparse,
2097 .cluster_sectors = cluster_sectors,
2098 .buf_sectors = bufsectors,
2099 };
2100 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002101
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002102out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002103 if (!ret) {
2104 qemu_progress_print(100, 0);
2105 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002106 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002107 qemu_opts_del(opts);
2108 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002109 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002110 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002111 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002112 if (blk) {
2113 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2114 blk_unref(blk[bs_i]);
2115 }
2116 g_free(blk);
2117 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002118 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002119fail_getopt:
2120 g_free(options);
2121
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002122 if (ret) {
2123 return 1;
2124 }
bellardea2384d2004-08-01 21:59:26 +00002125 return 0;
2126}
2127
bellard57d1a2b2004-08-03 21:15:11 +00002128
bellardfaea38e2006-08-05 21:31:00 +00002129static void dump_snapshots(BlockDriverState *bs)
2130{
2131 QEMUSnapshotInfo *sn_tab, *sn;
2132 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002133
2134 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2135 if (nb_sns <= 0)
2136 return;
2137 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002138 bdrv_snapshot_dump(fprintf, stdout, NULL);
2139 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002140 for(i = 0; i < nb_sns; i++) {
2141 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002142 bdrv_snapshot_dump(fprintf, stdout, sn);
2143 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002144 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002145 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002146}
2147
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002148static void dump_json_image_info_list(ImageInfoList *list)
2149{
Markus Armbruster4399c432014-04-25 16:50:32 +02002150 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002151 QString *str;
2152 QmpOutputVisitor *ov = qmp_output_visitor_new();
2153 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002154 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2155 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002156 obj = qmp_output_get_qobject(ov);
2157 str = qobject_to_json_pretty(obj);
2158 assert(str != NULL);
2159 printf("%s\n", qstring_get_str(str));
2160 qobject_decref(obj);
2161 qmp_output_visitor_cleanup(ov);
2162 QDECREF(str);
2163}
2164
Benoît Canetc054b3f2012-09-05 13:09:02 +02002165static void dump_json_image_info(ImageInfo *info)
2166{
Markus Armbruster4399c432014-04-25 16:50:32 +02002167 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002168 QString *str;
2169 QmpOutputVisitor *ov = qmp_output_visitor_new();
2170 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002171 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002172 obj = qmp_output_get_qobject(ov);
2173 str = qobject_to_json_pretty(obj);
2174 assert(str != NULL);
2175 printf("%s\n", qstring_get_str(str));
2176 qobject_decref(obj);
2177 qmp_output_visitor_cleanup(ov);
2178 QDECREF(str);
2179}
2180
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002181static void dump_human_image_info_list(ImageInfoList *list)
2182{
2183 ImageInfoList *elem;
2184 bool delim = false;
2185
2186 for (elem = list; elem; elem = elem->next) {
2187 if (delim) {
2188 printf("\n");
2189 }
2190 delim = true;
2191
Wenchao Xia5b917042013-05-25 11:09:45 +08002192 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002193 }
2194}
2195
2196static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2197{
2198 return strcmp(a, b) == 0;
2199}
2200
2201/**
2202 * Open an image file chain and return an ImageInfoList
2203 *
2204 * @filename: topmost image filename
2205 * @fmt: topmost image format (may be NULL to autodetect)
2206 * @chain: true - enumerate entire backing file chain
2207 * false - only topmost image file
2208 *
2209 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2210 * image file. If there was an error a message will have been printed to
2211 * stderr.
2212 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002213static ImageInfoList *collect_image_info_list(bool image_opts,
2214 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002215 const char *fmt,
2216 bool chain)
2217{
2218 ImageInfoList *head = NULL;
2219 ImageInfoList **last = &head;
2220 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002221 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002222
2223 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2224
2225 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002226 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002227 BlockDriverState *bs;
2228 ImageInfo *info;
2229 ImageInfoList *elem;
2230
2231 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2232 error_report("Backing file '%s' creates an infinite loop.",
2233 filename);
2234 goto err;
2235 }
2236 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2237
Max Reitzefaa7c42016-03-16 19:54:38 +01002238 blk = img_open(image_opts, filename, fmt,
Kevin Wolfe6996142016-03-15 13:03:11 +01002239 BDRV_O_CACHE_WB | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002240 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002241 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002242 goto err;
2243 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002244 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002245
Wenchao Xia43526ec2013-06-06 12:27:58 +08002246 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002247 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002248 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002249 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002250 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002251 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002252
2253 elem = g_new0(ImageInfoList, 1);
2254 elem->value = info;
2255 *last = elem;
2256 last = &elem->next;
2257
Markus Armbruster26f54e92014-10-07 13:59:04 +02002258 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002259
2260 filename = fmt = NULL;
2261 if (chain) {
2262 if (info->has_full_backing_filename) {
2263 filename = info->full_backing_filename;
2264 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002265 error_report("Could not determine absolute backing filename,"
2266 " but backing filename '%s' present",
2267 info->backing_filename);
2268 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002269 }
2270 if (info->has_backing_filename_format) {
2271 fmt = info->backing_filename_format;
2272 }
2273 }
2274 }
2275 g_hash_table_destroy(filenames);
2276 return head;
2277
2278err:
2279 qapi_free_ImageInfoList(head);
2280 g_hash_table_destroy(filenames);
2281 return NULL;
2282}
2283
Benoît Canetc054b3f2012-09-05 13:09:02 +02002284static int img_info(int argc, char **argv)
2285{
2286 int c;
2287 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002288 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002289 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002290 ImageInfoList *list;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002291 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002292 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002293
bellardea2384d2004-08-01 21:59:26 +00002294 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002295 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002296 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002297 int option_index = 0;
2298 static const struct option long_options[] = {
2299 {"help", no_argument, 0, 'h'},
2300 {"format", required_argument, 0, 'f'},
2301 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002302 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002303 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002304 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002305 {0, 0, 0, 0}
2306 };
2307 c = getopt_long(argc, argv, "f:h",
2308 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002309 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002310 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002311 }
bellardea2384d2004-08-01 21:59:26 +00002312 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002313 case '?':
bellardea2384d2004-08-01 21:59:26 +00002314 case 'h':
2315 help();
2316 break;
2317 case 'f':
2318 fmt = optarg;
2319 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002320 case OPTION_OUTPUT:
2321 output = optarg;
2322 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002323 case OPTION_BACKING_CHAIN:
2324 chain = true;
2325 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002326 case OPTION_OBJECT: {
2327 QemuOpts *opts;
2328 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2329 optarg, true);
2330 if (!opts) {
2331 return 1;
2332 }
2333 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002334 case OPTION_IMAGE_OPTS:
2335 image_opts = true;
2336 break;
bellardea2384d2004-08-01 21:59:26 +00002337 }
2338 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002339 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002340 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002341 }
bellardea2384d2004-08-01 21:59:26 +00002342 filename = argv[optind++];
2343
Benoît Canetc054b3f2012-09-05 13:09:02 +02002344 if (output && !strcmp(output, "json")) {
2345 output_format = OFORMAT_JSON;
2346 } else if (output && !strcmp(output, "human")) {
2347 output_format = OFORMAT_HUMAN;
2348 } else if (output) {
2349 error_report("--output must be used with human or json as argument.");
2350 return 1;
2351 }
2352
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002353 if (qemu_opts_foreach(&qemu_object_opts,
2354 user_creatable_add_opts_foreach,
2355 NULL, &local_err)) {
2356 error_report_err(local_err);
2357 return 1;
2358 }
2359
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002360 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002361 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002362 return 1;
2363 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002364
Benoît Canetc054b3f2012-09-05 13:09:02 +02002365 switch (output_format) {
2366 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002367 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002368 break;
2369 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002370 if (chain) {
2371 dump_json_image_info_list(list);
2372 } else {
2373 dump_json_image_info(list->value);
2374 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002375 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002376 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002377
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002378 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002379 return 0;
2380}
2381
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002382static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2383 MapEntry *next)
2384{
2385 switch (output_format) {
2386 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002387 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002388 error_report("File contains external, encrypted or compressed clusters.");
2389 exit(1);
2390 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002391 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002392 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002393 e->start, e->length,
2394 e->has_offset ? e->offset : 0,
2395 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002396 }
2397 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2398 * Modify the flags here to allow more coalescing.
2399 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002400 if (next && (!next->data || next->zero)) {
2401 next->data = false;
2402 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002403 }
2404 break;
2405 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002406 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2407 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002408 (e->start == 0 ? "[" : ",\n"),
2409 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002410 e->zero ? "true" : "false",
2411 e->data ? "true" : "false");
2412 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002413 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002414 }
2415 putchar('}');
2416
2417 if (!next) {
2418 printf("]\n");
2419 }
2420 break;
2421 }
2422}
2423
2424static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2425 int nb_sectors, MapEntry *e)
2426{
2427 int64_t ret;
2428 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002429 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002430 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002431
2432 /* As an optimization, we could cache the current range of unallocated
2433 * clusters in each file of the chain, and avoid querying the same
2434 * range repeatedly.
2435 */
2436
2437 depth = 0;
2438 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002439 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2440 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002441 if (ret < 0) {
2442 return ret;
2443 }
2444 assert(nb_sectors);
2445 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2446 break;
2447 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002448 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002449 if (bs == NULL) {
2450 ret = 0;
2451 break;
2452 }
2453
2454 depth++;
2455 }
2456
John Snow28756452016-02-05 13:12:33 -05002457 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2458
2459 *e = (MapEntry) {
2460 .start = sector_num * BDRV_SECTOR_SIZE,
2461 .length = nb_sectors * BDRV_SECTOR_SIZE,
2462 .data = !!(ret & BDRV_BLOCK_DATA),
2463 .zero = !!(ret & BDRV_BLOCK_ZERO),
2464 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2465 .has_offset = has_offset,
2466 .depth = depth,
2467 .has_filename = file && has_offset,
2468 .filename = file && has_offset ? file->filename : NULL,
2469 };
2470
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002471 return 0;
2472}
2473
Fam Zheng16b0d552016-01-26 11:59:02 +08002474static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2475{
2476 if (curr->length == 0) {
2477 return false;
2478 }
2479 if (curr->zero != next->zero ||
2480 curr->data != next->data ||
2481 curr->depth != next->depth ||
2482 curr->has_filename != next->has_filename ||
2483 curr->has_offset != next->has_offset) {
2484 return false;
2485 }
2486 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2487 return false;
2488 }
2489 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2490 return false;
2491 }
2492 return true;
2493}
2494
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002495static int img_map(int argc, char **argv)
2496{
2497 int c;
2498 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002499 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002500 BlockDriverState *bs;
2501 const char *filename, *fmt, *output;
2502 int64_t length;
2503 MapEntry curr = { .length = 0 }, next;
2504 int ret = 0;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002505 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002506 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002507
2508 fmt = NULL;
2509 output = NULL;
2510 for (;;) {
2511 int option_index = 0;
2512 static const struct option long_options[] = {
2513 {"help", no_argument, 0, 'h'},
2514 {"format", required_argument, 0, 'f'},
2515 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002516 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002517 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002518 {0, 0, 0, 0}
2519 };
2520 c = getopt_long(argc, argv, "f:h",
2521 long_options, &option_index);
2522 if (c == -1) {
2523 break;
2524 }
2525 switch (c) {
2526 case '?':
2527 case 'h':
2528 help();
2529 break;
2530 case 'f':
2531 fmt = optarg;
2532 break;
2533 case OPTION_OUTPUT:
2534 output = optarg;
2535 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002536 case OPTION_OBJECT: {
2537 QemuOpts *opts;
2538 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2539 optarg, true);
2540 if (!opts) {
2541 return 1;
2542 }
2543 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002544 case OPTION_IMAGE_OPTS:
2545 image_opts = true;
2546 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002547 }
2548 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002549 if (optind != argc - 1) {
2550 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002551 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002552 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002553
2554 if (output && !strcmp(output, "json")) {
2555 output_format = OFORMAT_JSON;
2556 } else if (output && !strcmp(output, "human")) {
2557 output_format = OFORMAT_HUMAN;
2558 } else if (output) {
2559 error_report("--output must be used with human or json as argument.");
2560 return 1;
2561 }
2562
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002563 if (qemu_opts_foreach(&qemu_object_opts,
2564 user_creatable_add_opts_foreach,
2565 NULL, &local_err)) {
2566 error_report_err(local_err);
2567 return 1;
2568 }
2569
Kevin Wolfe6996142016-03-15 13:03:11 +01002570 blk = img_open(image_opts, filename, fmt, BDRV_O_CACHE_WB, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002571 if (!blk) {
2572 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002573 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002574 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002575
2576 if (output_format == OFORMAT_HUMAN) {
2577 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2578 }
2579
Max Reitzf1d3cd72015-02-05 13:58:18 -05002580 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002581 while (curr.start + curr.length < length) {
2582 int64_t nsectors_left;
2583 int64_t sector_num;
2584 int n;
2585
2586 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2587
2588 /* Probe up to 1 GiB at a time. */
2589 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2590 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2591 ret = get_block_status(bs, sector_num, n, &next);
2592
2593 if (ret < 0) {
2594 error_report("Could not read file metadata: %s", strerror(-ret));
2595 goto out;
2596 }
2597
Fam Zheng16b0d552016-01-26 11:59:02 +08002598 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002599 curr.length += next.length;
2600 continue;
2601 }
2602
2603 if (curr.length > 0) {
2604 dump_map_entry(output_format, &curr, &next);
2605 }
2606 curr = next;
2607 }
2608
2609 dump_map_entry(output_format, &curr, NULL);
2610
2611out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002612 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002613 return ret < 0;
2614}
2615
aliguorif7b4a942009-01-07 17:40:15 +00002616#define SNAPSHOT_LIST 1
2617#define SNAPSHOT_CREATE 2
2618#define SNAPSHOT_APPLY 3
2619#define SNAPSHOT_DELETE 4
2620
Stuart Brady153859b2009-06-07 00:42:17 +01002621static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002622{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002623 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002624 BlockDriverState *bs;
2625 QEMUSnapshotInfo sn;
2626 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002627 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002628 int action = 0;
2629 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002630 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002631 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002632 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002633
Kevin Wolfe6996142016-03-15 13:03:11 +01002634 bdrv_oflags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002635 /* Parse commandline parameters */
2636 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002637 static const struct option long_options[] = {
2638 {"help", no_argument, 0, 'h'},
2639 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002640 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002641 {0, 0, 0, 0}
2642 };
2643 c = getopt_long(argc, argv, "la:c:d:hq",
2644 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002645 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002646 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002647 }
aliguorif7b4a942009-01-07 17:40:15 +00002648 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002649 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002650 case 'h':
2651 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002652 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002653 case 'l':
2654 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002655 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002656 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002657 }
2658 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002659 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002660 break;
2661 case 'a':
2662 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002663 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002664 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002665 }
2666 action = SNAPSHOT_APPLY;
2667 snapshot_name = optarg;
2668 break;
2669 case 'c':
2670 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002671 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002672 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002673 }
2674 action = SNAPSHOT_CREATE;
2675 snapshot_name = optarg;
2676 break;
2677 case 'd':
2678 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002679 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002680 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002681 }
2682 action = SNAPSHOT_DELETE;
2683 snapshot_name = optarg;
2684 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002685 case 'q':
2686 quiet = true;
2687 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002688 case OPTION_OBJECT: {
2689 QemuOpts *opts;
2690 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2691 optarg, true);
2692 if (!opts) {
2693 return 1;
2694 }
2695 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002696 case OPTION_IMAGE_OPTS:
2697 image_opts = true;
2698 break;
aliguorif7b4a942009-01-07 17:40:15 +00002699 }
2700 }
2701
Kevin Wolffc11eb22013-08-05 10:53:04 +02002702 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002703 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002704 }
aliguorif7b4a942009-01-07 17:40:15 +00002705 filename = argv[optind++];
2706
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002707 if (qemu_opts_foreach(&qemu_object_opts,
2708 user_creatable_add_opts_foreach,
2709 NULL, &err)) {
2710 error_report_err(err);
2711 return 1;
2712 }
2713
aliguorif7b4a942009-01-07 17:40:15 +00002714 /* Open the image */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002715 blk = img_open(image_opts, filename, NULL, bdrv_oflags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002716 if (!blk) {
2717 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002718 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002719 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002720
2721 /* Perform the requested action */
2722 switch(action) {
2723 case SNAPSHOT_LIST:
2724 dump_snapshots(bs);
2725 break;
2726
2727 case SNAPSHOT_CREATE:
2728 memset(&sn, 0, sizeof(sn));
2729 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2730
2731 qemu_gettimeofday(&tv);
2732 sn.date_sec = tv.tv_sec;
2733 sn.date_nsec = tv.tv_usec * 1000;
2734
2735 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002736 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002737 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002738 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002739 }
aliguorif7b4a942009-01-07 17:40:15 +00002740 break;
2741
2742 case SNAPSHOT_APPLY:
2743 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002744 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002745 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002746 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002747 }
aliguorif7b4a942009-01-07 17:40:15 +00002748 break;
2749
2750 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002751 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002752 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002753 error_reportf_err(err, "Could not delete snapshot '%s': ",
2754 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002755 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002756 }
aliguorif7b4a942009-01-07 17:40:15 +00002757 break;
2758 }
2759
2760 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002761 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002762 if (ret) {
2763 return 1;
2764 }
Stuart Brady153859b2009-06-07 00:42:17 +01002765 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002766}
2767
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002768static int img_rebase(int argc, char **argv)
2769{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002770 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002771 uint8_t *buf_old = NULL;
2772 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002773 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002774 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002775 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2776 int c, flags, src_flags, ret;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002777 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002778 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002779 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002780 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002781 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002782
2783 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002784 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002785 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002786 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002787 out_baseimg = NULL;
2788 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002789 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002790 static const struct option long_options[] = {
2791 {"help", no_argument, 0, 'h'},
2792 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002793 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002794 {0, 0, 0, 0}
2795 };
2796 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2797 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002798 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002799 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002800 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002801 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002802 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002803 case 'h':
2804 help();
2805 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002806 case 'f':
2807 fmt = optarg;
2808 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002809 case 'F':
2810 out_basefmt = optarg;
2811 break;
2812 case 'b':
2813 out_baseimg = optarg;
2814 break;
2815 case 'u':
2816 unsafe = 1;
2817 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002818 case 'p':
2819 progress = 1;
2820 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002821 case 't':
2822 cache = optarg;
2823 break;
Max Reitz40055952014-07-22 22:58:42 +02002824 case 'T':
2825 src_cache = optarg;
2826 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002827 case 'q':
2828 quiet = true;
2829 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002830 case OPTION_OBJECT: {
2831 QemuOpts *opts;
2832 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2833 optarg, true);
2834 if (!opts) {
2835 return 1;
2836 }
2837 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002838 case OPTION_IMAGE_OPTS:
2839 image_opts = true;
2840 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002841 }
2842 }
2843
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002844 if (quiet) {
2845 progress = 0;
2846 }
2847
Fam Zhengac1307a2014-04-22 13:36:11 +08002848 if (optind != argc - 1) {
2849 error_exit("Expecting one image file name");
2850 }
2851 if (!unsafe && !out_baseimg) {
2852 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002853 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002854 filename = argv[optind++];
2855
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002856 if (qemu_opts_foreach(&qemu_object_opts,
2857 user_creatable_add_opts_foreach,
2858 NULL, &local_err)) {
2859 error_report_err(local_err);
2860 return 1;
2861 }
2862
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002863 qemu_progress_init(progress, 2.0);
2864 qemu_progress_print(0, 100);
2865
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002866 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002867 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002868 if (ret < 0) {
2869 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002870 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002871 }
2872
Kevin Wolfe6996142016-03-15 13:03:11 +01002873 src_flags = BDRV_O_CACHE_WB;
Max Reitz40055952014-07-22 22:58:42 +02002874 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
2875 if (ret < 0) {
2876 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002877 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002878 }
2879
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002880 /*
2881 * Open the images.
2882 *
2883 * Ignore the old backing file for unsafe rebase in case we want to correct
2884 * the reference to a renamed or moved backing file.
2885 */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002886 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002887 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002888 ret = -1;
2889 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002890 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002891 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002892
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002893 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002894 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002895 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002896 ret = -1;
2897 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002898 }
2899 }
2900
2901 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002902 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002903 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002904 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002905
Max Reitz644483d2015-02-05 13:58:17 -05002906 if (bs->backing_format[0] != '\0') {
2907 options = qdict_new();
2908 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2909 }
2910
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002911 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002912 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002913 options, src_flags, &local_err);
2914 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002915 error_reportf_err(local_err,
2916 "Could not open old backing file '%s': ",
2917 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002918 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002919 }
Max Reitz644483d2015-02-05 13:58:17 -05002920
Alex Bligha6166732012-10-16 13:46:18 +01002921 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002922 if (out_basefmt) {
2923 options = qdict_new();
2924 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2925 } else {
2926 options = NULL;
2927 }
2928
Max Reitzefaa7c42016-03-16 19:54:38 +01002929 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002930 options, src_flags, &local_err);
2931 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002932 error_reportf_err(local_err,
2933 "Could not open new backing file '%s': ",
2934 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002935 goto out;
2936 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002937 }
2938 }
2939
2940 /*
2941 * Check each unallocated cluster in the COW file. If it is unallocated,
2942 * accesses go to the backing file. We must therefore compare this cluster
2943 * in the old and new backing file, and if they differ we need to copy it
2944 * from the old backing file into the COW file.
2945 *
2946 * If qemu-img crashes during this step, no harm is done. The content of
2947 * the image is the same as the original one at any time.
2948 */
2949 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002950 int64_t num_sectors;
2951 int64_t old_backing_num_sectors;
2952 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002953 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002954 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002955 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002956
Max Reitzf1d3cd72015-02-05 13:58:18 -05002957 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2958 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002959
Max Reitzf1d3cd72015-02-05 13:58:18 -05002960 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002961 if (num_sectors < 0) {
2962 error_report("Could not get size of '%s': %s",
2963 filename, strerror(-num_sectors));
2964 ret = -1;
2965 goto out;
2966 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002967 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002968 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002969 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002970
2971 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2972 error_report("Could not get size of '%s': %s",
2973 backing_name, strerror(-old_backing_num_sectors));
2974 ret = -1;
2975 goto out;
2976 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002977 if (blk_new_backing) {
2978 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002979 if (new_backing_num_sectors < 0) {
2980 error_report("Could not get size of '%s': %s",
2981 out_baseimg, strerror(-new_backing_num_sectors));
2982 ret = -1;
2983 goto out;
2984 }
Alex Bligha6166732012-10-16 13:46:18 +01002985 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002986
Kevin Wolf1f710492012-10-12 14:29:18 +02002987 if (num_sectors != 0) {
2988 local_progress = (float)100 /
2989 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2990 }
2991
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002992 for (sector = 0; sector < num_sectors; sector += n) {
2993
2994 /* How many sectors can we handle with the next read? */
2995 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2996 n = (IO_BUF_SIZE / 512);
2997 } else {
2998 n = num_sectors - sector;
2999 }
3000
3001 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003002 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003003 if (ret < 0) {
3004 error_report("error while reading image metadata: %s",
3005 strerror(-ret));
3006 goto out;
3007 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003008 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003009 continue;
3010 }
3011
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003012 /*
3013 * Read old and new backing file and take into consideration that
3014 * backing files may be smaller than the COW image.
3015 */
3016 if (sector >= old_backing_num_sectors) {
3017 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3018 } else {
3019 if (sector + n > old_backing_num_sectors) {
3020 n = old_backing_num_sectors - sector;
3021 }
3022
Max Reitzf1d3cd72015-02-05 13:58:18 -05003023 ret = blk_read(blk_old_backing, sector, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003024 if (ret < 0) {
3025 error_report("error while reading from old backing file");
3026 goto out;
3027 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003028 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003029
Max Reitzf1d3cd72015-02-05 13:58:18 -05003030 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003031 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3032 } else {
3033 if (sector + n > new_backing_num_sectors) {
3034 n = new_backing_num_sectors - sector;
3035 }
3036
Max Reitzf1d3cd72015-02-05 13:58:18 -05003037 ret = blk_read(blk_new_backing, sector, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003038 if (ret < 0) {
3039 error_report("error while reading from new backing file");
3040 goto out;
3041 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003042 }
3043
3044 /* If they differ, we need to write to the COW file */
3045 uint64_t written = 0;
3046
3047 while (written < n) {
3048 int pnum;
3049
3050 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003051 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003052 {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003053 ret = blk_write(blk, sector + written,
3054 buf_old + written * 512, pnum);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003055 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003056 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003057 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003058 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003059 }
3060 }
3061
3062 written += pnum;
3063 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003064 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003065 }
3066 }
3067
3068 /*
3069 * Change the backing file. All clusters that are different from the old
3070 * backing file are overwritten in the COW file now, so the visible content
3071 * doesn't change when we switch the backing file.
3072 */
Alex Bligha6166732012-10-16 13:46:18 +01003073 if (out_baseimg && *out_baseimg) {
3074 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3075 } else {
3076 ret = bdrv_change_backing_file(bs, NULL, NULL);
3077 }
3078
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003079 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003080 error_report("Could not change the backing file to '%s': No "
3081 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003082 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003083 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003084 out_baseimg, strerror(-ret));
3085 }
3086
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003087 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003088 /*
3089 * TODO At this point it is possible to check if any clusters that are
3090 * allocated in the COW file are the same in the backing file. If so, they
3091 * could be dropped from the COW file. Don't do this before switching the
3092 * backing file, in case of a crash this would lead to corruption.
3093 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003094out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003095 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003096 /* Cleanup */
3097 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003098 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003099 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003100 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003101 qemu_vfree(buf_old);
3102 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003103
Markus Armbruster26f54e92014-10-07 13:59:04 +02003104 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003105 if (ret) {
3106 return 1;
3107 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003108 return 0;
3109}
3110
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003111static int img_resize(int argc, char **argv)
3112{
Markus Armbruster6750e792015-02-12 17:43:08 +01003113 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003114 int c, ret, relative;
3115 const char *filename, *fmt, *size;
3116 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003117 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003118 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003119 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003120 Error *local_err = NULL;
3121
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003122 static QemuOptsList resize_options = {
3123 .name = "resize_options",
3124 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3125 .desc = {
3126 {
3127 .name = BLOCK_OPT_SIZE,
3128 .type = QEMU_OPT_SIZE,
3129 .help = "Virtual disk size"
3130 }, {
3131 /* end of list */
3132 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003133 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003134 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003135 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003136
Kevin Wolfe80fec72011-04-29 10:58:12 +02003137 /* Remove size from argv manually so that negative numbers are not treated
3138 * as options by getopt. */
3139 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003140 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003141 return 1;
3142 }
3143
3144 size = argv[--argc];
3145
3146 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003147 fmt = NULL;
3148 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003149 static const struct option long_options[] = {
3150 {"help", no_argument, 0, 'h'},
3151 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003152 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003153 {0, 0, 0, 0}
3154 };
3155 c = getopt_long(argc, argv, "f:hq",
3156 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003157 if (c == -1) {
3158 break;
3159 }
3160 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003161 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003162 case 'h':
3163 help();
3164 break;
3165 case 'f':
3166 fmt = optarg;
3167 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003168 case 'q':
3169 quiet = true;
3170 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003171 case OPTION_OBJECT: {
3172 QemuOpts *opts;
3173 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3174 optarg, true);
3175 if (!opts) {
3176 return 1;
3177 }
3178 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003179 case OPTION_IMAGE_OPTS:
3180 image_opts = true;
3181 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003182 }
3183 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003184 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003185 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003186 }
3187 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003188
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003189 if (qemu_opts_foreach(&qemu_object_opts,
3190 user_creatable_add_opts_foreach,
3191 NULL, &local_err)) {
3192 error_report_err(local_err);
3193 return 1;
3194 }
3195
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003196 /* Choose grow, shrink, or absolute resize mode */
3197 switch (size[0]) {
3198 case '+':
3199 relative = 1;
3200 size++;
3201 break;
3202 case '-':
3203 relative = -1;
3204 size++;
3205 break;
3206 default:
3207 relative = 0;
3208 break;
3209 }
3210
3211 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003212 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003213 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003214 if (err) {
3215 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003216 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003217 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003218 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003219 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003220 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3221 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003222
Max Reitzefaa7c42016-03-16 19:54:38 +01003223 blk = img_open(image_opts, filename, fmt,
Kevin Wolfe6996142016-03-15 13:03:11 +01003224 BDRV_O_CACHE_WB | BDRV_O_RDWR, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003225 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003226 ret = -1;
3227 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003228 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003229
3230 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003231 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003232 } else {
3233 total_size = n;
3234 }
3235 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003236 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003237 ret = -1;
3238 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003239 }
3240
Max Reitzf1d3cd72015-02-05 13:58:18 -05003241 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003242 switch (ret) {
3243 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003244 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003245 break;
3246 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003247 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003248 break;
3249 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003250 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003251 break;
3252 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003253 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003254 break;
3255 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003256out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003257 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003258 if (ret) {
3259 return 1;
3260 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003261 return 0;
3262}
3263
Max Reitz76a3a342014-10-27 11:12:51 +01003264static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003265 int64_t offset, int64_t total_work_size,
3266 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003267{
3268 qemu_progress_print(100.f * offset / total_work_size, 0);
3269}
3270
Max Reitz6f176b42013-09-03 10:09:50 +02003271static int img_amend(int argc, char **argv)
3272{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003273 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003274 int c, ret = 0;
3275 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003276 QemuOptsList *create_opts = NULL;
3277 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003278 const char *fmt = NULL, *filename, *cache;
3279 int flags;
Max Reitz76a3a342014-10-27 11:12:51 +01003280 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003281 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003282 BlockDriverState *bs = NULL;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003283 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003284 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003285
Max Reitzbd39e6e2014-07-22 22:58:43 +02003286 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003287 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003288 static const struct option long_options[] = {
3289 {"help", no_argument, 0, 'h'},
3290 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003291 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003292 {0, 0, 0, 0}
3293 };
3294 c = getopt_long(argc, argv, "ho:f:t:pq",
3295 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003296 if (c == -1) {
3297 break;
3298 }
3299
3300 switch (c) {
3301 case 'h':
3302 case '?':
3303 help();
3304 break;
3305 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003306 if (!is_valid_option_list(optarg)) {
3307 error_report("Invalid option list: %s", optarg);
3308 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003309 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003310 }
3311 if (!options) {
3312 options = g_strdup(optarg);
3313 } else {
3314 char *old_options = options;
3315 options = g_strdup_printf("%s,%s", options, optarg);
3316 g_free(old_options);
3317 }
Max Reitz6f176b42013-09-03 10:09:50 +02003318 break;
3319 case 'f':
3320 fmt = optarg;
3321 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003322 case 't':
3323 cache = optarg;
3324 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003325 case 'p':
3326 progress = true;
3327 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003328 case 'q':
3329 quiet = true;
3330 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003331 case OPTION_OBJECT:
3332 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3333 optarg, true);
3334 if (!opts) {
3335 ret = -1;
3336 goto out_no_progress;
3337 }
3338 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003339 case OPTION_IMAGE_OPTS:
3340 image_opts = true;
3341 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003342 }
3343 }
3344
Max Reitz6f176b42013-09-03 10:09:50 +02003345 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003346 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003347 }
3348
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003349 if (qemu_opts_foreach(&qemu_object_opts,
3350 user_creatable_add_opts_foreach,
3351 NULL, &local_err)) {
3352 error_report_err(local_err);
3353 ret = -1;
3354 goto out_no_progress;
3355 }
3356
Max Reitz76a3a342014-10-27 11:12:51 +01003357 if (quiet) {
3358 progress = false;
3359 }
3360 qemu_progress_init(progress, 1.0);
3361
Kevin Wolfa283cb62014-02-21 16:24:07 +01003362 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3363 if (fmt && has_help_option(options)) {
3364 /* If a format is explicitly specified (and possibly no filename is
3365 * given), print option help here */
3366 ret = print_block_option_help(filename, fmt);
3367 goto out;
3368 }
3369
3370 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003371 error_report("Expecting one image file name");
3372 ret = -1;
3373 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003374 }
Max Reitz6f176b42013-09-03 10:09:50 +02003375
Kevin Wolfe6996142016-03-15 13:03:11 +01003376 flags = BDRV_O_CACHE_WB | BDRV_O_RDWR;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003377 ret = bdrv_parse_cache_flags(cache, &flags);
3378 if (ret < 0) {
3379 error_report("Invalid cache option: %s", cache);
3380 goto out;
3381 }
3382
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00003383 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003384 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003385 ret = -1;
3386 goto out;
3387 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003388 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003389
3390 fmt = bs->drv->format_name;
3391
Kevin Wolf626f84f2014-02-21 16:24:06 +01003392 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003393 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003394 ret = print_block_option_help(filename, fmt);
3395 goto out;
3396 }
3397
Max Reitzb2439d22014-12-02 18:32:47 +01003398 if (!bs->drv->create_opts) {
3399 error_report("Format driver '%s' does not support any options to amend",
3400 fmt);
3401 ret = -1;
3402 goto out;
3403 }
3404
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003405 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003406 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003407 if (options) {
3408 qemu_opts_do_parse(opts, options, NULL, &err);
3409 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003410 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003411 ret = -1;
3412 goto out;
3413 }
Max Reitz6f176b42013-09-03 10:09:50 +02003414 }
3415
Max Reitz76a3a342014-10-27 11:12:51 +01003416 /* In case the driver does not call amend_status_cb() */
3417 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003418 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003419 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003420 if (ret < 0) {
3421 error_report("Error while amending options: %s", strerror(-ret));
3422 goto out;
3423 }
3424
3425out:
Max Reitz76a3a342014-10-27 11:12:51 +01003426 qemu_progress_end();
3427
Max Reitze814dff2015-08-20 16:00:38 -07003428out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003429 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003430 qemu_opts_del(opts);
3431 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003432 g_free(options);
3433
Max Reitz6f176b42013-09-03 10:09:50 +02003434 if (ret) {
3435 return 1;
3436 }
3437 return 0;
3438}
3439
Anthony Liguoric227f092009-10-01 16:12:16 -05003440static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003441#define DEF(option, callback, arg_string) \
3442 { option, callback },
3443#include "qemu-img-cmds.h"
3444#undef DEF
3445#undef GEN_DOCS
3446 { NULL, NULL, },
3447};
3448
bellardea2384d2004-08-01 21:59:26 +00003449int main(int argc, char **argv)
3450{
Anthony Liguoric227f092009-10-01 16:12:16 -05003451 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003452 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003453 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003454 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003455 static const struct option long_options[] = {
3456 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04003457 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04003458 {0, 0, 0, 0}
3459 };
bellardea2384d2004-08-01 21:59:26 +00003460
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003461#ifdef CONFIG_POSIX
3462 signal(SIGPIPE, SIG_IGN);
3463#endif
3464
Kevin Wolf53f76e52010-12-16 15:10:32 +01003465 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003466 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003467
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003468 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003469 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003470 exit(EXIT_FAILURE);
3471 }
3472
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003473 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003474 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003475 if (argc < 2) {
3476 error_exit("Not enough arguments");
3477 }
Stuart Brady153859b2009-06-07 00:42:17 +01003478 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01003479
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003480 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003481 qemu_add_opts(&qemu_source_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003482
Stuart Brady153859b2009-06-07 00:42:17 +01003483 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04003484 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01003485 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04003486 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01003487 }
bellardea2384d2004-08-01 21:59:26 +00003488 }
Stuart Brady153859b2009-06-07 00:42:17 +01003489
Jeff Cody5f6979c2014-04-28 14:37:18 -04003490 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04003491
3492 if (c == 'h') {
3493 help();
3494 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04003495 if (c == 'v') {
3496 printf(QEMU_IMG_VERSION);
3497 return 0;
3498 }
Jeff Cody7db16892014-04-25 17:02:32 -04003499
Stuart Brady153859b2009-06-07 00:42:17 +01003500 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003501 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003502}