blob: 9131416f60732c2b85bdcb1db6cd41e7a42f452b [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,
Kevin Wolfce099542016-03-15 13:01:04 +0100250 QemuOpts *opts, int flags, bool writethrough,
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 }
Kevin Wolfce099542016-03-15 13:01:04 +0100262 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000263
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000264 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000265 blk_unref(blk);
266 return NULL;
267 }
268 return blk;
269}
270
Max Reitzefaa7c42016-03-16 19:54:38 +0100271static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000272 const char *fmt, int flags,
Kevin Wolfce099542016-03-15 13:01:04 +0100273 bool writethrough, bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000274{
275 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200276 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500277 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100278
bellard75c23802004-08-27 21:28:58 +0000279 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500280 options = qdict_new();
281 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000282 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100283
Max Reitzefaa7c42016-03-16 19:54:38 +0100284 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500285 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100286 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000287 return NULL;
bellard75c23802004-08-27 21:28:58 +0000288 }
Kevin Wolfce099542016-03-15 13:01:04 +0100289 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100290
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000291 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000292 blk_unref(blk);
293 return NULL;
bellard75c23802004-08-27 21:28:58 +0000294 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200295 return blk;
bellard75c23802004-08-27 21:28:58 +0000296}
297
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298
Max Reitzefaa7c42016-03-16 19:54:38 +0100299static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100301 const char *fmt, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000302 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000303{
304 BlockBackend *blk;
305 if (image_opts) {
306 QemuOpts *opts;
307 if (fmt) {
308 error_report("--image-opts and --format are mutually exclusive");
309 return NULL;
310 }
311 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
312 filename, true);
313 if (!opts) {
314 return NULL;
315 }
Kevin Wolfce099542016-03-15 13:01:04 +0100316 blk = img_open_opts(filename, opts, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000317 } else {
Kevin Wolfce099542016-03-15 13:01:04 +0100318 blk = img_open_file(filename, fmt, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319 }
320 return blk;
321}
322
323
Chunyan Liu83d05212014-06-05 17:20:51 +0800324static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100325 const char *base_filename,
326 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200327{
Markus Armbruster6750e792015-02-12 17:43:08 +0100328 Error *err = NULL;
329
Kevin Wolfefa84d42009-05-18 16:42:12 +0200330 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100331 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100332 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100333 error_report("Backing file not supported for file format '%s'",
334 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100335 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900336 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200337 }
338 }
339 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100340 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100341 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100342 error_report("Backing file format not supported for file "
343 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100344 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900345 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200346 }
347 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900348 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200349}
350
bellardea2384d2004-08-01 21:59:26 +0000351static int img_create(int argc, char **argv)
352{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200353 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100354 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000355 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000356 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000357 const char *filename;
358 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200359 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200360 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100361 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000362
bellardea2384d2004-08-01 21:59:26 +0000363 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000364 static const struct option long_options[] = {
365 {"help", no_argument, 0, 'h'},
366 {"object", required_argument, 0, OPTION_OBJECT},
367 {0, 0, 0, 0}
368 };
369 c = getopt_long(argc, argv, "F:b:f:he6o:q",
370 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100371 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000372 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100373 }
bellardea2384d2004-08-01 21:59:26 +0000374 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100375 case '?':
bellardea2384d2004-08-01 21:59:26 +0000376 case 'h':
377 help();
378 break;
aliguori9230eaf2009-03-28 17:55:19 +0000379 case 'F':
380 base_fmt = optarg;
381 break;
bellardea2384d2004-08-01 21:59:26 +0000382 case 'b':
383 base_filename = optarg;
384 break;
385 case 'f':
386 fmt = optarg;
387 break;
388 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200389 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100390 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100391 goto fail;
thsd8871c52007-10-24 16:11:42 +0000392 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200393 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100394 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100395 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200396 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100397 if (!is_valid_option_list(optarg)) {
398 error_report("Invalid option list: %s", optarg);
399 goto fail;
400 }
401 if (!options) {
402 options = g_strdup(optarg);
403 } else {
404 char *old_options = options;
405 options = g_strdup_printf("%s,%s", options, optarg);
406 g_free(old_options);
407 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200408 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100409 case 'q':
410 quiet = true;
411 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000412 case OPTION_OBJECT: {
413 QemuOpts *opts;
414 opts = qemu_opts_parse_noisily(&qemu_object_opts,
415 optarg, true);
416 if (!opts) {
417 goto fail;
418 }
419 } break;
bellardea2384d2004-08-01 21:59:26 +0000420 }
421 }
aliguori9230eaf2009-03-28 17:55:19 +0000422
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900423 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100424 filename = (optind < argc) ? argv[optind] : NULL;
425 if (options && has_help_option(options)) {
426 g_free(options);
427 return print_block_option_help(filename, fmt);
428 }
429
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100430 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800431 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100432 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100433 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900434
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000435 if (qemu_opts_foreach(&qemu_object_opts,
436 user_creatable_add_opts_foreach,
437 NULL, &local_err)) {
438 error_report_err(local_err);
439 goto fail;
440 }
441
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100442 /* Get image size, if specified */
443 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100444 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100445 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200446 sval = qemu_strtosz_suffix(argv[optind++], &end,
447 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100448 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800449 if (sval == -ERANGE) {
450 error_report("Image size must be less than 8 EiB!");
451 } else {
452 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200453 "G, T, P or E suffixes for ");
454 error_report("kilobytes, megabytes, gigabytes, terabytes, "
455 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800456 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100457 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100458 }
459 img_size = (uint64_t)sval;
460 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200461 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800462 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200463 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100464
Luiz Capitulino9b375252012-11-30 10:52:05 -0200465 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Kevin Wolfe6996142016-03-15 13:03:11 +0100466 options, img_size, BDRV_O_CACHE_WB, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100467 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100468 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100469 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900470 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200471
Kevin Wolf77386bf2014-02-21 16:24:04 +0100472 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000473 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100474
475fail:
476 g_free(options);
477 return 1;
bellardea2384d2004-08-01 21:59:26 +0000478}
479
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100480static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500481{
Markus Armbruster4399c432014-04-25 16:50:32 +0200482 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500483 QString *str;
484 QmpOutputVisitor *ov = qmp_output_visitor_new();
485 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -0700486 visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
487 &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500488 obj = qmp_output_get_qobject(ov);
489 str = qobject_to_json_pretty(obj);
490 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100491 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500492 qobject_decref(obj);
493 qmp_output_visitor_cleanup(ov);
494 QDECREF(str);
495}
496
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100497static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500498{
499 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100500 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500501 } else {
502 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100503 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
504 "Data may be corrupted, or further writes to the image "
505 "may corrupt it.\n",
506 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500507 }
508
509 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100510 qprintf(quiet,
511 "\n%" PRId64 " leaked clusters were found on the image.\n"
512 "This means waste of disk space, but no harm to data.\n",
513 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500514 }
515
516 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100517 qprintf(quiet,
518 "\n%" PRId64
519 " internal errors have occurred during the check.\n",
520 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500521 }
522 }
523
524 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100525 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
526 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
527 check->allocated_clusters, check->total_clusters,
528 check->allocated_clusters * 100.0 / check->total_clusters,
529 check->fragmented_clusters * 100.0 / check->allocated_clusters,
530 check->compressed_clusters * 100.0 /
531 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500532 }
533
534 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100535 qprintf(quiet,
536 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500537 }
538}
539
540static int collect_image_check(BlockDriverState *bs,
541 ImageCheck *check,
542 const char *filename,
543 const char *fmt,
544 int fix)
545{
546 int ret;
547 BdrvCheckResult result;
548
549 ret = bdrv_check(bs, &result, fix);
550 if (ret < 0) {
551 return ret;
552 }
553
554 check->filename = g_strdup(filename);
555 check->format = g_strdup(bdrv_get_format_name(bs));
556 check->check_errors = result.check_errors;
557 check->corruptions = result.corruptions;
558 check->has_corruptions = result.corruptions != 0;
559 check->leaks = result.leaks;
560 check->has_leaks = result.leaks != 0;
561 check->corruptions_fixed = result.corruptions_fixed;
562 check->has_corruptions_fixed = result.corruptions != 0;
563 check->leaks_fixed = result.leaks_fixed;
564 check->has_leaks_fixed = result.leaks != 0;
565 check->image_end_offset = result.image_end_offset;
566 check->has_image_end_offset = result.image_end_offset != 0;
567 check->total_clusters = result.bfi.total_clusters;
568 check->has_total_clusters = result.bfi.total_clusters != 0;
569 check->allocated_clusters = result.bfi.allocated_clusters;
570 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
571 check->fragmented_clusters = result.bfi.fragmented_clusters;
572 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100573 check->compressed_clusters = result.bfi.compressed_clusters;
574 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500575
576 return 0;
577}
578
Kevin Wolfe076f332010-06-29 11:43:13 +0200579/*
580 * Checks an image for consistency. Exit codes:
581 *
Max Reitzd6635c42014-06-02 22:15:21 +0200582 * 0 - Check completed, image is good
583 * 1 - Check not completed because of internal errors
584 * 2 - Check completed, image is corrupted
585 * 3 - Check completed, image has leaked clusters, but is good otherwise
586 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200587 */
aliguori15859692009-04-21 23:11:53 +0000588static int img_check(int argc, char **argv)
589{
590 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500591 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200592 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200593 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000594 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200595 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100596 int flags = BDRV_O_CHECK;
597 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200598 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100599 bool quiet = false;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000600 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000601 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000602
603 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500604 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200605 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100606
aliguori15859692009-04-21 23:11:53 +0000607 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500608 int option_index = 0;
609 static const struct option long_options[] = {
610 {"help", no_argument, 0, 'h'},
611 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530612 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500613 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000614 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000615 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500616 {0, 0, 0, 0}
617 };
Max Reitz40055952014-07-22 22:58:42 +0200618 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500619 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100620 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000621 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100622 }
aliguori15859692009-04-21 23:11:53 +0000623 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100624 case '?':
aliguori15859692009-04-21 23:11:53 +0000625 case 'h':
626 help();
627 break;
628 case 'f':
629 fmt = optarg;
630 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200631 case 'r':
632 flags |= BDRV_O_RDWR;
633
634 if (!strcmp(optarg, "leaks")) {
635 fix = BDRV_FIX_LEAKS;
636 } else if (!strcmp(optarg, "all")) {
637 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
638 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800639 error_exit("Unknown option value for -r "
640 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200641 }
642 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500643 case OPTION_OUTPUT:
644 output = optarg;
645 break;
Max Reitz40055952014-07-22 22:58:42 +0200646 case 'T':
647 cache = optarg;
648 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100649 case 'q':
650 quiet = true;
651 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000652 case OPTION_OBJECT: {
653 QemuOpts *opts;
654 opts = qemu_opts_parse_noisily(&qemu_object_opts,
655 optarg, true);
656 if (!opts) {
657 return 1;
658 }
659 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000660 case OPTION_IMAGE_OPTS:
661 image_opts = true;
662 break;
aliguori15859692009-04-21 23:11:53 +0000663 }
664 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200665 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800666 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100667 }
aliguori15859692009-04-21 23:11:53 +0000668 filename = argv[optind++];
669
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500670 if (output && !strcmp(output, "json")) {
671 output_format = OFORMAT_JSON;
672 } else if (output && !strcmp(output, "human")) {
673 output_format = OFORMAT_HUMAN;
674 } else if (output) {
675 error_report("--output must be used with human or json as argument.");
676 return 1;
677 }
678
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000679 if (qemu_opts_foreach(&qemu_object_opts,
680 user_creatable_add_opts_foreach,
681 NULL, &local_err)) {
682 error_report_err(local_err);
683 return 1;
684 }
685
Kevin Wolfce099542016-03-15 13:01:04 +0100686 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200687 if (ret < 0) {
688 error_report("Invalid source cache option: %s", cache);
689 return 1;
690 }
691
Kevin Wolfce099542016-03-15 13:01:04 +0100692 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200693 if (!blk) {
694 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900695 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200696 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500697
698 check = g_new0(ImageCheck, 1);
699 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200700
701 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200702 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200703 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500704 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200705 }
706
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500707 if (check->corruptions_fixed || check->leaks_fixed) {
708 int corruptions_fixed, leaks_fixed;
709
710 leaks_fixed = check->leaks_fixed;
711 corruptions_fixed = check->corruptions_fixed;
712
713 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100714 qprintf(quiet,
715 "The following inconsistencies were found and repaired:\n\n"
716 " %" PRId64 " leaked clusters\n"
717 " %" PRId64 " corruptions\n\n"
718 "Double checking the fixed image now...\n",
719 check->leaks_fixed,
720 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500721 }
722
723 ret = collect_image_check(bs, check, filename, fmt, 0);
724
725 check->leaks_fixed = leaks_fixed;
726 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200727 }
728
Max Reitz832390a2014-10-23 15:29:12 +0200729 if (!ret) {
730 switch (output_format) {
731 case OFORMAT_HUMAN:
732 dump_human_image_check(check, quiet);
733 break;
734 case OFORMAT_JSON:
735 dump_json_image_check(check, quiet);
736 break;
737 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500738 }
739
740 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200741 if (ret) {
742 error_report("Check failed: %s", strerror(-ret));
743 } else {
744 error_report("Check failed");
745 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500746 ret = 1;
747 goto fail;
748 }
749
750 if (check->corruptions) {
751 ret = 2;
752 } else if (check->leaks) {
753 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200754 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500755 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000756 }
757
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500758fail:
759 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200760 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500761 return ret;
aliguori15859692009-04-21 23:11:53 +0000762}
763
Max Reitzd4a32382014-10-24 15:57:37 +0200764typedef struct CommonBlockJobCBInfo {
765 BlockDriverState *bs;
766 Error **errp;
767} CommonBlockJobCBInfo;
768
769static void common_block_job_cb(void *opaque, int ret)
770{
771 CommonBlockJobCBInfo *cbi = opaque;
772
773 if (ret < 0) {
774 error_setg_errno(cbi->errp, -ret, "Block job failed");
775 }
Max Reitzd4a32382014-10-24 15:57:37 +0200776}
777
778static void run_block_job(BlockJob *job, Error **errp)
779{
780 AioContext *aio_context = bdrv_get_aio_context(job->bs);
781
782 do {
783 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500784 qemu_progress_print(job->len ?
785 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200786 } while (!job->ready);
787
788 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200789
790 /* A block job may finish instantaneously without publishing any progress,
791 * so just signal completion here */
792 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200793}
794
bellardea2384d2004-08-01 21:59:26 +0000795static int img_commit(int argc, char **argv)
796{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400797 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200798 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200799 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200800 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200801 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100802 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200803 Error *local_err = NULL;
804 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000805 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000806
807 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400808 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200809 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000810 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000811 static const struct option long_options[] = {
812 {"help", no_argument, 0, 'h'},
813 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000814 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000815 {0, 0, 0, 0}
816 };
817 c = getopt_long(argc, argv, "f:ht:b:dpq",
818 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100819 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000820 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100821 }
bellardea2384d2004-08-01 21:59:26 +0000822 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100823 case '?':
bellardea2384d2004-08-01 21:59:26 +0000824 case 'h':
825 help();
826 break;
827 case 'f':
828 fmt = optarg;
829 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400830 case 't':
831 cache = optarg;
832 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200833 case 'b':
834 base = optarg;
835 /* -b implies -d */
836 drop = true;
837 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200838 case 'd':
839 drop = true;
840 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200841 case 'p':
842 progress = true;
843 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100844 case 'q':
845 quiet = true;
846 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000847 case OPTION_OBJECT: {
848 QemuOpts *opts;
849 opts = qemu_opts_parse_noisily(&qemu_object_opts,
850 optarg, true);
851 if (!opts) {
852 return 1;
853 }
854 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000855 case OPTION_IMAGE_OPTS:
856 image_opts = true;
857 break;
bellardea2384d2004-08-01 21:59:26 +0000858 }
859 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200860
861 /* Progress is not shown in Quiet mode */
862 if (quiet) {
863 progress = false;
864 }
865
Kevin Wolffc11eb22013-08-05 10:53:04 +0200866 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800867 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100868 }
bellardea2384d2004-08-01 21:59:26 +0000869 filename = argv[optind++];
870
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000871 if (qemu_opts_foreach(&qemu_object_opts,
872 user_creatable_add_opts_foreach,
873 NULL, &local_err)) {
874 error_report_err(local_err);
875 return 1;
876 }
877
Max Reitz9a86fe42014-10-24 15:57:38 +0200878 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100879 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400880 if (ret < 0) {
881 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100882 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400883 }
884
Kevin Wolfce099542016-03-15 13:01:04 +0100885 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200886 if (!blk) {
887 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900888 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200889 bs = blk_bs(blk);
890
Max Reitz687fa1d2014-10-24 15:57:39 +0200891 qemu_progress_init(progress, 1.f);
892 qemu_progress_print(0.f, 100);
893
Max Reitz1b22bff2014-10-24 15:57:40 +0200894 if (base) {
895 base_bs = bdrv_find_backing_image(bs, base);
896 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100897 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200898 goto done;
899 }
900 } else {
901 /* This is different from QMP, which by default uses the deepest file in
902 * the backing chain (i.e., the very base); however, the traditional
903 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200904 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200905 if (!base_bs) {
906 error_setg(&local_err, "Image does not have a backing file");
907 goto done;
908 }
bellardea2384d2004-08-01 21:59:26 +0000909 }
910
Max Reitzd4a32382014-10-24 15:57:37 +0200911 cbi = (CommonBlockJobCBInfo){
912 .errp = &local_err,
913 .bs = bs,
914 };
915
916 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
917 common_block_job_cb, &cbi, &local_err);
918 if (local_err) {
919 goto done;
920 }
921
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200922 /* When the block job completes, the BlockBackend reference will point to
923 * the old backing file. In order to avoid that the top image is already
924 * deleted, so we can still empty it afterwards, increment the reference
925 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200926 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200927 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200928 }
929
Max Reitzd4a32382014-10-24 15:57:37 +0200930 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200931 if (local_err) {
932 goto unref_backing;
933 }
934
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200935 if (!drop && bs->drv->bdrv_make_empty) {
936 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200937 if (ret) {
938 error_setg_errno(&local_err, -ret, "Could not empty %s",
939 filename);
940 goto unref_backing;
941 }
942 }
943
944unref_backing:
945 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200946 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200947 }
Max Reitzd4a32382014-10-24 15:57:37 +0200948
949done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200950 qemu_progress_end();
951
Markus Armbruster26f54e92014-10-07 13:59:04 +0200952 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200953
954 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100955 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900956 return 1;
957 }
Max Reitzd4a32382014-10-24 15:57:37 +0200958
959 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000960 return 0;
961}
962
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400963/*
thsf58c7b32008-06-05 21:53:49 +0000964 * Returns true iff the first sector pointed to by 'buf' contains at least
965 * a non-NUL byte.
966 *
967 * 'pnum' is set to the number of sectors (including and immediately following
968 * the first one) that are known to be in the same allocated/unallocated state.
969 */
bellardea2384d2004-08-01 21:59:26 +0000970static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
971{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000972 bool is_zero;
973 int i;
bellardea2384d2004-08-01 21:59:26 +0000974
975 if (n <= 0) {
976 *pnum = 0;
977 return 0;
978 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000979 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000980 for(i = 1; i < n; i++) {
981 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000982 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000983 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000984 }
bellardea2384d2004-08-01 21:59:26 +0000985 }
986 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000987 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000988}
989
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100990/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200991 * Like is_allocated_sectors, but if the buffer starts with a used sector,
992 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
993 * breaking up write requests for only small sparse areas.
994 */
995static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
996 int min)
997{
998 int ret;
999 int num_checked, num_used;
1000
1001 if (n < min) {
1002 min = n;
1003 }
1004
1005 ret = is_allocated_sectors(buf, n, pnum);
1006 if (!ret) {
1007 return ret;
1008 }
1009
1010 num_used = *pnum;
1011 buf += BDRV_SECTOR_SIZE * *pnum;
1012 n -= *pnum;
1013 num_checked = num_used;
1014
1015 while (n > 0) {
1016 ret = is_allocated_sectors(buf, n, pnum);
1017
1018 buf += BDRV_SECTOR_SIZE * *pnum;
1019 n -= *pnum;
1020 num_checked += *pnum;
1021 if (ret) {
1022 num_used = num_checked;
1023 } else if (*pnum >= min) {
1024 break;
1025 }
1026 }
1027
1028 *pnum = num_used;
1029 return 1;
1030}
1031
1032/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001033 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1034 * buffers matches, non-zero otherwise.
1035 *
1036 * pnum is set to the number of sectors (including and immediately following
1037 * the first one) that are known to have the same comparison result
1038 */
1039static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1040 int *pnum)
1041{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001042 bool res;
1043 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001044
1045 if (n <= 0) {
1046 *pnum = 0;
1047 return 0;
1048 }
1049
1050 res = !!memcmp(buf1, buf2, 512);
1051 for(i = 1; i < n; i++) {
1052 buf1 += 512;
1053 buf2 += 512;
1054
1055 if (!!memcmp(buf1, buf2, 512) != res) {
1056 break;
1057 }
1058 }
1059
1060 *pnum = i;
1061 return res;
1062}
1063
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001064#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001065
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001066static int64_t sectors_to_bytes(int64_t sectors)
1067{
1068 return sectors << BDRV_SECTOR_BITS;
1069}
1070
1071static int64_t sectors_to_process(int64_t total, int64_t from)
1072{
1073 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1074}
1075
1076/*
1077 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1078 *
1079 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1080 * data and negative value on error.
1081 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001082 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001083 * @param sect_num: Number of first sector to check
1084 * @param sect_count: Number of sectors to check
1085 * @param filename: Name of disk file we are checking (logging purpose)
1086 * @param buffer: Allocated buffer for storing read data
1087 * @param quiet: Flag for quiet mode
1088 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001089static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001090 int sect_count, const char *filename,
1091 uint8_t *buffer, bool quiet)
1092{
1093 int pnum, ret = 0;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001094 ret = blk_read(blk, sect_num, buffer, sect_count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001095 if (ret < 0) {
1096 error_report("Error while reading offset %" PRId64 " of %s: %s",
1097 sectors_to_bytes(sect_num), filename, strerror(-ret));
1098 return ret;
1099 }
1100 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1101 if (ret || pnum != sect_count) {
1102 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1103 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1104 return 1;
1105 }
1106
1107 return 0;
1108}
1109
1110/*
1111 * Compares two images. Exit codes:
1112 *
1113 * 0 - Images are identical
1114 * 1 - Images differ
1115 * >1 - Error occurred
1116 */
1117static int img_compare(int argc, char **argv)
1118{
Max Reitz40055952014-07-22 22:58:42 +02001119 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001120 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001121 BlockDriverState *bs1, *bs2;
1122 int64_t total_sectors1, total_sectors2;
1123 uint8_t *buf1 = NULL, *buf2 = NULL;
1124 int pnum1, pnum2;
1125 int allocated1, allocated2;
1126 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1127 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001128 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001129 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001130 int64_t total_sectors;
1131 int64_t sector_num = 0;
1132 int64_t nb_sectors;
1133 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001134 uint64_t progress_base;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001135 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001136 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001137
Max Reitz40055952014-07-22 22:58:42 +02001138 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001139 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001140 static const struct option long_options[] = {
1141 {"help", no_argument, 0, 'h'},
1142 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001143 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001144 {0, 0, 0, 0}
1145 };
1146 c = getopt_long(argc, argv, "hf:F:T:pqs",
1147 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001148 if (c == -1) {
1149 break;
1150 }
1151 switch (c) {
1152 case '?':
1153 case 'h':
1154 help();
1155 break;
1156 case 'f':
1157 fmt1 = optarg;
1158 break;
1159 case 'F':
1160 fmt2 = optarg;
1161 break;
Max Reitz40055952014-07-22 22:58:42 +02001162 case 'T':
1163 cache = optarg;
1164 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001165 case 'p':
1166 progress = true;
1167 break;
1168 case 'q':
1169 quiet = true;
1170 break;
1171 case 's':
1172 strict = true;
1173 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001174 case OPTION_OBJECT: {
1175 QemuOpts *opts;
1176 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1177 optarg, true);
1178 if (!opts) {
1179 ret = 2;
1180 goto out4;
1181 }
1182 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001183 case OPTION_IMAGE_OPTS:
1184 image_opts = true;
1185 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001186 }
1187 }
1188
1189 /* Progress is not shown in Quiet mode */
1190 if (quiet) {
1191 progress = false;
1192 }
1193
1194
Kevin Wolffc11eb22013-08-05 10:53:04 +02001195 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001196 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001197 }
1198 filename1 = argv[optind++];
1199 filename2 = argv[optind++];
1200
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001201 if (qemu_opts_foreach(&qemu_object_opts,
1202 user_creatable_add_opts_foreach,
1203 NULL, &local_err)) {
1204 error_report_err(local_err);
1205 ret = 2;
1206 goto out4;
1207 }
1208
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001209 /* Initialize before goto out */
1210 qemu_progress_init(progress, 2.0);
1211
Kevin Wolfce099542016-03-15 13:01:04 +01001212 flags = 0;
1213 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001214 if (ret < 0) {
1215 error_report("Invalid source cache option: %s", cache);
1216 ret = 2;
1217 goto out3;
1218 }
1219
Kevin Wolfce099542016-03-15 13:01:04 +01001220 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001221 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001222 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001223 goto out3;
1224 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001225
Kevin Wolfce099542016-03-15 13:01:04 +01001226 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001227 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001228 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001229 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001230 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001231 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001232 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001233
Max Reitzf1d3cd72015-02-05 13:58:18 -05001234 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1235 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1236 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001237 if (total_sectors1 < 0) {
1238 error_report("Can't get size of %s: %s",
1239 filename1, strerror(-total_sectors1));
1240 ret = 4;
1241 goto out;
1242 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001243 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001244 if (total_sectors2 < 0) {
1245 error_report("Can't get size of %s: %s",
1246 filename2, strerror(-total_sectors2));
1247 ret = 4;
1248 goto out;
1249 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001250 total_sectors = MIN(total_sectors1, total_sectors2);
1251 progress_base = MAX(total_sectors1, total_sectors2);
1252
1253 qemu_progress_print(0, 100);
1254
1255 if (strict && total_sectors1 != total_sectors2) {
1256 ret = 1;
1257 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1258 goto out;
1259 }
1260
1261 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001262 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001263 BlockDriverState *file;
1264
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001265 nb_sectors = sectors_to_process(total_sectors, sector_num);
1266 if (nb_sectors <= 0) {
1267 break;
1268 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001269 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1270 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001271 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001272 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001273 ret = 3;
1274 error_report("Sector allocation test failed for %s", filename1);
1275 goto out;
1276 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001277 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001278
Fam Zheng25ad8e62016-01-13 16:37:41 +08001279 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1280 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001281 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001282 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001283 ret = 3;
1284 error_report("Sector allocation test failed for %s", filename2);
1285 goto out;
1286 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001287 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1288 if (pnum1) {
1289 nb_sectors = MIN(nb_sectors, pnum1);
1290 }
1291 if (pnum2) {
1292 nb_sectors = MIN(nb_sectors, pnum2);
1293 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001294
Fam Zheng25ad8e62016-01-13 16:37:41 +08001295 if (strict) {
1296 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1297 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1298 ret = 1;
1299 qprintf(quiet, "Strict mode: Offset %" PRId64
1300 " block status mismatch!\n",
1301 sectors_to_bytes(sector_num));
1302 goto out;
1303 }
1304 }
1305 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1306 nb_sectors = MIN(pnum1, pnum2);
1307 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001308 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001309 ret = blk_read(blk1, sector_num, buf1, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001310 if (ret < 0) {
1311 error_report("Error while reading offset %" PRId64 " of %s:"
1312 " %s", sectors_to_bytes(sector_num), filename1,
1313 strerror(-ret));
1314 ret = 4;
1315 goto out;
1316 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001317 ret = blk_read(blk2, sector_num, buf2, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001318 if (ret < 0) {
1319 error_report("Error while reading offset %" PRId64
1320 " of %s: %s", sectors_to_bytes(sector_num),
1321 filename2, strerror(-ret));
1322 ret = 4;
1323 goto out;
1324 }
1325 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1326 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001327 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1328 sectors_to_bytes(
1329 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001330 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001331 goto out;
1332 }
1333 }
1334 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001335
1336 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001337 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001338 filename1, buf1, quiet);
1339 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001340 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001341 filename2, buf1, quiet);
1342 }
1343 if (ret) {
1344 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001345 error_report("Error while reading offset %" PRId64 ": %s",
1346 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001347 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001348 }
1349 goto out;
1350 }
1351 }
1352 sector_num += nb_sectors;
1353 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1354 }
1355
1356 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001357 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001358 int64_t total_sectors_over;
1359 const char *filename_over;
1360
1361 qprintf(quiet, "Warning: Image size mismatch!\n");
1362 if (total_sectors1 > total_sectors2) {
1363 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001364 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001365 filename_over = filename1;
1366 } else {
1367 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001368 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001369 filename_over = filename2;
1370 }
1371
1372 for (;;) {
1373 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1374 if (nb_sectors <= 0) {
1375 break;
1376 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001377 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001378 nb_sectors, &pnum);
1379 if (ret < 0) {
1380 ret = 3;
1381 error_report("Sector allocation test failed for %s",
1382 filename_over);
1383 goto out;
1384
1385 }
1386 nb_sectors = pnum;
1387 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001388 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001389 filename_over, buf1, quiet);
1390 if (ret) {
1391 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001392 error_report("Error while reading offset %" PRId64
1393 " of %s: %s", sectors_to_bytes(sector_num),
1394 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001395 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001396 }
1397 goto out;
1398 }
1399 }
1400 sector_num += nb_sectors;
1401 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1402 }
1403 }
1404
1405 qprintf(quiet, "Images are identical.\n");
1406 ret = 0;
1407
1408out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409 qemu_vfree(buf1);
1410 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001411 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001412out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001413 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414out3:
1415 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001416out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001417 return ret;
1418}
1419
Kevin Wolf690c7302015-03-19 13:33:32 +01001420enum ImgConvertBlockStatus {
1421 BLK_DATA,
1422 BLK_ZERO,
1423 BLK_BACKING_FILE,
1424};
1425
1426typedef struct ImgConvertState {
1427 BlockBackend **src;
1428 int64_t *src_sectors;
1429 int src_cur, src_num;
1430 int64_t src_cur_offset;
1431 int64_t total_sectors;
1432 int64_t allocated_sectors;
1433 enum ImgConvertBlockStatus status;
1434 int64_t sector_next_status;
1435 BlockBackend *target;
1436 bool has_zero_init;
1437 bool compressed;
1438 bool target_has_backing;
1439 int min_sparse;
1440 size_t cluster_sectors;
1441 size_t buf_sectors;
1442} ImgConvertState;
1443
1444static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1445{
1446 assert(sector_num >= s->src_cur_offset);
1447 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1448 s->src_cur_offset += s->src_sectors[s->src_cur];
1449 s->src_cur++;
1450 assert(s->src_cur < s->src_num);
1451 }
1452}
1453
1454static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1455{
1456 int64_t ret;
1457 int n;
1458
1459 convert_select_part(s, sector_num);
1460
1461 assert(s->total_sectors > sector_num);
1462 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1463
1464 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001465 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001466 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1467 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001468 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001469 if (ret < 0) {
1470 return ret;
1471 }
1472
1473 if (ret & BDRV_BLOCK_ZERO) {
1474 s->status = BLK_ZERO;
1475 } else if (ret & BDRV_BLOCK_DATA) {
1476 s->status = BLK_DATA;
1477 } else if (!s->target_has_backing) {
1478 /* Without a target backing file we must copy over the contents of
1479 * the backing file as well. */
1480 /* TODO Check block status of the backing file chain to avoid
1481 * needlessly reading zeroes and limiting the iteration to the
1482 * buffer size */
1483 s->status = BLK_DATA;
1484 } else {
1485 s->status = BLK_BACKING_FILE;
1486 }
1487
1488 s->sector_next_status = sector_num + n;
1489 }
1490
1491 n = MIN(n, s->sector_next_status - sector_num);
1492 if (s->status == BLK_DATA) {
1493 n = MIN(n, s->buf_sectors);
1494 }
1495
1496 /* We need to write complete clusters for compressed images, so if an
1497 * unallocated area is shorter than that, we must consider the whole
1498 * cluster allocated. */
1499 if (s->compressed) {
1500 if (n < s->cluster_sectors) {
1501 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1502 s->status = BLK_DATA;
1503 } else {
1504 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1505 }
1506 }
1507
1508 return n;
1509}
1510
1511static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1512 uint8_t *buf)
1513{
1514 int n;
1515 int ret;
1516
1517 if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) {
1518 return 0;
1519 }
1520
1521 assert(nb_sectors <= s->buf_sectors);
1522 while (nb_sectors > 0) {
1523 BlockBackend *blk;
1524 int64_t bs_sectors;
1525
1526 /* In the case of compression with multiple source files, we can get a
1527 * nb_sectors that spreads into the next part. So we must be able to
1528 * read across multiple BDSes for one convert_read() call. */
1529 convert_select_part(s, sector_num);
1530 blk = s->src[s->src_cur];
1531 bs_sectors = s->src_sectors[s->src_cur];
1532
1533 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
1534 ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n);
1535 if (ret < 0) {
1536 return ret;
1537 }
1538
1539 sector_num += n;
1540 nb_sectors -= n;
1541 buf += n * BDRV_SECTOR_SIZE;
1542 }
1543
1544 return 0;
1545}
1546
1547static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1548 const uint8_t *buf)
1549{
1550 int ret;
1551
1552 while (nb_sectors > 0) {
1553 int n = nb_sectors;
1554
1555 switch (s->status) {
1556 case BLK_BACKING_FILE:
1557 /* If we have a backing file, leave clusters unallocated that are
1558 * unallocated in the source image, so that the backing file is
1559 * visible at the respective offset. */
1560 assert(s->target_has_backing);
1561 break;
1562
1563 case BLK_DATA:
1564 /* We must always write compressed clusters as a whole, so don't
1565 * try to find zeroed parts in the buffer. We can only save the
1566 * write if the buffer is completely zeroed and we're allowed to
1567 * keep the target sparse. */
1568 if (s->compressed) {
1569 if (s->has_zero_init && s->min_sparse &&
1570 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1571 {
1572 assert(!s->target_has_backing);
1573 break;
1574 }
1575
1576 ret = blk_write_compressed(s->target, sector_num, buf, n);
1577 if (ret < 0) {
1578 return ret;
1579 }
1580 break;
1581 }
1582
1583 /* If there is real non-zero data or we're told to keep the target
1584 * fully allocated (-S 0), we must write it. Otherwise we can treat
1585 * it as zero sectors. */
1586 if (!s->min_sparse ||
1587 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1588 {
1589 ret = blk_write(s->target, sector_num, buf, n);
1590 if (ret < 0) {
1591 return ret;
1592 }
1593 break;
1594 }
1595 /* fall-through */
1596
1597 case BLK_ZERO:
1598 if (s->has_zero_init) {
1599 break;
1600 }
1601 ret = blk_write_zeroes(s->target, sector_num, n, 0);
1602 if (ret < 0) {
1603 return ret;
1604 }
1605 break;
1606 }
1607
1608 sector_num += n;
1609 nb_sectors -= n;
1610 buf += n * BDRV_SECTOR_SIZE;
1611 }
1612
1613 return 0;
1614}
1615
1616static int convert_do_copy(ImgConvertState *s)
1617{
1618 uint8_t *buf = NULL;
1619 int64_t sector_num, allocated_done;
1620 int ret;
1621 int n;
1622
1623 /* Check whether we have zero initialisation or can get it efficiently */
1624 s->has_zero_init = s->min_sparse && !s->target_has_backing
1625 ? bdrv_has_zero_init(blk_bs(s->target))
1626 : false;
1627
1628 if (!s->has_zero_init && !s->target_has_backing &&
1629 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1630 {
1631 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1632 if (ret == 0) {
1633 s->has_zero_init = true;
1634 }
1635 }
1636
1637 /* Allocate buffer for copied data. For compressed images, only one cluster
1638 * can be copied at a time. */
1639 if (s->compressed) {
1640 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1641 error_report("invalid cluster size");
1642 ret = -EINVAL;
1643 goto fail;
1644 }
1645 s->buf_sectors = s->cluster_sectors;
1646 }
1647 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1648
1649 /* Calculate allocated sectors for progress */
1650 s->allocated_sectors = 0;
1651 sector_num = 0;
1652 while (sector_num < s->total_sectors) {
1653 n = convert_iteration_sectors(s, sector_num);
1654 if (n < 0) {
1655 ret = n;
1656 goto fail;
1657 }
1658 if (s->status == BLK_DATA) {
1659 s->allocated_sectors += n;
1660 }
1661 sector_num += n;
1662 }
1663
1664 /* Do the copy */
1665 s->src_cur = 0;
1666 s->src_cur_offset = 0;
1667 s->sector_next_status = 0;
1668
1669 sector_num = 0;
1670 allocated_done = 0;
1671
1672 while (sector_num < s->total_sectors) {
1673 n = convert_iteration_sectors(s, sector_num);
1674 if (n < 0) {
1675 ret = n;
1676 goto fail;
1677 }
1678 if (s->status == BLK_DATA) {
1679 allocated_done += n;
1680 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1681 0);
1682 }
1683
1684 ret = convert_read(s, sector_num, n, buf);
1685 if (ret < 0) {
1686 error_report("error while reading sector %" PRId64
1687 ": %s", sector_num, strerror(-ret));
1688 goto fail;
1689 }
1690
1691 ret = convert_write(s, sector_num, n, buf);
1692 if (ret < 0) {
1693 error_report("error while writing sector %" PRId64
1694 ": %s", sector_num, strerror(-ret));
1695 goto fail;
1696 }
1697
1698 sector_num += n;
1699 }
1700
1701 if (s->compressed) {
1702 /* signal EOF to align */
1703 ret = blk_write_compressed(s->target, 0, NULL, 0);
1704 if (ret < 0) {
1705 goto fail;
1706 }
1707 }
1708
1709 ret = 0;
1710fail:
1711 qemu_vfree(buf);
1712 return ret;
1713}
1714
bellardea2384d2004-08-01 21:59:26 +00001715static int img_convert(int argc, char **argv)
1716{
Kevin Wolf690c7302015-03-19 13:33:32 +01001717 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001718 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001719 int progress = 0, flags, src_flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001720 bool writethrough, src_writethrough;
Max Reitz40055952014-07-22 22:58:42 +02001721 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001722 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001723 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001724 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001725 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001726 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001727 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001728 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001729 QemuOpts *opts = NULL;
1730 QemuOptsList *create_opts = NULL;
1731 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001732 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001733 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001734 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001735 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001736 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001737 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001738 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001739 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001740
1741 fmt = NULL;
1742 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001743 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001744 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001745 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001746 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001747 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001748 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001749 static const struct option long_options[] = {
1750 {"help", no_argument, 0, 'h'},
1751 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001752 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001753 {0, 0, 0, 0}
1754 };
1755 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1756 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001757 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001758 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001759 }
bellardea2384d2004-08-01 21:59:26 +00001760 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001761 case '?':
bellardea2384d2004-08-01 21:59:26 +00001762 case 'h':
1763 help();
1764 break;
1765 case 'f':
1766 fmt = optarg;
1767 break;
1768 case 'O':
1769 out_fmt = optarg;
1770 break;
thsf58c7b32008-06-05 21:53:49 +00001771 case 'B':
1772 out_baseimg = optarg;
1773 break;
bellardea2384d2004-08-01 21:59:26 +00001774 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001775 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001776 break;
1777 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001778 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001779 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001780 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001781 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001782 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001783 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001784 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001785 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001786 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001787 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001788 if (!is_valid_option_list(optarg)) {
1789 error_report("Invalid option list: %s", optarg);
1790 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001791 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001792 }
1793 if (!options) {
1794 options = g_strdup(optarg);
1795 } else {
1796 char *old_options = options;
1797 options = g_strdup_printf("%s,%s", options, optarg);
1798 g_free(old_options);
1799 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001800 break;
edison51ef6722010-09-21 19:58:41 -07001801 case 's':
1802 snapshot_name = optarg;
1803 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001804 case 'l':
1805 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001806 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1807 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001808 if (!sn_opts) {
1809 error_report("Failed in parsing snapshot param '%s'",
1810 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001811 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001812 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001813 }
1814 } else {
1815 snapshot_name = optarg;
1816 }
1817 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001818 case 'S':
1819 {
1820 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001821 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001822 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001823 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001824 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001825 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001826 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001827 }
1828
1829 min_sparse = sval / BDRV_SECTOR_SIZE;
1830 break;
1831 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001832 case 'p':
1833 progress = 1;
1834 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001835 case 't':
1836 cache = optarg;
1837 break;
Max Reitz40055952014-07-22 22:58:42 +02001838 case 'T':
1839 src_cache = optarg;
1840 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001841 case 'q':
1842 quiet = true;
1843 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001844 case 'n':
1845 skip_create = 1;
1846 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001847 case OPTION_OBJECT:
1848 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1849 optarg, true);
1850 if (!opts) {
1851 goto fail_getopt;
1852 }
1853 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001854 case OPTION_IMAGE_OPTS:
1855 image_opts = true;
1856 break;
bellardea2384d2004-08-01 21:59:26 +00001857 }
1858 }
ths3b46e622007-09-17 08:09:54 +00001859
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001860 if (qemu_opts_foreach(&qemu_object_opts,
1861 user_creatable_add_opts_foreach,
1862 NULL, &local_err)) {
1863 error_report_err(local_err);
1864 goto fail_getopt;
1865 }
1866
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001867 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001868 if (quiet) {
1869 progress = 0;
1870 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001871 qemu_progress_init(progress, 1.0);
1872
balrog926c2d22007-10-31 01:11:44 +00001873 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001874 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001875
Kevin Wolf2dc83282014-02-21 16:24:05 +01001876 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001877 ret = print_block_option_help(out_filename, out_fmt);
1878 goto out;
1879 }
1880
bellardea2384d2004-08-01 21:59:26 +00001881 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001882 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001883 }
bellardea2384d2004-08-01 21:59:26 +00001884
thsf58c7b32008-06-05 21:53:49 +00001885
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001886 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001887 error_report("-B makes no sense when concatenating multiple input "
1888 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001889 ret = -1;
1890 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001891 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001892
Kevin Wolfce099542016-03-15 13:01:04 +01001893 src_flags = 0;
1894 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001895 if (ret < 0) {
1896 error_report("Invalid source cache option: %s", src_cache);
1897 goto out;
1898 }
1899
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001900 qemu_progress_print(0, 100);
1901
Markus Armbruster26f54e92014-10-07 13:59:04 +02001902 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001903 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001904 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001905
1906 total_sectors = 0;
1907 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001908 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Kevin Wolfce099542016-03-15 13:01:04 +01001909 fmt, src_flags, src_writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001910 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001911 ret = -1;
1912 goto out;
1913 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001914 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001915 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001916 if (bs_sectors[bs_i] < 0) {
1917 error_report("Could not get size of %s: %s",
1918 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1919 ret = -1;
1920 goto out;
1921 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001922 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001923 }
bellardea2384d2004-08-01 21:59:26 +00001924
Wenchao Xiaef806542013-12-04 17:10:57 +08001925 if (sn_opts) {
1926 ret = bdrv_snapshot_load_tmp(bs[0],
1927 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1928 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1929 &local_err);
1930 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001931 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001932 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001933 ret = -1;
1934 goto out;
1935 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001936
1937 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001938 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001939 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001940 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001941 ret = -1;
1942 goto out;
edison51ef6722010-09-21 19:58:41 -07001943 }
1944
Kevin Wolfefa84d42009-05-18 16:42:12 +02001945 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001946 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001947 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001948 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001949 ret = -1;
1950 goto out;
1951 }
balrog926c2d22007-10-31 01:11:44 +00001952
Max Reitzb65a5e12015-02-05 13:58:12 -05001953 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001954 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001955 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001956 ret = -1;
1957 goto out;
1958 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001959
Max Reitz2e024cd2015-02-11 09:58:46 -05001960 if (!skip_create) {
1961 if (!drv->create_opts) {
1962 error_report("Format driver '%s' does not support image creation",
1963 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 if (!proto_drv->create_opts) {
1969 error_report("Protocol driver '%s' does not support image creation",
1970 proto_drv->format_name);
1971 ret = -1;
1972 goto out;
1973 }
Max Reitzf75613c2014-12-02 18:32:46 +01001974
Max Reitz2e024cd2015-02-11 09:58:46 -05001975 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1976 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001977
Max Reitz2e024cd2015-02-11 09:58:46 -05001978 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001979 if (options) {
1980 qemu_opts_do_parse(opts, options, NULL, &local_err);
1981 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01001982 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001983 ret = -1;
1984 goto out;
1985 }
Max Reitz2e024cd2015-02-11 09:58:46 -05001986 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001987
Markus Armbruster39101f22015-02-12 16:46:36 +01001988 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
1989 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05001990 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
1991 if (ret < 0) {
1992 goto out;
1993 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001994 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001995
Kevin Wolfa18953f2010-10-14 15:46:04 +02001996 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001997 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001998 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001999 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002000 }
2001
Kevin Wolfefa84d42009-05-18 16:42:12 +02002002 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01002003 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002004 bool encryption =
2005 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2006 const char *preallocation =
2007 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002008
2009 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002010 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002011 ret = -1;
2012 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002013 }
2014
Chunyan Liu83d05212014-06-05 17:20:51 +08002015 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002016 error_report("Compression and encryption not supported at "
2017 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002018 ret = -1;
2019 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002020 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002021
Chunyan Liu83d05212014-06-05 17:20:51 +08002022 if (preallocation
2023 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002024 {
2025 error_report("Compression and preallocation not supported at "
2026 "the same time");
2027 ret = -1;
2028 goto out;
2029 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002030 }
2031
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002032 if (!skip_create) {
2033 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002034 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002035 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002036 error_reportf_err(local_err, "%s: error while converting %s: ",
2037 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002038 goto out;
bellardea2384d2004-08-01 21:59:26 +00002039 }
2040 }
ths3b46e622007-09-17 08:09:54 +00002041
Peter Lieven5a37b602013-10-24 12:07:06 +02002042 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002043 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002044 if (ret < 0) {
2045 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002046 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002047 }
2048
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002049 /* XXX we should allow --image-opts to trigger use of
2050 * img_open() here, but then we have trouble with
2051 * the bdrv_create() call which takes different params.
2052 * Not critical right now, so fix can wait...
2053 */
Kevin Wolfce099542016-03-15 13:01:04 +01002054 out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002055 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002056 ret = -1;
2057 goto out;
2058 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002059 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002060
Peter Lievenf2521c92013-11-27 11:07:06 +01002061 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
2062 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2063 * as maximum. */
2064 bufsectors = MIN(32768,
2065 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
2066 out_bs->bl.discard_alignment))
2067 );
2068
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002069 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002070 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002071 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002072 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002073 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002074 ret = -1;
2075 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002076 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002077 error_report("output file is smaller than input file");
2078 ret = -1;
2079 goto out;
2080 }
2081 }
2082
Peter Lieven24f833c2013-11-27 11:07:07 +01002083 cluster_sectors = 0;
2084 ret = bdrv_get_info(out_bs, &bdi);
2085 if (ret < 0) {
2086 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002087 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002088 goto out;
2089 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002090 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002091 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002092 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2093 }
2094
Kevin Wolf690c7302015-03-19 13:33:32 +01002095 state = (ImgConvertState) {
2096 .src = blk,
2097 .src_sectors = bs_sectors,
2098 .src_num = bs_n,
2099 .total_sectors = total_sectors,
2100 .target = out_blk,
2101 .compressed = compress,
2102 .target_has_backing = (bool) out_baseimg,
2103 .min_sparse = min_sparse,
2104 .cluster_sectors = cluster_sectors,
2105 .buf_sectors = bufsectors,
2106 };
2107 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002108
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002109out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002110 if (!ret) {
2111 qemu_progress_print(100, 0);
2112 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002113 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002114 qemu_opts_del(opts);
2115 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002116 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002117 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002118 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002119 if (blk) {
2120 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2121 blk_unref(blk[bs_i]);
2122 }
2123 g_free(blk);
2124 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002125 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002126fail_getopt:
2127 g_free(options);
2128
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002129 if (ret) {
2130 return 1;
2131 }
bellardea2384d2004-08-01 21:59:26 +00002132 return 0;
2133}
2134
bellard57d1a2b2004-08-03 21:15:11 +00002135
bellardfaea38e2006-08-05 21:31:00 +00002136static void dump_snapshots(BlockDriverState *bs)
2137{
2138 QEMUSnapshotInfo *sn_tab, *sn;
2139 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002140
2141 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2142 if (nb_sns <= 0)
2143 return;
2144 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002145 bdrv_snapshot_dump(fprintf, stdout, NULL);
2146 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002147 for(i = 0; i < nb_sns; i++) {
2148 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002149 bdrv_snapshot_dump(fprintf, stdout, sn);
2150 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002151 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002152 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002153}
2154
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002155static void dump_json_image_info_list(ImageInfoList *list)
2156{
Markus Armbruster4399c432014-04-25 16:50:32 +02002157 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002158 QString *str;
2159 QmpOutputVisitor *ov = qmp_output_visitor_new();
2160 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002161 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2162 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002163 obj = qmp_output_get_qobject(ov);
2164 str = qobject_to_json_pretty(obj);
2165 assert(str != NULL);
2166 printf("%s\n", qstring_get_str(str));
2167 qobject_decref(obj);
2168 qmp_output_visitor_cleanup(ov);
2169 QDECREF(str);
2170}
2171
Benoît Canetc054b3f2012-09-05 13:09:02 +02002172static void dump_json_image_info(ImageInfo *info)
2173{
Markus Armbruster4399c432014-04-25 16:50:32 +02002174 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002175 QString *str;
2176 QmpOutputVisitor *ov = qmp_output_visitor_new();
2177 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002178 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002179 obj = qmp_output_get_qobject(ov);
2180 str = qobject_to_json_pretty(obj);
2181 assert(str != NULL);
2182 printf("%s\n", qstring_get_str(str));
2183 qobject_decref(obj);
2184 qmp_output_visitor_cleanup(ov);
2185 QDECREF(str);
2186}
2187
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002188static void dump_human_image_info_list(ImageInfoList *list)
2189{
2190 ImageInfoList *elem;
2191 bool delim = false;
2192
2193 for (elem = list; elem; elem = elem->next) {
2194 if (delim) {
2195 printf("\n");
2196 }
2197 delim = true;
2198
Wenchao Xia5b917042013-05-25 11:09:45 +08002199 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002200 }
2201}
2202
2203static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2204{
2205 return strcmp(a, b) == 0;
2206}
2207
2208/**
2209 * Open an image file chain and return an ImageInfoList
2210 *
2211 * @filename: topmost image filename
2212 * @fmt: topmost image format (may be NULL to autodetect)
2213 * @chain: true - enumerate entire backing file chain
2214 * false - only topmost image file
2215 *
2216 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2217 * image file. If there was an error a message will have been printed to
2218 * stderr.
2219 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002220static ImageInfoList *collect_image_info_list(bool image_opts,
2221 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002222 const char *fmt,
2223 bool chain)
2224{
2225 ImageInfoList *head = NULL;
2226 ImageInfoList **last = &head;
2227 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002228 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002229
2230 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2231
2232 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002233 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002234 BlockDriverState *bs;
2235 ImageInfo *info;
2236 ImageInfoList *elem;
2237
2238 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2239 error_report("Backing file '%s' creates an infinite loop.",
2240 filename);
2241 goto err;
2242 }
2243 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2244
Max Reitzefaa7c42016-03-16 19:54:38 +01002245 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002246 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002247 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002248 goto err;
2249 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002250 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002251
Wenchao Xia43526ec2013-06-06 12:27:58 +08002252 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002253 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002254 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002255 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002256 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002257 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002258
2259 elem = g_new0(ImageInfoList, 1);
2260 elem->value = info;
2261 *last = elem;
2262 last = &elem->next;
2263
Markus Armbruster26f54e92014-10-07 13:59:04 +02002264 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002265
2266 filename = fmt = NULL;
2267 if (chain) {
2268 if (info->has_full_backing_filename) {
2269 filename = info->full_backing_filename;
2270 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002271 error_report("Could not determine absolute backing filename,"
2272 " but backing filename '%s' present",
2273 info->backing_filename);
2274 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002275 }
2276 if (info->has_backing_filename_format) {
2277 fmt = info->backing_filename_format;
2278 }
2279 }
2280 }
2281 g_hash_table_destroy(filenames);
2282 return head;
2283
2284err:
2285 qapi_free_ImageInfoList(head);
2286 g_hash_table_destroy(filenames);
2287 return NULL;
2288}
2289
Benoît Canetc054b3f2012-09-05 13:09:02 +02002290static int img_info(int argc, char **argv)
2291{
2292 int c;
2293 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002294 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002295 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002296 ImageInfoList *list;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002297 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002298 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002299
bellardea2384d2004-08-01 21:59:26 +00002300 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002301 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002302 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002303 int option_index = 0;
2304 static const struct option long_options[] = {
2305 {"help", no_argument, 0, 'h'},
2306 {"format", required_argument, 0, 'f'},
2307 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002308 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002309 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002310 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002311 {0, 0, 0, 0}
2312 };
2313 c = getopt_long(argc, argv, "f:h",
2314 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002315 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002316 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002317 }
bellardea2384d2004-08-01 21:59:26 +00002318 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002319 case '?':
bellardea2384d2004-08-01 21:59:26 +00002320 case 'h':
2321 help();
2322 break;
2323 case 'f':
2324 fmt = optarg;
2325 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002326 case OPTION_OUTPUT:
2327 output = optarg;
2328 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002329 case OPTION_BACKING_CHAIN:
2330 chain = true;
2331 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002332 case OPTION_OBJECT: {
2333 QemuOpts *opts;
2334 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2335 optarg, true);
2336 if (!opts) {
2337 return 1;
2338 }
2339 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002340 case OPTION_IMAGE_OPTS:
2341 image_opts = true;
2342 break;
bellardea2384d2004-08-01 21:59:26 +00002343 }
2344 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002345 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002346 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002347 }
bellardea2384d2004-08-01 21:59:26 +00002348 filename = argv[optind++];
2349
Benoît Canetc054b3f2012-09-05 13:09:02 +02002350 if (output && !strcmp(output, "json")) {
2351 output_format = OFORMAT_JSON;
2352 } else if (output && !strcmp(output, "human")) {
2353 output_format = OFORMAT_HUMAN;
2354 } else if (output) {
2355 error_report("--output must be used with human or json as argument.");
2356 return 1;
2357 }
2358
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002359 if (qemu_opts_foreach(&qemu_object_opts,
2360 user_creatable_add_opts_foreach,
2361 NULL, &local_err)) {
2362 error_report_err(local_err);
2363 return 1;
2364 }
2365
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002366 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002367 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002368 return 1;
2369 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002370
Benoît Canetc054b3f2012-09-05 13:09:02 +02002371 switch (output_format) {
2372 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002373 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002374 break;
2375 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002376 if (chain) {
2377 dump_json_image_info_list(list);
2378 } else {
2379 dump_json_image_info(list->value);
2380 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002381 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002382 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002383
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002384 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002385 return 0;
2386}
2387
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002388static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2389 MapEntry *next)
2390{
2391 switch (output_format) {
2392 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002393 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002394 error_report("File contains external, encrypted or compressed clusters.");
2395 exit(1);
2396 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002397 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002398 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002399 e->start, e->length,
2400 e->has_offset ? e->offset : 0,
2401 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002402 }
2403 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2404 * Modify the flags here to allow more coalescing.
2405 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002406 if (next && (!next->data || next->zero)) {
2407 next->data = false;
2408 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002409 }
2410 break;
2411 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002412 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2413 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002414 (e->start == 0 ? "[" : ",\n"),
2415 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002416 e->zero ? "true" : "false",
2417 e->data ? "true" : "false");
2418 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002419 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002420 }
2421 putchar('}');
2422
2423 if (!next) {
2424 printf("]\n");
2425 }
2426 break;
2427 }
2428}
2429
2430static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2431 int nb_sectors, MapEntry *e)
2432{
2433 int64_t ret;
2434 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002435 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002436 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002437
2438 /* As an optimization, we could cache the current range of unallocated
2439 * clusters in each file of the chain, and avoid querying the same
2440 * range repeatedly.
2441 */
2442
2443 depth = 0;
2444 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002445 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2446 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002447 if (ret < 0) {
2448 return ret;
2449 }
2450 assert(nb_sectors);
2451 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2452 break;
2453 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002454 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002455 if (bs == NULL) {
2456 ret = 0;
2457 break;
2458 }
2459
2460 depth++;
2461 }
2462
John Snow28756452016-02-05 13:12:33 -05002463 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2464
2465 *e = (MapEntry) {
2466 .start = sector_num * BDRV_SECTOR_SIZE,
2467 .length = nb_sectors * BDRV_SECTOR_SIZE,
2468 .data = !!(ret & BDRV_BLOCK_DATA),
2469 .zero = !!(ret & BDRV_BLOCK_ZERO),
2470 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2471 .has_offset = has_offset,
2472 .depth = depth,
2473 .has_filename = file && has_offset,
2474 .filename = file && has_offset ? file->filename : NULL,
2475 };
2476
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002477 return 0;
2478}
2479
Fam Zheng16b0d552016-01-26 11:59:02 +08002480static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2481{
2482 if (curr->length == 0) {
2483 return false;
2484 }
2485 if (curr->zero != next->zero ||
2486 curr->data != next->data ||
2487 curr->depth != next->depth ||
2488 curr->has_filename != next->has_filename ||
2489 curr->has_offset != next->has_offset) {
2490 return false;
2491 }
2492 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2493 return false;
2494 }
2495 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2496 return false;
2497 }
2498 return true;
2499}
2500
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002501static int img_map(int argc, char **argv)
2502{
2503 int c;
2504 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002505 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002506 BlockDriverState *bs;
2507 const char *filename, *fmt, *output;
2508 int64_t length;
2509 MapEntry curr = { .length = 0 }, next;
2510 int ret = 0;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002511 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002512 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002513
2514 fmt = NULL;
2515 output = NULL;
2516 for (;;) {
2517 int option_index = 0;
2518 static const struct option long_options[] = {
2519 {"help", no_argument, 0, 'h'},
2520 {"format", required_argument, 0, 'f'},
2521 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002522 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002523 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002524 {0, 0, 0, 0}
2525 };
2526 c = getopt_long(argc, argv, "f:h",
2527 long_options, &option_index);
2528 if (c == -1) {
2529 break;
2530 }
2531 switch (c) {
2532 case '?':
2533 case 'h':
2534 help();
2535 break;
2536 case 'f':
2537 fmt = optarg;
2538 break;
2539 case OPTION_OUTPUT:
2540 output = optarg;
2541 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002542 case OPTION_OBJECT: {
2543 QemuOpts *opts;
2544 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2545 optarg, true);
2546 if (!opts) {
2547 return 1;
2548 }
2549 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002550 case OPTION_IMAGE_OPTS:
2551 image_opts = true;
2552 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002553 }
2554 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002555 if (optind != argc - 1) {
2556 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002557 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002558 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002559
2560 if (output && !strcmp(output, "json")) {
2561 output_format = OFORMAT_JSON;
2562 } else if (output && !strcmp(output, "human")) {
2563 output_format = OFORMAT_HUMAN;
2564 } else if (output) {
2565 error_report("--output must be used with human or json as argument.");
2566 return 1;
2567 }
2568
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002569 if (qemu_opts_foreach(&qemu_object_opts,
2570 user_creatable_add_opts_foreach,
2571 NULL, &local_err)) {
2572 error_report_err(local_err);
2573 return 1;
2574 }
2575
Kevin Wolfce099542016-03-15 13:01:04 +01002576 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002577 if (!blk) {
2578 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002579 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002580 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002581
2582 if (output_format == OFORMAT_HUMAN) {
2583 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2584 }
2585
Max Reitzf1d3cd72015-02-05 13:58:18 -05002586 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002587 while (curr.start + curr.length < length) {
2588 int64_t nsectors_left;
2589 int64_t sector_num;
2590 int n;
2591
2592 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2593
2594 /* Probe up to 1 GiB at a time. */
2595 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2596 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2597 ret = get_block_status(bs, sector_num, n, &next);
2598
2599 if (ret < 0) {
2600 error_report("Could not read file metadata: %s", strerror(-ret));
2601 goto out;
2602 }
2603
Fam Zheng16b0d552016-01-26 11:59:02 +08002604 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002605 curr.length += next.length;
2606 continue;
2607 }
2608
2609 if (curr.length > 0) {
2610 dump_map_entry(output_format, &curr, &next);
2611 }
2612 curr = next;
2613 }
2614
2615 dump_map_entry(output_format, &curr, NULL);
2616
2617out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002618 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002619 return ret < 0;
2620}
2621
aliguorif7b4a942009-01-07 17:40:15 +00002622#define SNAPSHOT_LIST 1
2623#define SNAPSHOT_CREATE 2
2624#define SNAPSHOT_APPLY 3
2625#define SNAPSHOT_DELETE 4
2626
Stuart Brady153859b2009-06-07 00:42:17 +01002627static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002628{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002629 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002630 BlockDriverState *bs;
2631 QEMUSnapshotInfo sn;
2632 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002633 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002634 int action = 0;
2635 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002636 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002637 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002638 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002639
Kevin Wolfce099542016-03-15 13:01:04 +01002640 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002641 /* Parse commandline parameters */
2642 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002643 static const struct option long_options[] = {
2644 {"help", no_argument, 0, 'h'},
2645 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002646 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002647 {0, 0, 0, 0}
2648 };
2649 c = getopt_long(argc, argv, "la:c:d:hq",
2650 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002651 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002652 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002653 }
aliguorif7b4a942009-01-07 17:40:15 +00002654 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002655 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002656 case 'h':
2657 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002658 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002659 case 'l':
2660 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002661 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002662 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002663 }
2664 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002665 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002666 break;
2667 case 'a':
2668 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002669 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002670 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002671 }
2672 action = SNAPSHOT_APPLY;
2673 snapshot_name = optarg;
2674 break;
2675 case 'c':
2676 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002677 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002678 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002679 }
2680 action = SNAPSHOT_CREATE;
2681 snapshot_name = optarg;
2682 break;
2683 case 'd':
2684 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002685 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002686 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002687 }
2688 action = SNAPSHOT_DELETE;
2689 snapshot_name = optarg;
2690 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002691 case 'q':
2692 quiet = true;
2693 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002694 case OPTION_OBJECT: {
2695 QemuOpts *opts;
2696 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2697 optarg, true);
2698 if (!opts) {
2699 return 1;
2700 }
2701 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002702 case OPTION_IMAGE_OPTS:
2703 image_opts = true;
2704 break;
aliguorif7b4a942009-01-07 17:40:15 +00002705 }
2706 }
2707
Kevin Wolffc11eb22013-08-05 10:53:04 +02002708 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002709 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002710 }
aliguorif7b4a942009-01-07 17:40:15 +00002711 filename = argv[optind++];
2712
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002713 if (qemu_opts_foreach(&qemu_object_opts,
2714 user_creatable_add_opts_foreach,
2715 NULL, &err)) {
2716 error_report_err(err);
2717 return 1;
2718 }
2719
aliguorif7b4a942009-01-07 17:40:15 +00002720 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002721 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002722 if (!blk) {
2723 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002724 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002725 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002726
2727 /* Perform the requested action */
2728 switch(action) {
2729 case SNAPSHOT_LIST:
2730 dump_snapshots(bs);
2731 break;
2732
2733 case SNAPSHOT_CREATE:
2734 memset(&sn, 0, sizeof(sn));
2735 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2736
2737 qemu_gettimeofday(&tv);
2738 sn.date_sec = tv.tv_sec;
2739 sn.date_nsec = tv.tv_usec * 1000;
2740
2741 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002742 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002743 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002744 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002745 }
aliguorif7b4a942009-01-07 17:40:15 +00002746 break;
2747
2748 case SNAPSHOT_APPLY:
2749 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002750 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002751 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002752 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002753 }
aliguorif7b4a942009-01-07 17:40:15 +00002754 break;
2755
2756 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002757 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002758 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002759 error_reportf_err(err, "Could not delete snapshot '%s': ",
2760 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002761 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002762 }
aliguorif7b4a942009-01-07 17:40:15 +00002763 break;
2764 }
2765
2766 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002767 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002768 if (ret) {
2769 return 1;
2770 }
Stuart Brady153859b2009-06-07 00:42:17 +01002771 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002772}
2773
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002774static int img_rebase(int argc, char **argv)
2775{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002776 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002777 uint8_t *buf_old = NULL;
2778 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002779 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002780 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002781 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2782 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002783 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002784 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002785 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002786 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002787 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002788 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002789
2790 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002791 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002792 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002793 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002794 out_baseimg = NULL;
2795 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002796 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002797 static const struct option long_options[] = {
2798 {"help", no_argument, 0, 'h'},
2799 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002800 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002801 {0, 0, 0, 0}
2802 };
2803 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2804 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002805 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002806 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002807 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002808 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002809 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002810 case 'h':
2811 help();
2812 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002813 case 'f':
2814 fmt = optarg;
2815 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002816 case 'F':
2817 out_basefmt = optarg;
2818 break;
2819 case 'b':
2820 out_baseimg = optarg;
2821 break;
2822 case 'u':
2823 unsafe = 1;
2824 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002825 case 'p':
2826 progress = 1;
2827 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002828 case 't':
2829 cache = optarg;
2830 break;
Max Reitz40055952014-07-22 22:58:42 +02002831 case 'T':
2832 src_cache = optarg;
2833 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002834 case 'q':
2835 quiet = true;
2836 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002837 case OPTION_OBJECT: {
2838 QemuOpts *opts;
2839 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2840 optarg, true);
2841 if (!opts) {
2842 return 1;
2843 }
2844 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002845 case OPTION_IMAGE_OPTS:
2846 image_opts = true;
2847 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002848 }
2849 }
2850
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002851 if (quiet) {
2852 progress = 0;
2853 }
2854
Fam Zhengac1307a2014-04-22 13:36:11 +08002855 if (optind != argc - 1) {
2856 error_exit("Expecting one image file name");
2857 }
2858 if (!unsafe && !out_baseimg) {
2859 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002860 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002861 filename = argv[optind++];
2862
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002863 if (qemu_opts_foreach(&qemu_object_opts,
2864 user_creatable_add_opts_foreach,
2865 NULL, &local_err)) {
2866 error_report_err(local_err);
2867 return 1;
2868 }
2869
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002870 qemu_progress_init(progress, 2.0);
2871 qemu_progress_print(0, 100);
2872
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002873 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01002874 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002875 if (ret < 0) {
2876 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002877 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002878 }
2879
Kevin Wolfce099542016-03-15 13:01:04 +01002880 src_flags = 0;
2881 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02002882 if (ret < 0) {
2883 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002884 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002885 }
2886
Kevin Wolfce099542016-03-15 13:01:04 +01002887 /* The source files are opened read-only, don't care about WCE */
2888 assert((src_flags & BDRV_O_RDWR) == 0);
2889 (void) src_writethrough;
2890
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002891 /*
2892 * Open the images.
2893 *
2894 * Ignore the old backing file for unsafe rebase in case we want to correct
2895 * the reference to a renamed or moved backing file.
2896 */
Kevin Wolfce099542016-03-15 13:01:04 +01002897 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002898 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002899 ret = -1;
2900 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002901 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002902 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002903
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002904 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002905 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002906 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002907 ret = -1;
2908 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002909 }
2910 }
2911
2912 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002913 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002914 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002915 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002916
Max Reitz644483d2015-02-05 13:58:17 -05002917 if (bs->backing_format[0] != '\0') {
2918 options = qdict_new();
2919 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2920 }
2921
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002922 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002923 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002924 options, src_flags, &local_err);
2925 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002926 error_reportf_err(local_err,
2927 "Could not open old backing file '%s': ",
2928 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002929 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002930 }
Max Reitz644483d2015-02-05 13:58:17 -05002931
Alex Bligha6166732012-10-16 13:46:18 +01002932 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002933 if (out_basefmt) {
2934 options = qdict_new();
2935 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2936 } else {
2937 options = NULL;
2938 }
2939
Max Reitzefaa7c42016-03-16 19:54:38 +01002940 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002941 options, src_flags, &local_err);
2942 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002943 error_reportf_err(local_err,
2944 "Could not open new backing file '%s': ",
2945 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002946 goto out;
2947 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002948 }
2949 }
2950
2951 /*
2952 * Check each unallocated cluster in the COW file. If it is unallocated,
2953 * accesses go to the backing file. We must therefore compare this cluster
2954 * in the old and new backing file, and if they differ we need to copy it
2955 * from the old backing file into the COW file.
2956 *
2957 * If qemu-img crashes during this step, no harm is done. The content of
2958 * the image is the same as the original one at any time.
2959 */
2960 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002961 int64_t num_sectors;
2962 int64_t old_backing_num_sectors;
2963 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002964 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002965 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002966 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002967
Max Reitzf1d3cd72015-02-05 13:58:18 -05002968 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2969 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002970
Max Reitzf1d3cd72015-02-05 13:58:18 -05002971 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002972 if (num_sectors < 0) {
2973 error_report("Could not get size of '%s': %s",
2974 filename, strerror(-num_sectors));
2975 ret = -1;
2976 goto out;
2977 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002978 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002979 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002980 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002981
2982 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2983 error_report("Could not get size of '%s': %s",
2984 backing_name, strerror(-old_backing_num_sectors));
2985 ret = -1;
2986 goto out;
2987 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002988 if (blk_new_backing) {
2989 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002990 if (new_backing_num_sectors < 0) {
2991 error_report("Could not get size of '%s': %s",
2992 out_baseimg, strerror(-new_backing_num_sectors));
2993 ret = -1;
2994 goto out;
2995 }
Alex Bligha6166732012-10-16 13:46:18 +01002996 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002997
Kevin Wolf1f710492012-10-12 14:29:18 +02002998 if (num_sectors != 0) {
2999 local_progress = (float)100 /
3000 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3001 }
3002
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003003 for (sector = 0; sector < num_sectors; sector += n) {
3004
3005 /* How many sectors can we handle with the next read? */
3006 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3007 n = (IO_BUF_SIZE / 512);
3008 } else {
3009 n = num_sectors - sector;
3010 }
3011
3012 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003013 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003014 if (ret < 0) {
3015 error_report("error while reading image metadata: %s",
3016 strerror(-ret));
3017 goto out;
3018 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003019 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003020 continue;
3021 }
3022
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003023 /*
3024 * Read old and new backing file and take into consideration that
3025 * backing files may be smaller than the COW image.
3026 */
3027 if (sector >= old_backing_num_sectors) {
3028 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3029 } else {
3030 if (sector + n > old_backing_num_sectors) {
3031 n = old_backing_num_sectors - sector;
3032 }
3033
Max Reitzf1d3cd72015-02-05 13:58:18 -05003034 ret = blk_read(blk_old_backing, sector, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003035 if (ret < 0) {
3036 error_report("error while reading from old backing file");
3037 goto out;
3038 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003039 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003040
Max Reitzf1d3cd72015-02-05 13:58:18 -05003041 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003042 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3043 } else {
3044 if (sector + n > new_backing_num_sectors) {
3045 n = new_backing_num_sectors - sector;
3046 }
3047
Max Reitzf1d3cd72015-02-05 13:58:18 -05003048 ret = blk_read(blk_new_backing, sector, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003049 if (ret < 0) {
3050 error_report("error while reading from new backing file");
3051 goto out;
3052 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003053 }
3054
3055 /* If they differ, we need to write to the COW file */
3056 uint64_t written = 0;
3057
3058 while (written < n) {
3059 int pnum;
3060
3061 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003062 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003063 {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003064 ret = blk_write(blk, sector + written,
3065 buf_old + written * 512, pnum);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003066 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003067 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003068 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003069 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003070 }
3071 }
3072
3073 written += pnum;
3074 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003075 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003076 }
3077 }
3078
3079 /*
3080 * Change the backing file. All clusters that are different from the old
3081 * backing file are overwritten in the COW file now, so the visible content
3082 * doesn't change when we switch the backing file.
3083 */
Alex Bligha6166732012-10-16 13:46:18 +01003084 if (out_baseimg && *out_baseimg) {
3085 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3086 } else {
3087 ret = bdrv_change_backing_file(bs, NULL, NULL);
3088 }
3089
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003090 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003091 error_report("Could not change the backing file to '%s': No "
3092 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003093 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003094 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003095 out_baseimg, strerror(-ret));
3096 }
3097
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003098 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003099 /*
3100 * TODO At this point it is possible to check if any clusters that are
3101 * allocated in the COW file are the same in the backing file. If so, they
3102 * could be dropped from the COW file. Don't do this before switching the
3103 * backing file, in case of a crash this would lead to corruption.
3104 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003105out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003106 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003107 /* Cleanup */
3108 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003109 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003110 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003111 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003112 qemu_vfree(buf_old);
3113 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003114
Markus Armbruster26f54e92014-10-07 13:59:04 +02003115 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003116 if (ret) {
3117 return 1;
3118 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003119 return 0;
3120}
3121
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003122static int img_resize(int argc, char **argv)
3123{
Markus Armbruster6750e792015-02-12 17:43:08 +01003124 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003125 int c, ret, relative;
3126 const char *filename, *fmt, *size;
3127 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003128 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003129 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003130 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003131 Error *local_err = NULL;
3132
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003133 static QemuOptsList resize_options = {
3134 .name = "resize_options",
3135 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3136 .desc = {
3137 {
3138 .name = BLOCK_OPT_SIZE,
3139 .type = QEMU_OPT_SIZE,
3140 .help = "Virtual disk size"
3141 }, {
3142 /* end of list */
3143 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003144 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003145 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003146 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003147
Kevin Wolfe80fec72011-04-29 10:58:12 +02003148 /* Remove size from argv manually so that negative numbers are not treated
3149 * as options by getopt. */
3150 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003151 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003152 return 1;
3153 }
3154
3155 size = argv[--argc];
3156
3157 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003158 fmt = NULL;
3159 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003160 static const struct option long_options[] = {
3161 {"help", no_argument, 0, 'h'},
3162 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003163 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003164 {0, 0, 0, 0}
3165 };
3166 c = getopt_long(argc, argv, "f:hq",
3167 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003168 if (c == -1) {
3169 break;
3170 }
3171 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003172 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003173 case 'h':
3174 help();
3175 break;
3176 case 'f':
3177 fmt = optarg;
3178 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003179 case 'q':
3180 quiet = true;
3181 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003182 case OPTION_OBJECT: {
3183 QemuOpts *opts;
3184 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3185 optarg, true);
3186 if (!opts) {
3187 return 1;
3188 }
3189 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003190 case OPTION_IMAGE_OPTS:
3191 image_opts = true;
3192 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003193 }
3194 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003195 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003196 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003197 }
3198 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003199
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003200 if (qemu_opts_foreach(&qemu_object_opts,
3201 user_creatable_add_opts_foreach,
3202 NULL, &local_err)) {
3203 error_report_err(local_err);
3204 return 1;
3205 }
3206
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003207 /* Choose grow, shrink, or absolute resize mode */
3208 switch (size[0]) {
3209 case '+':
3210 relative = 1;
3211 size++;
3212 break;
3213 case '-':
3214 relative = -1;
3215 size++;
3216 break;
3217 default:
3218 relative = 0;
3219 break;
3220 }
3221
3222 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003223 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003224 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003225 if (err) {
3226 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003227 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003228 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003229 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003230 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003231 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3232 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003233
Max Reitzefaa7c42016-03-16 19:54:38 +01003234 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01003235 BDRV_O_RDWR, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003236 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003237 ret = -1;
3238 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003239 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003240
3241 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003242 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003243 } else {
3244 total_size = n;
3245 }
3246 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003247 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003248 ret = -1;
3249 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003250 }
3251
Max Reitzf1d3cd72015-02-05 13:58:18 -05003252 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003253 switch (ret) {
3254 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003255 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003256 break;
3257 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003258 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003259 break;
3260 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003261 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003262 break;
3263 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003264 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003265 break;
3266 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003267out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003268 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003269 if (ret) {
3270 return 1;
3271 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003272 return 0;
3273}
3274
Max Reitz76a3a342014-10-27 11:12:51 +01003275static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003276 int64_t offset, int64_t total_work_size,
3277 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003278{
3279 qemu_progress_print(100.f * offset / total_work_size, 0);
3280}
3281
Max Reitz6f176b42013-09-03 10:09:50 +02003282static int img_amend(int argc, char **argv)
3283{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003284 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003285 int c, ret = 0;
3286 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003287 QemuOptsList *create_opts = NULL;
3288 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003289 const char *fmt = NULL, *filename, *cache;
3290 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003291 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003292 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003293 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003294 BlockDriverState *bs = NULL;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003295 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003296 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003297
Max Reitzbd39e6e2014-07-22 22:58:43 +02003298 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003299 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003300 static const struct option long_options[] = {
3301 {"help", no_argument, 0, 'h'},
3302 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003303 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003304 {0, 0, 0, 0}
3305 };
3306 c = getopt_long(argc, argv, "ho:f:t:pq",
3307 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003308 if (c == -1) {
3309 break;
3310 }
3311
3312 switch (c) {
3313 case 'h':
3314 case '?':
3315 help();
3316 break;
3317 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003318 if (!is_valid_option_list(optarg)) {
3319 error_report("Invalid option list: %s", optarg);
3320 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003321 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003322 }
3323 if (!options) {
3324 options = g_strdup(optarg);
3325 } else {
3326 char *old_options = options;
3327 options = g_strdup_printf("%s,%s", options, optarg);
3328 g_free(old_options);
3329 }
Max Reitz6f176b42013-09-03 10:09:50 +02003330 break;
3331 case 'f':
3332 fmt = optarg;
3333 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003334 case 't':
3335 cache = optarg;
3336 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003337 case 'p':
3338 progress = true;
3339 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003340 case 'q':
3341 quiet = true;
3342 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003343 case OPTION_OBJECT:
3344 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3345 optarg, true);
3346 if (!opts) {
3347 ret = -1;
3348 goto out_no_progress;
3349 }
3350 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003351 case OPTION_IMAGE_OPTS:
3352 image_opts = true;
3353 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003354 }
3355 }
3356
Max Reitz6f176b42013-09-03 10:09:50 +02003357 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003358 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003359 }
3360
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003361 if (qemu_opts_foreach(&qemu_object_opts,
3362 user_creatable_add_opts_foreach,
3363 NULL, &local_err)) {
3364 error_report_err(local_err);
3365 ret = -1;
3366 goto out_no_progress;
3367 }
3368
Max Reitz76a3a342014-10-27 11:12:51 +01003369 if (quiet) {
3370 progress = false;
3371 }
3372 qemu_progress_init(progress, 1.0);
3373
Kevin Wolfa283cb62014-02-21 16:24:07 +01003374 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3375 if (fmt && has_help_option(options)) {
3376 /* If a format is explicitly specified (and possibly no filename is
3377 * given), print option help here */
3378 ret = print_block_option_help(filename, fmt);
3379 goto out;
3380 }
3381
3382 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003383 error_report("Expecting one image file name");
3384 ret = -1;
3385 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003386 }
Max Reitz6f176b42013-09-03 10:09:50 +02003387
Kevin Wolfce099542016-03-15 13:01:04 +01003388 flags = BDRV_O_RDWR;
3389 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003390 if (ret < 0) {
3391 error_report("Invalid cache option: %s", cache);
3392 goto out;
3393 }
3394
Kevin Wolfce099542016-03-15 13:01:04 +01003395 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003396 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003397 ret = -1;
3398 goto out;
3399 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003400 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003401
3402 fmt = bs->drv->format_name;
3403
Kevin Wolf626f84f2014-02-21 16:24:06 +01003404 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003405 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003406 ret = print_block_option_help(filename, fmt);
3407 goto out;
3408 }
3409
Max Reitzb2439d22014-12-02 18:32:47 +01003410 if (!bs->drv->create_opts) {
3411 error_report("Format driver '%s' does not support any options to amend",
3412 fmt);
3413 ret = -1;
3414 goto out;
3415 }
3416
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003417 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003418 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003419 if (options) {
3420 qemu_opts_do_parse(opts, options, NULL, &err);
3421 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003422 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003423 ret = -1;
3424 goto out;
3425 }
Max Reitz6f176b42013-09-03 10:09:50 +02003426 }
3427
Max Reitz76a3a342014-10-27 11:12:51 +01003428 /* In case the driver does not call amend_status_cb() */
3429 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003430 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003431 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003432 if (ret < 0) {
3433 error_report("Error while amending options: %s", strerror(-ret));
3434 goto out;
3435 }
3436
3437out:
Max Reitz76a3a342014-10-27 11:12:51 +01003438 qemu_progress_end();
3439
Max Reitze814dff2015-08-20 16:00:38 -07003440out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003441 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003442 qemu_opts_del(opts);
3443 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003444 g_free(options);
3445
Max Reitz6f176b42013-09-03 10:09:50 +02003446 if (ret) {
3447 return 1;
3448 }
3449 return 0;
3450}
3451
Anthony Liguoric227f092009-10-01 16:12:16 -05003452static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003453#define DEF(option, callback, arg_string) \
3454 { option, callback },
3455#include "qemu-img-cmds.h"
3456#undef DEF
3457#undef GEN_DOCS
3458 { NULL, NULL, },
3459};
3460
bellardea2384d2004-08-01 21:59:26 +00003461int main(int argc, char **argv)
3462{
Anthony Liguoric227f092009-10-01 16:12:16 -05003463 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003464 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003465 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003466 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003467 static const struct option long_options[] = {
3468 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04003469 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04003470 {0, 0, 0, 0}
3471 };
bellardea2384d2004-08-01 21:59:26 +00003472
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003473#ifdef CONFIG_POSIX
3474 signal(SIGPIPE, SIG_IGN);
3475#endif
3476
Kevin Wolf53f76e52010-12-16 15:10:32 +01003477 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003478 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003479
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003480 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003481 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003482 exit(EXIT_FAILURE);
3483 }
3484
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003485 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003486 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003487 if (argc < 2) {
3488 error_exit("Not enough arguments");
3489 }
Stuart Brady153859b2009-06-07 00:42:17 +01003490 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01003491
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003492 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003493 qemu_add_opts(&qemu_source_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003494
Stuart Brady153859b2009-06-07 00:42:17 +01003495 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04003496 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01003497 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04003498 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01003499 }
bellardea2384d2004-08-01 21:59:26 +00003500 }
Stuart Brady153859b2009-06-07 00:42:17 +01003501
Jeff Cody5f6979c2014-04-28 14:37:18 -04003502 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04003503
3504 if (c == 'h') {
3505 help();
3506 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04003507 if (c == 'v') {
3508 printf(QEMU_IMG_VERSION);
3509 return 0;
3510 }
Jeff Cody7db16892014-04-25 17:02:32 -04003511
Stuart Brady153859b2009-06-07 00:42:17 +01003512 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003513 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003514}