blob: 46f2a6def4a21000a500ff8a333fd2450a2901e2 [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"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010040#include "crypto/init.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020041#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000042
Don Slutz61979a62015-01-09 10:17:35 -050043#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Jeff Cody5f6979c2014-04-28 14:37:18 -040044 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
45
Anthony Liguoric227f092009-10-01 16:12:16 -050046typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010047 const char *name;
48 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050049} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010050
Federico Simoncelli8599ea42013-01-28 06:59:47 -050051enum {
52 OPTION_OUTPUT = 256,
53 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000054 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000055 OPTION_IMAGE_OPTS = 259,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050056};
57
58typedef enum OutputFormat {
59 OFORMAT_JSON,
60 OFORMAT_HUMAN,
61} OutputFormat;
62
Kevin Wolfe6996142016-03-15 13:03:11 +010063/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040064#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000065
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010066static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000067{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010068 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000069}
70
Fam Zhengac1307a2014-04-22 13:36:11 +080071static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
72{
73 va_list ap;
74
75 error_printf("qemu-img: ");
76
77 va_start(ap, fmt);
78 error_vprintf(fmt, ap);
79 va_end(ap);
80
81 error_printf("\nTry 'qemu-img --help' for more information\n");
82 exit(EXIT_FAILURE);
83}
84
blueswir1d2c639d2009-01-24 18:19:25 +000085/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080086static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000087{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010088 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040089 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030090 "usage: qemu-img command [command options]\n"
91 "QEMU disk image utility\n"
92 "\n"
93 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010094#define DEF(option, callback, arg_string) \
95 " " arg_string "\n"
96#include "qemu-img-cmds.h"
97#undef DEF
98#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030099 "\n"
100 "Command parameters:\n"
101 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000102 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
103 " manual page for a description of the object properties. The most common\n"
104 " object type is a 'secret', which is used to supply passwords and/or\n"
105 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300106 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400107 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800108 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
109 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100110 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
111 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300112 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200113 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
114 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
115 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300116 " 'output_filename' is the destination disk image filename\n"
117 " 'output_fmt' is the destination format\n"
118 " 'options' is a comma separated list of format specific options in a\n"
119 " name=value format. Use -o ? for an overview of the options supported by the\n"
120 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800121 " 'snapshot_param' is param used for internal snapshot, format\n"
122 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
123 " '[ID_OR_NAME]'\n"
124 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
125 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300126 " '-c' indicates that target image must be compressed (qcow format only)\n"
127 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
128 " match exactly. The image doesn't need a working backing file before\n"
129 " rebasing in this case (useful for renaming the backing file)\n"
130 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200131 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100132 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200133 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
134 " contain only zeros for qemu-img to create a sparse image during\n"
135 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
136 " unallocated or zero sectors, and the destination image will always be\n"
137 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200138 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100139 " '-n' skips the target volume creation (useful if the volume is created\n"
140 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300141 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200142 "Parameters to check subcommand:\n"
143 " '-r' tries to repair any inconsistencies that are found during the check.\n"
144 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
145 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200146 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200147 "\n"
malc3f020d72010-02-08 12:04:56 +0300148 "Parameters to snapshot subcommand:\n"
149 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
150 " '-a' applies a snapshot (revert disk to saved state)\n"
151 " '-c' creates a snapshot\n"
152 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100153 " '-l' lists all snapshots in the given image\n"
154 "\n"
155 "Parameters to compare subcommand:\n"
156 " '-f' first image format\n"
157 " '-F' second image format\n"
158 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100159
160 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100161 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000162 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800163 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000164}
165
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000166static QemuOptsList qemu_object_opts = {
167 .name = "object",
168 .implied_opt_name = "qom-type",
169 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
170 .desc = {
171 { }
172 },
173};
174
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000175static QemuOptsList qemu_source_opts = {
176 .name = "source",
177 .implied_opt_name = "file",
178 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
179 .desc = {
180 { }
181 },
182};
183
Stefan Weil7c30f652013-06-16 17:01:05 +0200184static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100185{
186 int ret = 0;
187 if (!quiet) {
188 va_list args;
189 va_start(args, fmt);
190 ret = vprintf(fmt, args);
191 va_end(args);
192 }
193 return ret;
194}
195
bellardea2384d2004-08-01 21:59:26 +0000196
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100197static int print_block_option_help(const char *filename, const char *fmt)
198{
199 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800200 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500201 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100202
203 /* Find driver and parse its options */
204 drv = bdrv_find_format(fmt);
205 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100206 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100207 return 1;
208 }
209
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800210 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100211 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500212 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100213 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100214 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800215 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100216 return 1;
217 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800218 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100219 }
220
Chunyan Liu83d05212014-06-05 17:20:51 +0800221 qemu_opts_print_help(create_opts);
222 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100223 return 0;
224}
225
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000226
227static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000228 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000229{
230 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000231 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000232
233 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000234 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
235 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000236 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
237 if (qemu_read_password(password, sizeof(password)) < 0) {
238 error_report("No password given");
239 return -1;
240 }
241 if (bdrv_set_key(bs, password) < 0) {
242 error_report("invalid password");
243 return -1;
244 }
245 }
246 return 0;
247}
248
249
Max Reitzefaa7c42016-03-16 19:54:38 +0100250static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100251 QemuOpts *opts, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000252 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000253{
254 QDict *options;
255 Error *local_err = NULL;
256 BlockBackend *blk;
257 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100258 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000259 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100260 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000261 return NULL;
262 }
Kevin Wolfce099542016-03-15 13:01:04 +0100263 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000264
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000265 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000266 blk_unref(blk);
267 return NULL;
268 }
269 return blk;
270}
271
Max Reitzefaa7c42016-03-16 19:54:38 +0100272static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000273 const char *fmt, int flags,
Kevin Wolfce099542016-03-15 13:01:04 +0100274 bool writethrough, bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000275{
276 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200277 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500278 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100279
bellard75c23802004-08-27 21:28:58 +0000280 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500281 options = qdict_new();
282 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000283 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100284
Max Reitzefaa7c42016-03-16 19:54:38 +0100285 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500286 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100287 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000288 return NULL;
bellard75c23802004-08-27 21:28:58 +0000289 }
Kevin Wolfce099542016-03-15 13:01:04 +0100290 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100291
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000292 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000293 blk_unref(blk);
294 return NULL;
bellard75c23802004-08-27 21:28:58 +0000295 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200296 return blk;
bellard75c23802004-08-27 21:28:58 +0000297}
298
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000299
Max Reitzefaa7c42016-03-16 19:54:38 +0100300static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000301 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100302 const char *fmt, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000303 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000304{
305 BlockBackend *blk;
306 if (image_opts) {
307 QemuOpts *opts;
308 if (fmt) {
309 error_report("--image-opts and --format are mutually exclusive");
310 return NULL;
311 }
312 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
313 filename, true);
314 if (!opts) {
315 return NULL;
316 }
Kevin Wolfce099542016-03-15 13:01:04 +0100317 blk = img_open_opts(filename, opts, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000318 } else {
Kevin Wolfce099542016-03-15 13:01:04 +0100319 blk = img_open_file(filename, fmt, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000320 }
321 return blk;
322}
323
324
Chunyan Liu83d05212014-06-05 17:20:51 +0800325static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100326 const char *base_filename,
327 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200328{
Markus Armbruster6750e792015-02-12 17:43:08 +0100329 Error *err = NULL;
330
Kevin Wolfefa84d42009-05-18 16:42:12 +0200331 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100332 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100333 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100334 error_report("Backing file not supported for file format '%s'",
335 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100336 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900337 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200338 }
339 }
340 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100341 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100342 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100343 error_report("Backing file format not supported for file "
344 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100345 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900346 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200347 }
348 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900349 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200350}
351
bellardea2384d2004-08-01 21:59:26 +0000352static int img_create(int argc, char **argv)
353{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200354 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100355 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000356 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000357 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000358 const char *filename;
359 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200360 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200361 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100362 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000363
bellardea2384d2004-08-01 21:59:26 +0000364 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000365 static const struct option long_options[] = {
366 {"help", no_argument, 0, 'h'},
367 {"object", required_argument, 0, OPTION_OBJECT},
368 {0, 0, 0, 0}
369 };
370 c = getopt_long(argc, argv, "F:b:f:he6o:q",
371 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100372 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000373 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100374 }
bellardea2384d2004-08-01 21:59:26 +0000375 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100376 case '?':
bellardea2384d2004-08-01 21:59:26 +0000377 case 'h':
378 help();
379 break;
aliguori9230eaf2009-03-28 17:55:19 +0000380 case 'F':
381 base_fmt = optarg;
382 break;
bellardea2384d2004-08-01 21:59:26 +0000383 case 'b':
384 base_filename = optarg;
385 break;
386 case 'f':
387 fmt = optarg;
388 break;
389 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200390 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100391 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100392 goto fail;
thsd8871c52007-10-24 16:11:42 +0000393 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200394 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100395 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100396 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200397 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100398 if (!is_valid_option_list(optarg)) {
399 error_report("Invalid option list: %s", optarg);
400 goto fail;
401 }
402 if (!options) {
403 options = g_strdup(optarg);
404 } else {
405 char *old_options = options;
406 options = g_strdup_printf("%s,%s", options, optarg);
407 g_free(old_options);
408 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200409 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100410 case 'q':
411 quiet = true;
412 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000413 case OPTION_OBJECT: {
414 QemuOpts *opts;
415 opts = qemu_opts_parse_noisily(&qemu_object_opts,
416 optarg, true);
417 if (!opts) {
418 goto fail;
419 }
420 } break;
bellardea2384d2004-08-01 21:59:26 +0000421 }
422 }
aliguori9230eaf2009-03-28 17:55:19 +0000423
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900424 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100425 filename = (optind < argc) ? argv[optind] : NULL;
426 if (options && has_help_option(options)) {
427 g_free(options);
428 return print_block_option_help(filename, fmt);
429 }
430
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100431 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800432 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100433 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100434 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900435
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000436 if (qemu_opts_foreach(&qemu_object_opts,
437 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200438 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000439 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 Wolf61de4c62016-03-18 17:46:45 +0100466 options, img_size, 0, &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. Berrangeeb769f72016-02-17 10:10:20 +0000600 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000601
602 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500603 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200604 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100605
aliguori15859692009-04-21 23:11:53 +0000606 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500607 int option_index = 0;
608 static const struct option long_options[] = {
609 {"help", no_argument, 0, 'h'},
610 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530611 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500612 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000613 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000614 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500615 {0, 0, 0, 0}
616 };
Max Reitz40055952014-07-22 22:58:42 +0200617 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500618 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100619 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000620 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100621 }
aliguori15859692009-04-21 23:11:53 +0000622 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100623 case '?':
aliguori15859692009-04-21 23:11:53 +0000624 case 'h':
625 help();
626 break;
627 case 'f':
628 fmt = optarg;
629 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200630 case 'r':
631 flags |= BDRV_O_RDWR;
632
633 if (!strcmp(optarg, "leaks")) {
634 fix = BDRV_FIX_LEAKS;
635 } else if (!strcmp(optarg, "all")) {
636 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
637 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800638 error_exit("Unknown option value for -r "
639 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200640 }
641 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500642 case OPTION_OUTPUT:
643 output = optarg;
644 break;
Max Reitz40055952014-07-22 22:58:42 +0200645 case 'T':
646 cache = optarg;
647 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100648 case 'q':
649 quiet = true;
650 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000651 case OPTION_OBJECT: {
652 QemuOpts *opts;
653 opts = qemu_opts_parse_noisily(&qemu_object_opts,
654 optarg, true);
655 if (!opts) {
656 return 1;
657 }
658 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000659 case OPTION_IMAGE_OPTS:
660 image_opts = true;
661 break;
aliguori15859692009-04-21 23:11:53 +0000662 }
663 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200664 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800665 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100666 }
aliguori15859692009-04-21 23:11:53 +0000667 filename = argv[optind++];
668
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500669 if (output && !strcmp(output, "json")) {
670 output_format = OFORMAT_JSON;
671 } else if (output && !strcmp(output, "human")) {
672 output_format = OFORMAT_HUMAN;
673 } else if (output) {
674 error_report("--output must be used with human or json as argument.");
675 return 1;
676 }
677
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000678 if (qemu_opts_foreach(&qemu_object_opts,
679 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200680 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000681 return 1;
682 }
683
Kevin Wolfce099542016-03-15 13:01:04 +0100684 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200685 if (ret < 0) {
686 error_report("Invalid source cache option: %s", cache);
687 return 1;
688 }
689
Kevin Wolfce099542016-03-15 13:01:04 +0100690 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200691 if (!blk) {
692 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900693 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200694 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500695
696 check = g_new0(ImageCheck, 1);
697 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200698
699 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200700 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200701 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500702 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200703 }
704
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500705 if (check->corruptions_fixed || check->leaks_fixed) {
706 int corruptions_fixed, leaks_fixed;
707
708 leaks_fixed = check->leaks_fixed;
709 corruptions_fixed = check->corruptions_fixed;
710
711 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100712 qprintf(quiet,
713 "The following inconsistencies were found and repaired:\n\n"
714 " %" PRId64 " leaked clusters\n"
715 " %" PRId64 " corruptions\n\n"
716 "Double checking the fixed image now...\n",
717 check->leaks_fixed,
718 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500719 }
720
721 ret = collect_image_check(bs, check, filename, fmt, 0);
722
723 check->leaks_fixed = leaks_fixed;
724 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200725 }
726
Max Reitz832390a2014-10-23 15:29:12 +0200727 if (!ret) {
728 switch (output_format) {
729 case OFORMAT_HUMAN:
730 dump_human_image_check(check, quiet);
731 break;
732 case OFORMAT_JSON:
733 dump_json_image_check(check, quiet);
734 break;
735 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500736 }
737
738 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200739 if (ret) {
740 error_report("Check failed: %s", strerror(-ret));
741 } else {
742 error_report("Check failed");
743 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500744 ret = 1;
745 goto fail;
746 }
747
748 if (check->corruptions) {
749 ret = 2;
750 } else if (check->leaks) {
751 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200752 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500753 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000754 }
755
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500756fail:
757 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200758 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500759 return ret;
aliguori15859692009-04-21 23:11:53 +0000760}
761
Max Reitzd4a32382014-10-24 15:57:37 +0200762typedef struct CommonBlockJobCBInfo {
763 BlockDriverState *bs;
764 Error **errp;
765} CommonBlockJobCBInfo;
766
767static void common_block_job_cb(void *opaque, int ret)
768{
769 CommonBlockJobCBInfo *cbi = opaque;
770
771 if (ret < 0) {
772 error_setg_errno(cbi->errp, -ret, "Block job failed");
773 }
Max Reitzd4a32382014-10-24 15:57:37 +0200774}
775
776static void run_block_job(BlockJob *job, Error **errp)
777{
778 AioContext *aio_context = bdrv_get_aio_context(job->bs);
779
780 do {
781 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500782 qemu_progress_print(job->len ?
783 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200784 } while (!job->ready);
785
786 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200787
788 /* A block job may finish instantaneously without publishing any progress,
789 * so just signal completion here */
790 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200791}
792
bellardea2384d2004-08-01 21:59:26 +0000793static int img_commit(int argc, char **argv)
794{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400795 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200796 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200797 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200798 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200799 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100800 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200801 Error *local_err = NULL;
802 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000803 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000804
805 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400806 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200807 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000808 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000809 static const struct option long_options[] = {
810 {"help", no_argument, 0, 'h'},
811 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000812 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000813 {0, 0, 0, 0}
814 };
815 c = getopt_long(argc, argv, "f:ht:b:dpq",
816 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100817 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000818 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100819 }
bellardea2384d2004-08-01 21:59:26 +0000820 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100821 case '?':
bellardea2384d2004-08-01 21:59:26 +0000822 case 'h':
823 help();
824 break;
825 case 'f':
826 fmt = optarg;
827 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400828 case 't':
829 cache = optarg;
830 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200831 case 'b':
832 base = optarg;
833 /* -b implies -d */
834 drop = true;
835 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200836 case 'd':
837 drop = true;
838 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200839 case 'p':
840 progress = true;
841 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100842 case 'q':
843 quiet = true;
844 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000845 case OPTION_OBJECT: {
846 QemuOpts *opts;
847 opts = qemu_opts_parse_noisily(&qemu_object_opts,
848 optarg, true);
849 if (!opts) {
850 return 1;
851 }
852 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000853 case OPTION_IMAGE_OPTS:
854 image_opts = true;
855 break;
bellardea2384d2004-08-01 21:59:26 +0000856 }
857 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200858
859 /* Progress is not shown in Quiet mode */
860 if (quiet) {
861 progress = false;
862 }
863
Kevin Wolffc11eb22013-08-05 10:53:04 +0200864 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800865 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100866 }
bellardea2384d2004-08-01 21:59:26 +0000867 filename = argv[optind++];
868
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000869 if (qemu_opts_foreach(&qemu_object_opts,
870 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200871 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000872 return 1;
873 }
874
Max Reitz9a86fe42014-10-24 15:57:38 +0200875 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100876 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400877 if (ret < 0) {
878 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100879 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400880 }
881
Kevin Wolfce099542016-03-15 13:01:04 +0100882 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200883 if (!blk) {
884 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900885 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200886 bs = blk_bs(blk);
887
Max Reitz687fa1d2014-10-24 15:57:39 +0200888 qemu_progress_init(progress, 1.f);
889 qemu_progress_print(0.f, 100);
890
Max Reitz1b22bff2014-10-24 15:57:40 +0200891 if (base) {
892 base_bs = bdrv_find_backing_image(bs, base);
893 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100894 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200895 goto done;
896 }
897 } else {
898 /* This is different from QMP, which by default uses the deepest file in
899 * the backing chain (i.e., the very base); however, the traditional
900 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200901 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200902 if (!base_bs) {
903 error_setg(&local_err, "Image does not have a backing file");
904 goto done;
905 }
bellardea2384d2004-08-01 21:59:26 +0000906 }
907
Max Reitzd4a32382014-10-24 15:57:37 +0200908 cbi = (CommonBlockJobCBInfo){
909 .errp = &local_err,
910 .bs = bs,
911 };
912
913 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
914 common_block_job_cb, &cbi, &local_err);
915 if (local_err) {
916 goto done;
917 }
918
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200919 /* When the block job completes, the BlockBackend reference will point to
920 * the old backing file. In order to avoid that the top image is already
921 * deleted, so we can still empty it afterwards, increment the reference
922 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200923 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200924 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200925 }
926
Max Reitzd4a32382014-10-24 15:57:37 +0200927 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200928 if (local_err) {
929 goto unref_backing;
930 }
931
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200932 if (!drop && bs->drv->bdrv_make_empty) {
933 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200934 if (ret) {
935 error_setg_errno(&local_err, -ret, "Could not empty %s",
936 filename);
937 goto unref_backing;
938 }
939 }
940
941unref_backing:
942 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200943 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200944 }
Max Reitzd4a32382014-10-24 15:57:37 +0200945
946done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200947 qemu_progress_end();
948
Markus Armbruster26f54e92014-10-07 13:59:04 +0200949 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200950
951 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100952 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900953 return 1;
954 }
Max Reitzd4a32382014-10-24 15:57:37 +0200955
956 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000957 return 0;
958}
959
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400960/*
thsf58c7b32008-06-05 21:53:49 +0000961 * Returns true iff the first sector pointed to by 'buf' contains at least
962 * a non-NUL byte.
963 *
964 * 'pnum' is set to the number of sectors (including and immediately following
965 * the first one) that are known to be in the same allocated/unallocated state.
966 */
bellardea2384d2004-08-01 21:59:26 +0000967static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
968{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000969 bool is_zero;
970 int i;
bellardea2384d2004-08-01 21:59:26 +0000971
972 if (n <= 0) {
973 *pnum = 0;
974 return 0;
975 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000976 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000977 for(i = 1; i < n; i++) {
978 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000979 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000980 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000981 }
bellardea2384d2004-08-01 21:59:26 +0000982 }
983 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000984 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000985}
986
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100987/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200988 * Like is_allocated_sectors, but if the buffer starts with a used sector,
989 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
990 * breaking up write requests for only small sparse areas.
991 */
992static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
993 int min)
994{
995 int ret;
996 int num_checked, num_used;
997
998 if (n < min) {
999 min = n;
1000 }
1001
1002 ret = is_allocated_sectors(buf, n, pnum);
1003 if (!ret) {
1004 return ret;
1005 }
1006
1007 num_used = *pnum;
1008 buf += BDRV_SECTOR_SIZE * *pnum;
1009 n -= *pnum;
1010 num_checked = num_used;
1011
1012 while (n > 0) {
1013 ret = is_allocated_sectors(buf, n, pnum);
1014
1015 buf += BDRV_SECTOR_SIZE * *pnum;
1016 n -= *pnum;
1017 num_checked += *pnum;
1018 if (ret) {
1019 num_used = num_checked;
1020 } else if (*pnum >= min) {
1021 break;
1022 }
1023 }
1024
1025 *pnum = num_used;
1026 return 1;
1027}
1028
1029/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001030 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1031 * buffers matches, non-zero otherwise.
1032 *
1033 * pnum is set to the number of sectors (including and immediately following
1034 * the first one) that are known to have the same comparison result
1035 */
1036static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1037 int *pnum)
1038{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001039 bool res;
1040 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001041
1042 if (n <= 0) {
1043 *pnum = 0;
1044 return 0;
1045 }
1046
1047 res = !!memcmp(buf1, buf2, 512);
1048 for(i = 1; i < n; i++) {
1049 buf1 += 512;
1050 buf2 += 512;
1051
1052 if (!!memcmp(buf1, buf2, 512) != res) {
1053 break;
1054 }
1055 }
1056
1057 *pnum = i;
1058 return res;
1059}
1060
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001061#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001062
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001063static int64_t sectors_to_bytes(int64_t sectors)
1064{
1065 return sectors << BDRV_SECTOR_BITS;
1066}
1067
1068static int64_t sectors_to_process(int64_t total, int64_t from)
1069{
1070 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1071}
1072
1073/*
1074 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1075 *
1076 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1077 * data and negative value on error.
1078 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001079 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001080 * @param sect_num: Number of first sector to check
1081 * @param sect_count: Number of sectors to check
1082 * @param filename: Name of disk file we are checking (logging purpose)
1083 * @param buffer: Allocated buffer for storing read data
1084 * @param quiet: Flag for quiet mode
1085 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001086static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001087 int sect_count, const char *filename,
1088 uint8_t *buffer, bool quiet)
1089{
1090 int pnum, ret = 0;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001091 ret = blk_read(blk, sect_num, buffer, sect_count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001092 if (ret < 0) {
1093 error_report("Error while reading offset %" PRId64 " of %s: %s",
1094 sectors_to_bytes(sect_num), filename, strerror(-ret));
1095 return ret;
1096 }
1097 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1098 if (ret || pnum != sect_count) {
1099 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1100 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1101 return 1;
1102 }
1103
1104 return 0;
1105}
1106
1107/*
1108 * Compares two images. Exit codes:
1109 *
1110 * 0 - Images are identical
1111 * 1 - Images differ
1112 * >1 - Error occurred
1113 */
1114static int img_compare(int argc, char **argv)
1115{
Max Reitz40055952014-07-22 22:58:42 +02001116 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001117 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001118 BlockDriverState *bs1, *bs2;
1119 int64_t total_sectors1, total_sectors2;
1120 uint8_t *buf1 = NULL, *buf2 = NULL;
1121 int pnum1, pnum2;
1122 int allocated1, allocated2;
1123 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1124 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001125 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001126 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001127 int64_t total_sectors;
1128 int64_t sector_num = 0;
1129 int64_t nb_sectors;
1130 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001131 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001132 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001133
Max Reitz40055952014-07-22 22:58:42 +02001134 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001135 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001136 static const struct option long_options[] = {
1137 {"help", no_argument, 0, 'h'},
1138 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001139 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001140 {0, 0, 0, 0}
1141 };
1142 c = getopt_long(argc, argv, "hf:F:T:pqs",
1143 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001144 if (c == -1) {
1145 break;
1146 }
1147 switch (c) {
1148 case '?':
1149 case 'h':
1150 help();
1151 break;
1152 case 'f':
1153 fmt1 = optarg;
1154 break;
1155 case 'F':
1156 fmt2 = optarg;
1157 break;
Max Reitz40055952014-07-22 22:58:42 +02001158 case 'T':
1159 cache = optarg;
1160 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001161 case 'p':
1162 progress = true;
1163 break;
1164 case 'q':
1165 quiet = true;
1166 break;
1167 case 's':
1168 strict = true;
1169 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001170 case OPTION_OBJECT: {
1171 QemuOpts *opts;
1172 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1173 optarg, true);
1174 if (!opts) {
1175 ret = 2;
1176 goto out4;
1177 }
1178 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001179 case OPTION_IMAGE_OPTS:
1180 image_opts = true;
1181 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001182 }
1183 }
1184
1185 /* Progress is not shown in Quiet mode */
1186 if (quiet) {
1187 progress = false;
1188 }
1189
1190
Kevin Wolffc11eb22013-08-05 10:53:04 +02001191 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001192 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001193 }
1194 filename1 = argv[optind++];
1195 filename2 = argv[optind++];
1196
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001197 if (qemu_opts_foreach(&qemu_object_opts,
1198 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001199 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001200 ret = 2;
1201 goto out4;
1202 }
1203
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001204 /* Initialize before goto out */
1205 qemu_progress_init(progress, 2.0);
1206
Kevin Wolfce099542016-03-15 13:01:04 +01001207 flags = 0;
1208 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001209 if (ret < 0) {
1210 error_report("Invalid source cache option: %s", cache);
1211 ret = 2;
1212 goto out3;
1213 }
1214
Kevin Wolfce099542016-03-15 13:01:04 +01001215 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001216 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001217 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001218 goto out3;
1219 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001220
Kevin Wolfce099542016-03-15 13:01:04 +01001221 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001222 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001223 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001224 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001226 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001227 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001228
Max Reitzf1d3cd72015-02-05 13:58:18 -05001229 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1230 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1231 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001232 if (total_sectors1 < 0) {
1233 error_report("Can't get size of %s: %s",
1234 filename1, strerror(-total_sectors1));
1235 ret = 4;
1236 goto out;
1237 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001238 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001239 if (total_sectors2 < 0) {
1240 error_report("Can't get size of %s: %s",
1241 filename2, strerror(-total_sectors2));
1242 ret = 4;
1243 goto out;
1244 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001245 total_sectors = MIN(total_sectors1, total_sectors2);
1246 progress_base = MAX(total_sectors1, total_sectors2);
1247
1248 qemu_progress_print(0, 100);
1249
1250 if (strict && total_sectors1 != total_sectors2) {
1251 ret = 1;
1252 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1253 goto out;
1254 }
1255
1256 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001257 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001258 BlockDriverState *file;
1259
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001260 nb_sectors = sectors_to_process(total_sectors, sector_num);
1261 if (nb_sectors <= 0) {
1262 break;
1263 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001264 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1265 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001266 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001267 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001268 ret = 3;
1269 error_report("Sector allocation test failed for %s", filename1);
1270 goto out;
1271 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001272 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001273
Fam Zheng25ad8e62016-01-13 16:37:41 +08001274 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1275 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001276 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001277 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001278 ret = 3;
1279 error_report("Sector allocation test failed for %s", filename2);
1280 goto out;
1281 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001282 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1283 if (pnum1) {
1284 nb_sectors = MIN(nb_sectors, pnum1);
1285 }
1286 if (pnum2) {
1287 nb_sectors = MIN(nb_sectors, pnum2);
1288 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001289
Fam Zheng25ad8e62016-01-13 16:37:41 +08001290 if (strict) {
1291 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1292 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1293 ret = 1;
1294 qprintf(quiet, "Strict mode: Offset %" PRId64
1295 " block status mismatch!\n",
1296 sectors_to_bytes(sector_num));
1297 goto out;
1298 }
1299 }
1300 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1301 nb_sectors = MIN(pnum1, pnum2);
1302 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001303 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001304 ret = blk_read(blk1, sector_num, buf1, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001305 if (ret < 0) {
1306 error_report("Error while reading offset %" PRId64 " of %s:"
1307 " %s", sectors_to_bytes(sector_num), filename1,
1308 strerror(-ret));
1309 ret = 4;
1310 goto out;
1311 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001312 ret = blk_read(blk2, sector_num, buf2, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001313 if (ret < 0) {
1314 error_report("Error while reading offset %" PRId64
1315 " of %s: %s", sectors_to_bytes(sector_num),
1316 filename2, strerror(-ret));
1317 ret = 4;
1318 goto out;
1319 }
1320 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1321 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001322 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1323 sectors_to_bytes(
1324 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001325 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001326 goto out;
1327 }
1328 }
1329 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001330
1331 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001332 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001333 filename1, buf1, quiet);
1334 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001335 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336 filename2, buf1, quiet);
1337 }
1338 if (ret) {
1339 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001340 error_report("Error while reading offset %" PRId64 ": %s",
1341 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001342 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001343 }
1344 goto out;
1345 }
1346 }
1347 sector_num += nb_sectors;
1348 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1349 }
1350
1351 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001352 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001353 int64_t total_sectors_over;
1354 const char *filename_over;
1355
1356 qprintf(quiet, "Warning: Image size mismatch!\n");
1357 if (total_sectors1 > total_sectors2) {
1358 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001359 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001360 filename_over = filename1;
1361 } else {
1362 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001363 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001364 filename_over = filename2;
1365 }
1366
1367 for (;;) {
1368 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1369 if (nb_sectors <= 0) {
1370 break;
1371 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001372 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001373 nb_sectors, &pnum);
1374 if (ret < 0) {
1375 ret = 3;
1376 error_report("Sector allocation test failed for %s",
1377 filename_over);
1378 goto out;
1379
1380 }
1381 nb_sectors = pnum;
1382 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001383 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384 filename_over, buf1, quiet);
1385 if (ret) {
1386 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001387 error_report("Error while reading offset %" PRId64
1388 " of %s: %s", sectors_to_bytes(sector_num),
1389 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001390 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001391 }
1392 goto out;
1393 }
1394 }
1395 sector_num += nb_sectors;
1396 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1397 }
1398 }
1399
1400 qprintf(quiet, "Images are identical.\n");
1401 ret = 0;
1402
1403out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001404 qemu_vfree(buf1);
1405 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001406 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001407out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001408 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409out3:
1410 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001411out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001412 return ret;
1413}
1414
Kevin Wolf690c7302015-03-19 13:33:32 +01001415enum ImgConvertBlockStatus {
1416 BLK_DATA,
1417 BLK_ZERO,
1418 BLK_BACKING_FILE,
1419};
1420
1421typedef struct ImgConvertState {
1422 BlockBackend **src;
1423 int64_t *src_sectors;
1424 int src_cur, src_num;
1425 int64_t src_cur_offset;
1426 int64_t total_sectors;
1427 int64_t allocated_sectors;
1428 enum ImgConvertBlockStatus status;
1429 int64_t sector_next_status;
1430 BlockBackend *target;
1431 bool has_zero_init;
1432 bool compressed;
1433 bool target_has_backing;
1434 int min_sparse;
1435 size_t cluster_sectors;
1436 size_t buf_sectors;
1437} ImgConvertState;
1438
1439static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1440{
1441 assert(sector_num >= s->src_cur_offset);
1442 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1443 s->src_cur_offset += s->src_sectors[s->src_cur];
1444 s->src_cur++;
1445 assert(s->src_cur < s->src_num);
1446 }
1447}
1448
1449static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1450{
1451 int64_t ret;
1452 int n;
1453
1454 convert_select_part(s, sector_num);
1455
1456 assert(s->total_sectors > sector_num);
1457 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1458
1459 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001460 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001461 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1462 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001463 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001464 if (ret < 0) {
1465 return ret;
1466 }
1467
1468 if (ret & BDRV_BLOCK_ZERO) {
1469 s->status = BLK_ZERO;
1470 } else if (ret & BDRV_BLOCK_DATA) {
1471 s->status = BLK_DATA;
1472 } else if (!s->target_has_backing) {
1473 /* Without a target backing file we must copy over the contents of
1474 * the backing file as well. */
1475 /* TODO Check block status of the backing file chain to avoid
1476 * needlessly reading zeroes and limiting the iteration to the
1477 * buffer size */
1478 s->status = BLK_DATA;
1479 } else {
1480 s->status = BLK_BACKING_FILE;
1481 }
1482
1483 s->sector_next_status = sector_num + n;
1484 }
1485
1486 n = MIN(n, s->sector_next_status - sector_num);
1487 if (s->status == BLK_DATA) {
1488 n = MIN(n, s->buf_sectors);
1489 }
1490
1491 /* We need to write complete clusters for compressed images, so if an
1492 * unallocated area is shorter than that, we must consider the whole
1493 * cluster allocated. */
1494 if (s->compressed) {
1495 if (n < s->cluster_sectors) {
1496 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1497 s->status = BLK_DATA;
1498 } else {
1499 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1500 }
1501 }
1502
1503 return n;
1504}
1505
1506static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1507 uint8_t *buf)
1508{
1509 int n;
1510 int ret;
1511
Kevin Wolf690c7302015-03-19 13:33:32 +01001512 assert(nb_sectors <= s->buf_sectors);
1513 while (nb_sectors > 0) {
1514 BlockBackend *blk;
1515 int64_t bs_sectors;
1516
1517 /* In the case of compression with multiple source files, we can get a
1518 * nb_sectors that spreads into the next part. So we must be able to
1519 * read across multiple BDSes for one convert_read() call. */
1520 convert_select_part(s, sector_num);
1521 blk = s->src[s->src_cur];
1522 bs_sectors = s->src_sectors[s->src_cur];
1523
1524 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
1525 ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n);
1526 if (ret < 0) {
1527 return ret;
1528 }
1529
1530 sector_num += n;
1531 nb_sectors -= n;
1532 buf += n * BDRV_SECTOR_SIZE;
1533 }
1534
1535 return 0;
1536}
1537
1538static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1539 const uint8_t *buf)
1540{
1541 int ret;
1542
1543 while (nb_sectors > 0) {
1544 int n = nb_sectors;
1545
1546 switch (s->status) {
1547 case BLK_BACKING_FILE:
1548 /* If we have a backing file, leave clusters unallocated that are
1549 * unallocated in the source image, so that the backing file is
1550 * visible at the respective offset. */
1551 assert(s->target_has_backing);
1552 break;
1553
1554 case BLK_DATA:
1555 /* We must always write compressed clusters as a whole, so don't
1556 * try to find zeroed parts in the buffer. We can only save the
1557 * write if the buffer is completely zeroed and we're allowed to
1558 * keep the target sparse. */
1559 if (s->compressed) {
1560 if (s->has_zero_init && s->min_sparse &&
1561 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1562 {
1563 assert(!s->target_has_backing);
1564 break;
1565 }
1566
1567 ret = blk_write_compressed(s->target, sector_num, buf, n);
1568 if (ret < 0) {
1569 return ret;
1570 }
1571 break;
1572 }
1573
1574 /* If there is real non-zero data or we're told to keep the target
1575 * fully allocated (-S 0), we must write it. Otherwise we can treat
1576 * it as zero sectors. */
1577 if (!s->min_sparse ||
1578 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1579 {
1580 ret = blk_write(s->target, sector_num, buf, n);
1581 if (ret < 0) {
1582 return ret;
1583 }
1584 break;
1585 }
1586 /* fall-through */
1587
1588 case BLK_ZERO:
1589 if (s->has_zero_init) {
1590 break;
1591 }
1592 ret = blk_write_zeroes(s->target, sector_num, n, 0);
1593 if (ret < 0) {
1594 return ret;
1595 }
1596 break;
1597 }
1598
1599 sector_num += n;
1600 nb_sectors -= n;
1601 buf += n * BDRV_SECTOR_SIZE;
1602 }
1603
1604 return 0;
1605}
1606
1607static int convert_do_copy(ImgConvertState *s)
1608{
1609 uint8_t *buf = NULL;
1610 int64_t sector_num, allocated_done;
1611 int ret;
1612 int n;
1613
1614 /* Check whether we have zero initialisation or can get it efficiently */
1615 s->has_zero_init = s->min_sparse && !s->target_has_backing
1616 ? bdrv_has_zero_init(blk_bs(s->target))
1617 : false;
1618
1619 if (!s->has_zero_init && !s->target_has_backing &&
1620 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1621 {
1622 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1623 if (ret == 0) {
1624 s->has_zero_init = true;
1625 }
1626 }
1627
1628 /* Allocate buffer for copied data. For compressed images, only one cluster
1629 * can be copied at a time. */
1630 if (s->compressed) {
1631 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1632 error_report("invalid cluster size");
1633 ret = -EINVAL;
1634 goto fail;
1635 }
1636 s->buf_sectors = s->cluster_sectors;
1637 }
1638 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1639
1640 /* Calculate allocated sectors for progress */
1641 s->allocated_sectors = 0;
1642 sector_num = 0;
1643 while (sector_num < s->total_sectors) {
1644 n = convert_iteration_sectors(s, sector_num);
1645 if (n < 0) {
1646 ret = n;
1647 goto fail;
1648 }
Max Reitzaad15de2016-03-24 23:33:57 +01001649 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1650 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001651 s->allocated_sectors += n;
1652 }
1653 sector_num += n;
1654 }
1655
1656 /* Do the copy */
1657 s->src_cur = 0;
1658 s->src_cur_offset = 0;
1659 s->sector_next_status = 0;
1660
1661 sector_num = 0;
1662 allocated_done = 0;
1663
1664 while (sector_num < s->total_sectors) {
1665 n = convert_iteration_sectors(s, sector_num);
1666 if (n < 0) {
1667 ret = n;
1668 goto fail;
1669 }
Max Reitzaad15de2016-03-24 23:33:57 +01001670 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1671 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001672 allocated_done += n;
1673 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1674 0);
1675 }
1676
Max Reitzaad15de2016-03-24 23:33:57 +01001677 if (s->status == BLK_DATA) {
1678 ret = convert_read(s, sector_num, n, buf);
1679 if (ret < 0) {
1680 error_report("error while reading sector %" PRId64
1681 ": %s", sector_num, strerror(-ret));
1682 goto fail;
1683 }
1684 } else if (!s->min_sparse && s->status == BLK_ZERO) {
1685 n = MIN(n, s->buf_sectors);
1686 memset(buf, 0, n * BDRV_SECTOR_SIZE);
1687 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001688 }
1689
1690 ret = convert_write(s, sector_num, n, buf);
1691 if (ret < 0) {
1692 error_report("error while writing sector %" PRId64
1693 ": %s", sector_num, strerror(-ret));
1694 goto fail;
1695 }
1696
1697 sector_num += n;
1698 }
1699
1700 if (s->compressed) {
1701 /* signal EOF to align */
1702 ret = blk_write_compressed(s->target, 0, NULL, 0);
1703 if (ret < 0) {
1704 goto fail;
1705 }
1706 }
1707
1708 ret = 0;
1709fail:
1710 qemu_vfree(buf);
1711 return ret;
1712}
1713
bellardea2384d2004-08-01 21:59:26 +00001714static int img_convert(int argc, char **argv)
1715{
Kevin Wolf690c7302015-03-19 13:33:32 +01001716 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001717 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001718 int progress = 0, flags, src_flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001719 bool writethrough, src_writethrough;
Max Reitz40055952014-07-22 22:58:42 +02001720 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001721 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001722 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001723 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001724 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001725 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001726 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001727 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001728 QemuOpts *opts = NULL;
1729 QemuOptsList *create_opts = NULL;
1730 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001731 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001732 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001733 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001734 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001735 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001736 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001737 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001738 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001739
1740 fmt = NULL;
1741 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001742 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001743 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001744 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001745 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001746 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001747 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001748 static const struct option long_options[] = {
1749 {"help", no_argument, 0, 'h'},
1750 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001751 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001752 {0, 0, 0, 0}
1753 };
1754 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1755 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001756 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001757 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001758 }
bellardea2384d2004-08-01 21:59:26 +00001759 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001760 case '?':
bellardea2384d2004-08-01 21:59:26 +00001761 case 'h':
1762 help();
1763 break;
1764 case 'f':
1765 fmt = optarg;
1766 break;
1767 case 'O':
1768 out_fmt = optarg;
1769 break;
thsf58c7b32008-06-05 21:53:49 +00001770 case 'B':
1771 out_baseimg = optarg;
1772 break;
bellardea2384d2004-08-01 21:59:26 +00001773 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001774 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001775 break;
1776 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001777 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001778 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001779 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001780 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001781 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001782 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001783 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001784 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001785 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001786 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001787 if (!is_valid_option_list(optarg)) {
1788 error_report("Invalid option list: %s", optarg);
1789 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001790 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001791 }
1792 if (!options) {
1793 options = g_strdup(optarg);
1794 } else {
1795 char *old_options = options;
1796 options = g_strdup_printf("%s,%s", options, optarg);
1797 g_free(old_options);
1798 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001799 break;
edison51ef6722010-09-21 19:58:41 -07001800 case 's':
1801 snapshot_name = optarg;
1802 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001803 case 'l':
1804 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001805 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1806 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001807 if (!sn_opts) {
1808 error_report("Failed in parsing snapshot param '%s'",
1809 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001810 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001811 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001812 }
1813 } else {
1814 snapshot_name = optarg;
1815 }
1816 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001817 case 'S':
1818 {
1819 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001820 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001821 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001822 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001823 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001824 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001825 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001826 }
1827
1828 min_sparse = sval / BDRV_SECTOR_SIZE;
1829 break;
1830 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001831 case 'p':
1832 progress = 1;
1833 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001834 case 't':
1835 cache = optarg;
1836 break;
Max Reitz40055952014-07-22 22:58:42 +02001837 case 'T':
1838 src_cache = optarg;
1839 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001840 case 'q':
1841 quiet = true;
1842 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001843 case 'n':
1844 skip_create = 1;
1845 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001846 case OPTION_OBJECT:
1847 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1848 optarg, true);
1849 if (!opts) {
1850 goto fail_getopt;
1851 }
1852 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001853 case OPTION_IMAGE_OPTS:
1854 image_opts = true;
1855 break;
bellardea2384d2004-08-01 21:59:26 +00001856 }
1857 }
ths3b46e622007-09-17 08:09:54 +00001858
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001859 if (qemu_opts_foreach(&qemu_object_opts,
1860 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001861 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001862 goto fail_getopt;
1863 }
1864
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001865 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001866 if (quiet) {
1867 progress = 0;
1868 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001869 qemu_progress_init(progress, 1.0);
1870
balrog926c2d22007-10-31 01:11:44 +00001871 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001872 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001873
Kevin Wolf2dc83282014-02-21 16:24:05 +01001874 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001875 ret = print_block_option_help(out_filename, out_fmt);
1876 goto out;
1877 }
1878
bellardea2384d2004-08-01 21:59:26 +00001879 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001880 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001881 }
bellardea2384d2004-08-01 21:59:26 +00001882
thsf58c7b32008-06-05 21:53:49 +00001883
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001884 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001885 error_report("-B makes no sense when concatenating multiple input "
1886 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001887 ret = -1;
1888 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001889 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001890
Kevin Wolfce099542016-03-15 13:01:04 +01001891 src_flags = 0;
1892 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001893 if (ret < 0) {
1894 error_report("Invalid source cache option: %s", src_cache);
1895 goto out;
1896 }
1897
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001898 qemu_progress_print(0, 100);
1899
Markus Armbruster26f54e92014-10-07 13:59:04 +02001900 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001901 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001902 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001903
1904 total_sectors = 0;
1905 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001906 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Kevin Wolfce099542016-03-15 13:01:04 +01001907 fmt, src_flags, src_writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001908 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001909 ret = -1;
1910 goto out;
1911 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001912 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001913 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001914 if (bs_sectors[bs_i] < 0) {
1915 error_report("Could not get size of %s: %s",
1916 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1917 ret = -1;
1918 goto out;
1919 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001920 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001921 }
bellardea2384d2004-08-01 21:59:26 +00001922
Wenchao Xiaef806542013-12-04 17:10:57 +08001923 if (sn_opts) {
1924 ret = bdrv_snapshot_load_tmp(bs[0],
1925 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1926 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1927 &local_err);
1928 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001929 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001930 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001931 ret = -1;
1932 goto out;
1933 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001934
1935 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001936 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001937 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001938 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001939 ret = -1;
1940 goto out;
edison51ef6722010-09-21 19:58:41 -07001941 }
1942
Kevin Wolfefa84d42009-05-18 16:42:12 +02001943 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001944 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001945 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001946 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001947 ret = -1;
1948 goto out;
1949 }
balrog926c2d22007-10-31 01:11:44 +00001950
Max Reitzb65a5e12015-02-05 13:58:12 -05001951 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001952 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001953 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001954 ret = -1;
1955 goto out;
1956 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001957
Max Reitz2e024cd2015-02-11 09:58:46 -05001958 if (!skip_create) {
1959 if (!drv->create_opts) {
1960 error_report("Format driver '%s' does not support image creation",
1961 drv->format_name);
1962 ret = -1;
1963 goto out;
1964 }
Max Reitzf75613c2014-12-02 18:32:46 +01001965
Max Reitz2e024cd2015-02-11 09:58:46 -05001966 if (!proto_drv->create_opts) {
1967 error_report("Protocol driver '%s' does not support image creation",
1968 proto_drv->format_name);
1969 ret = -1;
1970 goto out;
1971 }
Max Reitzf75613c2014-12-02 18:32:46 +01001972
Max Reitz2e024cd2015-02-11 09:58:46 -05001973 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1974 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001975
Max Reitz2e024cd2015-02-11 09:58:46 -05001976 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001977 if (options) {
1978 qemu_opts_do_parse(opts, options, NULL, &local_err);
1979 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01001980 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001981 ret = -1;
1982 goto out;
1983 }
Max Reitz2e024cd2015-02-11 09:58:46 -05001984 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001985
Markus Armbruster39101f22015-02-12 16:46:36 +01001986 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
1987 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05001988 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
1989 if (ret < 0) {
1990 goto out;
1991 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001992 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001993
Kevin Wolfa18953f2010-10-14 15:46:04 +02001994 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001995 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001996 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001997 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001998 }
1999
Kevin Wolfefa84d42009-05-18 16:42:12 +02002000 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01002001 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002002 bool encryption =
2003 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2004 const char *preallocation =
2005 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002006
2007 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002008 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002009 ret = -1;
2010 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002011 }
2012
Chunyan Liu83d05212014-06-05 17:20:51 +08002013 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002014 error_report("Compression and encryption not supported at "
2015 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002016 ret = -1;
2017 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002018 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002019
Chunyan Liu83d05212014-06-05 17:20:51 +08002020 if (preallocation
2021 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002022 {
2023 error_report("Compression and preallocation not supported at "
2024 "the same time");
2025 ret = -1;
2026 goto out;
2027 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002028 }
2029
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002030 if (!skip_create) {
2031 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002032 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002033 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002034 error_reportf_err(local_err, "%s: error while converting %s: ",
2035 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002036 goto out;
bellardea2384d2004-08-01 21:59:26 +00002037 }
2038 }
ths3b46e622007-09-17 08:09:54 +00002039
Peter Lieven5a37b602013-10-24 12:07:06 +02002040 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002041 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002042 if (ret < 0) {
2043 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002044 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002045 }
2046
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002047 /* XXX we should allow --image-opts to trigger use of
2048 * img_open() here, but then we have trouble with
2049 * the bdrv_create() call which takes different params.
2050 * Not critical right now, so fix can wait...
2051 */
Kevin Wolfce099542016-03-15 13:01:04 +01002052 out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002053 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002054 ret = -1;
2055 goto out;
2056 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002057 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002058
Peter Lievenf2521c92013-11-27 11:07:06 +01002059 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
2060 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2061 * as maximum. */
2062 bufsectors = MIN(32768,
2063 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
2064 out_bs->bl.discard_alignment))
2065 );
2066
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002067 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002068 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002069 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002070 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002071 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002072 ret = -1;
2073 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002074 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002075 error_report("output file is smaller than input file");
2076 ret = -1;
2077 goto out;
2078 }
2079 }
2080
Peter Lieven24f833c2013-11-27 11:07:07 +01002081 cluster_sectors = 0;
2082 ret = bdrv_get_info(out_bs, &bdi);
2083 if (ret < 0) {
2084 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002085 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002086 goto out;
2087 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002088 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002089 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002090 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2091 }
2092
Kevin Wolf690c7302015-03-19 13:33:32 +01002093 state = (ImgConvertState) {
2094 .src = blk,
2095 .src_sectors = bs_sectors,
2096 .src_num = bs_n,
2097 .total_sectors = total_sectors,
2098 .target = out_blk,
2099 .compressed = compress,
2100 .target_has_backing = (bool) out_baseimg,
2101 .min_sparse = min_sparse,
2102 .cluster_sectors = cluster_sectors,
2103 .buf_sectors = bufsectors,
2104 };
2105 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002106
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002107out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002108 if (!ret) {
2109 qemu_progress_print(100, 0);
2110 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002111 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002112 qemu_opts_del(opts);
2113 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002114 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002115 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002116 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002117 if (blk) {
2118 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2119 blk_unref(blk[bs_i]);
2120 }
2121 g_free(blk);
2122 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002123 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002124fail_getopt:
2125 g_free(options);
2126
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002127 if (ret) {
2128 return 1;
2129 }
bellardea2384d2004-08-01 21:59:26 +00002130 return 0;
2131}
2132
bellard57d1a2b2004-08-03 21:15:11 +00002133
bellardfaea38e2006-08-05 21:31:00 +00002134static void dump_snapshots(BlockDriverState *bs)
2135{
2136 QEMUSnapshotInfo *sn_tab, *sn;
2137 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002138
2139 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2140 if (nb_sns <= 0)
2141 return;
2142 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002143 bdrv_snapshot_dump(fprintf, stdout, NULL);
2144 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002145 for(i = 0; i < nb_sns; i++) {
2146 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002147 bdrv_snapshot_dump(fprintf, stdout, sn);
2148 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002149 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002150 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002151}
2152
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002153static void dump_json_image_info_list(ImageInfoList *list)
2154{
Markus Armbruster4399c432014-04-25 16:50:32 +02002155 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002156 QString *str;
2157 QmpOutputVisitor *ov = qmp_output_visitor_new();
2158 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002159 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2160 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002161 obj = qmp_output_get_qobject(ov);
2162 str = qobject_to_json_pretty(obj);
2163 assert(str != NULL);
2164 printf("%s\n", qstring_get_str(str));
2165 qobject_decref(obj);
2166 qmp_output_visitor_cleanup(ov);
2167 QDECREF(str);
2168}
2169
Benoît Canetc054b3f2012-09-05 13:09:02 +02002170static void dump_json_image_info(ImageInfo *info)
2171{
Markus Armbruster4399c432014-04-25 16:50:32 +02002172 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002173 QString *str;
2174 QmpOutputVisitor *ov = qmp_output_visitor_new();
2175 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002176 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002177 obj = qmp_output_get_qobject(ov);
2178 str = qobject_to_json_pretty(obj);
2179 assert(str != NULL);
2180 printf("%s\n", qstring_get_str(str));
2181 qobject_decref(obj);
2182 qmp_output_visitor_cleanup(ov);
2183 QDECREF(str);
2184}
2185
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002186static void dump_human_image_info_list(ImageInfoList *list)
2187{
2188 ImageInfoList *elem;
2189 bool delim = false;
2190
2191 for (elem = list; elem; elem = elem->next) {
2192 if (delim) {
2193 printf("\n");
2194 }
2195 delim = true;
2196
Wenchao Xia5b917042013-05-25 11:09:45 +08002197 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002198 }
2199}
2200
2201static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2202{
2203 return strcmp(a, b) == 0;
2204}
2205
2206/**
2207 * Open an image file chain and return an ImageInfoList
2208 *
2209 * @filename: topmost image filename
2210 * @fmt: topmost image format (may be NULL to autodetect)
2211 * @chain: true - enumerate entire backing file chain
2212 * false - only topmost image file
2213 *
2214 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2215 * image file. If there was an error a message will have been printed to
2216 * stderr.
2217 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002218static ImageInfoList *collect_image_info_list(bool image_opts,
2219 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002220 const char *fmt,
2221 bool chain)
2222{
2223 ImageInfoList *head = NULL;
2224 ImageInfoList **last = &head;
2225 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002226 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002227
2228 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2229
2230 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002231 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002232 BlockDriverState *bs;
2233 ImageInfo *info;
2234 ImageInfoList *elem;
2235
2236 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2237 error_report("Backing file '%s' creates an infinite loop.",
2238 filename);
2239 goto err;
2240 }
2241 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2242
Max Reitzefaa7c42016-03-16 19:54:38 +01002243 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002244 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002245 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002246 goto err;
2247 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002248 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002249
Wenchao Xia43526ec2013-06-06 12:27:58 +08002250 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002251 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002252 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002253 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002254 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002255 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002256
2257 elem = g_new0(ImageInfoList, 1);
2258 elem->value = info;
2259 *last = elem;
2260 last = &elem->next;
2261
Markus Armbruster26f54e92014-10-07 13:59:04 +02002262 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002263
2264 filename = fmt = NULL;
2265 if (chain) {
2266 if (info->has_full_backing_filename) {
2267 filename = info->full_backing_filename;
2268 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002269 error_report("Could not determine absolute backing filename,"
2270 " but backing filename '%s' present",
2271 info->backing_filename);
2272 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002273 }
2274 if (info->has_backing_filename_format) {
2275 fmt = info->backing_filename_format;
2276 }
2277 }
2278 }
2279 g_hash_table_destroy(filenames);
2280 return head;
2281
2282err:
2283 qapi_free_ImageInfoList(head);
2284 g_hash_table_destroy(filenames);
2285 return NULL;
2286}
2287
Benoît Canetc054b3f2012-09-05 13:09:02 +02002288static int img_info(int argc, char **argv)
2289{
2290 int c;
2291 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002292 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002293 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002294 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002295 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002296
bellardea2384d2004-08-01 21:59:26 +00002297 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002298 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002299 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002300 int option_index = 0;
2301 static const struct option long_options[] = {
2302 {"help", no_argument, 0, 'h'},
2303 {"format", required_argument, 0, 'f'},
2304 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002305 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002306 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002307 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002308 {0, 0, 0, 0}
2309 };
2310 c = getopt_long(argc, argv, "f:h",
2311 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002312 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002313 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002314 }
bellardea2384d2004-08-01 21:59:26 +00002315 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002316 case '?':
bellardea2384d2004-08-01 21:59:26 +00002317 case 'h':
2318 help();
2319 break;
2320 case 'f':
2321 fmt = optarg;
2322 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002323 case OPTION_OUTPUT:
2324 output = optarg;
2325 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002326 case OPTION_BACKING_CHAIN:
2327 chain = true;
2328 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002329 case OPTION_OBJECT: {
2330 QemuOpts *opts;
2331 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2332 optarg, true);
2333 if (!opts) {
2334 return 1;
2335 }
2336 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002337 case OPTION_IMAGE_OPTS:
2338 image_opts = true;
2339 break;
bellardea2384d2004-08-01 21:59:26 +00002340 }
2341 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002342 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002343 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002344 }
bellardea2384d2004-08-01 21:59:26 +00002345 filename = argv[optind++];
2346
Benoît Canetc054b3f2012-09-05 13:09:02 +02002347 if (output && !strcmp(output, "json")) {
2348 output_format = OFORMAT_JSON;
2349 } else if (output && !strcmp(output, "human")) {
2350 output_format = OFORMAT_HUMAN;
2351 } else if (output) {
2352 error_report("--output must be used with human or json as argument.");
2353 return 1;
2354 }
2355
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002356 if (qemu_opts_foreach(&qemu_object_opts,
2357 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002358 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002359 return 1;
2360 }
2361
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002362 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002363 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002364 return 1;
2365 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002366
Benoît Canetc054b3f2012-09-05 13:09:02 +02002367 switch (output_format) {
2368 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002369 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002370 break;
2371 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002372 if (chain) {
2373 dump_json_image_info_list(list);
2374 } else {
2375 dump_json_image_info(list->value);
2376 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002377 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002378 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002379
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002380 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002381 return 0;
2382}
2383
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002384static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2385 MapEntry *next)
2386{
2387 switch (output_format) {
2388 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002389 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002390 error_report("File contains external, encrypted or compressed clusters.");
2391 exit(1);
2392 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002393 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002394 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002395 e->start, e->length,
2396 e->has_offset ? e->offset : 0,
2397 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002398 }
2399 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2400 * Modify the flags here to allow more coalescing.
2401 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002402 if (next && (!next->data || next->zero)) {
2403 next->data = false;
2404 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002405 }
2406 break;
2407 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002408 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2409 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002410 (e->start == 0 ? "[" : ",\n"),
2411 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002412 e->zero ? "true" : "false",
2413 e->data ? "true" : "false");
2414 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002415 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002416 }
2417 putchar('}');
2418
2419 if (!next) {
2420 printf("]\n");
2421 }
2422 break;
2423 }
2424}
2425
2426static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2427 int nb_sectors, MapEntry *e)
2428{
2429 int64_t ret;
2430 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002431 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002432 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002433
2434 /* As an optimization, we could cache the current range of unallocated
2435 * clusters in each file of the chain, and avoid querying the same
2436 * range repeatedly.
2437 */
2438
2439 depth = 0;
2440 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002441 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2442 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002443 if (ret < 0) {
2444 return ret;
2445 }
2446 assert(nb_sectors);
2447 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2448 break;
2449 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002450 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002451 if (bs == NULL) {
2452 ret = 0;
2453 break;
2454 }
2455
2456 depth++;
2457 }
2458
John Snow28756452016-02-05 13:12:33 -05002459 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2460
2461 *e = (MapEntry) {
2462 .start = sector_num * BDRV_SECTOR_SIZE,
2463 .length = nb_sectors * BDRV_SECTOR_SIZE,
2464 .data = !!(ret & BDRV_BLOCK_DATA),
2465 .zero = !!(ret & BDRV_BLOCK_ZERO),
2466 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2467 .has_offset = has_offset,
2468 .depth = depth,
2469 .has_filename = file && has_offset,
2470 .filename = file && has_offset ? file->filename : NULL,
2471 };
2472
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002473 return 0;
2474}
2475
Fam Zheng16b0d552016-01-26 11:59:02 +08002476static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2477{
2478 if (curr->length == 0) {
2479 return false;
2480 }
2481 if (curr->zero != next->zero ||
2482 curr->data != next->data ||
2483 curr->depth != next->depth ||
2484 curr->has_filename != next->has_filename ||
2485 curr->has_offset != next->has_offset) {
2486 return false;
2487 }
2488 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2489 return false;
2490 }
2491 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2492 return false;
2493 }
2494 return true;
2495}
2496
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002497static int img_map(int argc, char **argv)
2498{
2499 int c;
2500 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002501 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002502 BlockDriverState *bs;
2503 const char *filename, *fmt, *output;
2504 int64_t length;
2505 MapEntry curr = { .length = 0 }, next;
2506 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002507 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002508
2509 fmt = NULL;
2510 output = NULL;
2511 for (;;) {
2512 int option_index = 0;
2513 static const struct option long_options[] = {
2514 {"help", no_argument, 0, 'h'},
2515 {"format", required_argument, 0, 'f'},
2516 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002517 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002518 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002519 {0, 0, 0, 0}
2520 };
2521 c = getopt_long(argc, argv, "f:h",
2522 long_options, &option_index);
2523 if (c == -1) {
2524 break;
2525 }
2526 switch (c) {
2527 case '?':
2528 case 'h':
2529 help();
2530 break;
2531 case 'f':
2532 fmt = optarg;
2533 break;
2534 case OPTION_OUTPUT:
2535 output = optarg;
2536 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002537 case OPTION_OBJECT: {
2538 QemuOpts *opts;
2539 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2540 optarg, true);
2541 if (!opts) {
2542 return 1;
2543 }
2544 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002545 case OPTION_IMAGE_OPTS:
2546 image_opts = true;
2547 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002548 }
2549 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002550 if (optind != argc - 1) {
2551 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002552 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002553 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002554
2555 if (output && !strcmp(output, "json")) {
2556 output_format = OFORMAT_JSON;
2557 } else if (output && !strcmp(output, "human")) {
2558 output_format = OFORMAT_HUMAN;
2559 } else if (output) {
2560 error_report("--output must be used with human or json as argument.");
2561 return 1;
2562 }
2563
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002564 if (qemu_opts_foreach(&qemu_object_opts,
2565 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002566 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002567 return 1;
2568 }
2569
Kevin Wolfce099542016-03-15 13:01:04 +01002570 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002571 if (!blk) {
2572 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002573 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002574 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002575
2576 if (output_format == OFORMAT_HUMAN) {
2577 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2578 }
2579
Max Reitzf1d3cd72015-02-05 13:58:18 -05002580 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002581 while (curr.start + curr.length < length) {
2582 int64_t nsectors_left;
2583 int64_t sector_num;
2584 int n;
2585
2586 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2587
2588 /* Probe up to 1 GiB at a time. */
2589 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2590 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2591 ret = get_block_status(bs, sector_num, n, &next);
2592
2593 if (ret < 0) {
2594 error_report("Could not read file metadata: %s", strerror(-ret));
2595 goto out;
2596 }
2597
Fam Zheng16b0d552016-01-26 11:59:02 +08002598 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002599 curr.length += next.length;
2600 continue;
2601 }
2602
2603 if (curr.length > 0) {
2604 dump_map_entry(output_format, &curr, &next);
2605 }
2606 curr = next;
2607 }
2608
2609 dump_map_entry(output_format, &curr, NULL);
2610
2611out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002612 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002613 return ret < 0;
2614}
2615
aliguorif7b4a942009-01-07 17:40:15 +00002616#define SNAPSHOT_LIST 1
2617#define SNAPSHOT_CREATE 2
2618#define SNAPSHOT_APPLY 3
2619#define SNAPSHOT_DELETE 4
2620
Stuart Brady153859b2009-06-07 00:42:17 +01002621static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002622{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002623 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002624 BlockDriverState *bs;
2625 QEMUSnapshotInfo sn;
2626 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002627 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002628 int action = 0;
2629 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002630 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002631 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002632 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002633
Kevin Wolfce099542016-03-15 13:01:04 +01002634 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002635 /* Parse commandline parameters */
2636 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002637 static const struct option long_options[] = {
2638 {"help", no_argument, 0, 'h'},
2639 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002640 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002641 {0, 0, 0, 0}
2642 };
2643 c = getopt_long(argc, argv, "la:c:d:hq",
2644 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002645 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002646 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002647 }
aliguorif7b4a942009-01-07 17:40:15 +00002648 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002649 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002650 case 'h':
2651 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002652 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002653 case 'l':
2654 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002655 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002656 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002657 }
2658 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002659 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002660 break;
2661 case 'a':
2662 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002663 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002664 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002665 }
2666 action = SNAPSHOT_APPLY;
2667 snapshot_name = optarg;
2668 break;
2669 case 'c':
2670 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002671 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002672 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002673 }
2674 action = SNAPSHOT_CREATE;
2675 snapshot_name = optarg;
2676 break;
2677 case 'd':
2678 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002679 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002680 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002681 }
2682 action = SNAPSHOT_DELETE;
2683 snapshot_name = optarg;
2684 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002685 case 'q':
2686 quiet = true;
2687 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002688 case OPTION_OBJECT: {
2689 QemuOpts *opts;
2690 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2691 optarg, true);
2692 if (!opts) {
2693 return 1;
2694 }
2695 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002696 case OPTION_IMAGE_OPTS:
2697 image_opts = true;
2698 break;
aliguorif7b4a942009-01-07 17:40:15 +00002699 }
2700 }
2701
Kevin Wolffc11eb22013-08-05 10:53:04 +02002702 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002703 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002704 }
aliguorif7b4a942009-01-07 17:40:15 +00002705 filename = argv[optind++];
2706
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002707 if (qemu_opts_foreach(&qemu_object_opts,
2708 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002709 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002710 return 1;
2711 }
2712
aliguorif7b4a942009-01-07 17:40:15 +00002713 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002714 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002715 if (!blk) {
2716 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002717 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002718 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002719
2720 /* Perform the requested action */
2721 switch(action) {
2722 case SNAPSHOT_LIST:
2723 dump_snapshots(bs);
2724 break;
2725
2726 case SNAPSHOT_CREATE:
2727 memset(&sn, 0, sizeof(sn));
2728 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2729
2730 qemu_gettimeofday(&tv);
2731 sn.date_sec = tv.tv_sec;
2732 sn.date_nsec = tv.tv_usec * 1000;
2733
2734 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002735 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002736 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002737 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002738 }
aliguorif7b4a942009-01-07 17:40:15 +00002739 break;
2740
2741 case SNAPSHOT_APPLY:
2742 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002743 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002744 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002745 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002746 }
aliguorif7b4a942009-01-07 17:40:15 +00002747 break;
2748
2749 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002750 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002751 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002752 error_reportf_err(err, "Could not delete snapshot '%s': ",
2753 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002754 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002755 }
aliguorif7b4a942009-01-07 17:40:15 +00002756 break;
2757 }
2758
2759 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002760 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002761 if (ret) {
2762 return 1;
2763 }
Stuart Brady153859b2009-06-07 00:42:17 +01002764 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002765}
2766
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002767static int img_rebase(int argc, char **argv)
2768{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002769 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002770 uint8_t *buf_old = NULL;
2771 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002772 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002773 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002774 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2775 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002776 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002777 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002778 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002779 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002780 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002781 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002782
2783 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002784 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002785 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002786 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002787 out_baseimg = NULL;
2788 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002789 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002790 static const struct option long_options[] = {
2791 {"help", no_argument, 0, 'h'},
2792 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002793 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002794 {0, 0, 0, 0}
2795 };
2796 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2797 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002798 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002799 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002800 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002801 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002802 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002803 case 'h':
2804 help();
2805 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002806 case 'f':
2807 fmt = optarg;
2808 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002809 case 'F':
2810 out_basefmt = optarg;
2811 break;
2812 case 'b':
2813 out_baseimg = optarg;
2814 break;
2815 case 'u':
2816 unsafe = 1;
2817 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002818 case 'p':
2819 progress = 1;
2820 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002821 case 't':
2822 cache = optarg;
2823 break;
Max Reitz40055952014-07-22 22:58:42 +02002824 case 'T':
2825 src_cache = optarg;
2826 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002827 case 'q':
2828 quiet = true;
2829 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002830 case OPTION_OBJECT: {
2831 QemuOpts *opts;
2832 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2833 optarg, true);
2834 if (!opts) {
2835 return 1;
2836 }
2837 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002838 case OPTION_IMAGE_OPTS:
2839 image_opts = true;
2840 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002841 }
2842 }
2843
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002844 if (quiet) {
2845 progress = 0;
2846 }
2847
Fam Zhengac1307a2014-04-22 13:36:11 +08002848 if (optind != argc - 1) {
2849 error_exit("Expecting one image file name");
2850 }
2851 if (!unsafe && !out_baseimg) {
2852 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002853 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002854 filename = argv[optind++];
2855
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002856 if (qemu_opts_foreach(&qemu_object_opts,
2857 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002858 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002859 return 1;
2860 }
2861
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002862 qemu_progress_init(progress, 2.0);
2863 qemu_progress_print(0, 100);
2864
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002865 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01002866 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002867 if (ret < 0) {
2868 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002869 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002870 }
2871
Kevin Wolfce099542016-03-15 13:01:04 +01002872 src_flags = 0;
2873 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02002874 if (ret < 0) {
2875 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002876 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002877 }
2878
Kevin Wolfce099542016-03-15 13:01:04 +01002879 /* The source files are opened read-only, don't care about WCE */
2880 assert((src_flags & BDRV_O_RDWR) == 0);
2881 (void) src_writethrough;
2882
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002883 /*
2884 * Open the images.
2885 *
2886 * Ignore the old backing file for unsafe rebase in case we want to correct
2887 * the reference to a renamed or moved backing file.
2888 */
Kevin Wolfce099542016-03-15 13:01:04 +01002889 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002890 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002891 ret = -1;
2892 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002893 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002894 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002895
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002896 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002897 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002898 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002899 ret = -1;
2900 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002901 }
2902 }
2903
2904 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002905 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002906 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002907 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002908
Max Reitz644483d2015-02-05 13:58:17 -05002909 if (bs->backing_format[0] != '\0') {
2910 options = qdict_new();
2911 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2912 }
2913
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002914 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002915 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002916 options, src_flags, &local_err);
2917 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002918 error_reportf_err(local_err,
2919 "Could not open old backing file '%s': ",
2920 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002921 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002922 }
Max Reitz644483d2015-02-05 13:58:17 -05002923
Alex Bligha6166732012-10-16 13:46:18 +01002924 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002925 if (out_basefmt) {
2926 options = qdict_new();
2927 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2928 } else {
2929 options = NULL;
2930 }
2931
Max Reitzefaa7c42016-03-16 19:54:38 +01002932 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002933 options, src_flags, &local_err);
2934 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002935 error_reportf_err(local_err,
2936 "Could not open new backing file '%s': ",
2937 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002938 goto out;
2939 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002940 }
2941 }
2942
2943 /*
2944 * Check each unallocated cluster in the COW file. If it is unallocated,
2945 * accesses go to the backing file. We must therefore compare this cluster
2946 * in the old and new backing file, and if they differ we need to copy it
2947 * from the old backing file into the COW file.
2948 *
2949 * If qemu-img crashes during this step, no harm is done. The content of
2950 * the image is the same as the original one at any time.
2951 */
2952 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002953 int64_t num_sectors;
2954 int64_t old_backing_num_sectors;
2955 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002956 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002957 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002958 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002959
Max Reitzf1d3cd72015-02-05 13:58:18 -05002960 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2961 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002962
Max Reitzf1d3cd72015-02-05 13:58:18 -05002963 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002964 if (num_sectors < 0) {
2965 error_report("Could not get size of '%s': %s",
2966 filename, strerror(-num_sectors));
2967 ret = -1;
2968 goto out;
2969 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002970 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002971 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002972 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002973
2974 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2975 error_report("Could not get size of '%s': %s",
2976 backing_name, strerror(-old_backing_num_sectors));
2977 ret = -1;
2978 goto out;
2979 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002980 if (blk_new_backing) {
2981 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002982 if (new_backing_num_sectors < 0) {
2983 error_report("Could not get size of '%s': %s",
2984 out_baseimg, strerror(-new_backing_num_sectors));
2985 ret = -1;
2986 goto out;
2987 }
Alex Bligha6166732012-10-16 13:46:18 +01002988 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002989
Kevin Wolf1f710492012-10-12 14:29:18 +02002990 if (num_sectors != 0) {
2991 local_progress = (float)100 /
2992 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2993 }
2994
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002995 for (sector = 0; sector < num_sectors; sector += n) {
2996
2997 /* How many sectors can we handle with the next read? */
2998 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2999 n = (IO_BUF_SIZE / 512);
3000 } else {
3001 n = num_sectors - sector;
3002 }
3003
3004 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003005 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003006 if (ret < 0) {
3007 error_report("error while reading image metadata: %s",
3008 strerror(-ret));
3009 goto out;
3010 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003011 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003012 continue;
3013 }
3014
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003015 /*
3016 * Read old and new backing file and take into consideration that
3017 * backing files may be smaller than the COW image.
3018 */
3019 if (sector >= old_backing_num_sectors) {
3020 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3021 } else {
3022 if (sector + n > old_backing_num_sectors) {
3023 n = old_backing_num_sectors - sector;
3024 }
3025
Max Reitzf1d3cd72015-02-05 13:58:18 -05003026 ret = blk_read(blk_old_backing, sector, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003027 if (ret < 0) {
3028 error_report("error while reading from old backing file");
3029 goto out;
3030 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003031 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003032
Max Reitzf1d3cd72015-02-05 13:58:18 -05003033 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003034 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3035 } else {
3036 if (sector + n > new_backing_num_sectors) {
3037 n = new_backing_num_sectors - sector;
3038 }
3039
Max Reitzf1d3cd72015-02-05 13:58:18 -05003040 ret = blk_read(blk_new_backing, sector, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003041 if (ret < 0) {
3042 error_report("error while reading from new backing file");
3043 goto out;
3044 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003045 }
3046
3047 /* If they differ, we need to write to the COW file */
3048 uint64_t written = 0;
3049
3050 while (written < n) {
3051 int pnum;
3052
3053 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003054 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003055 {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003056 ret = blk_write(blk, sector + written,
3057 buf_old + written * 512, pnum);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003058 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003059 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003060 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003061 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003062 }
3063 }
3064
3065 written += pnum;
3066 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003067 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003068 }
3069 }
3070
3071 /*
3072 * Change the backing file. All clusters that are different from the old
3073 * backing file are overwritten in the COW file now, so the visible content
3074 * doesn't change when we switch the backing file.
3075 */
Alex Bligha6166732012-10-16 13:46:18 +01003076 if (out_baseimg && *out_baseimg) {
3077 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3078 } else {
3079 ret = bdrv_change_backing_file(bs, NULL, NULL);
3080 }
3081
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003082 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003083 error_report("Could not change the backing file to '%s': No "
3084 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003085 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003086 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003087 out_baseimg, strerror(-ret));
3088 }
3089
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003090 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003091 /*
3092 * TODO At this point it is possible to check if any clusters that are
3093 * allocated in the COW file are the same in the backing file. If so, they
3094 * could be dropped from the COW file. Don't do this before switching the
3095 * backing file, in case of a crash this would lead to corruption.
3096 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003097out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003098 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003099 /* Cleanup */
3100 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003101 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003102 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003103 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003104 qemu_vfree(buf_old);
3105 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003106
Markus Armbruster26f54e92014-10-07 13:59:04 +02003107 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003108 if (ret) {
3109 return 1;
3110 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003111 return 0;
3112}
3113
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003114static int img_resize(int argc, char **argv)
3115{
Markus Armbruster6750e792015-02-12 17:43:08 +01003116 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003117 int c, ret, relative;
3118 const char *filename, *fmt, *size;
3119 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003120 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003121 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003122 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003123
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003124 static QemuOptsList resize_options = {
3125 .name = "resize_options",
3126 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3127 .desc = {
3128 {
3129 .name = BLOCK_OPT_SIZE,
3130 .type = QEMU_OPT_SIZE,
3131 .help = "Virtual disk size"
3132 }, {
3133 /* end of list */
3134 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003135 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003136 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003137 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003138
Kevin Wolfe80fec72011-04-29 10:58:12 +02003139 /* Remove size from argv manually so that negative numbers are not treated
3140 * as options by getopt. */
3141 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003142 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003143 return 1;
3144 }
3145
3146 size = argv[--argc];
3147
3148 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003149 fmt = NULL;
3150 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003151 static const struct option long_options[] = {
3152 {"help", no_argument, 0, 'h'},
3153 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003154 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003155 {0, 0, 0, 0}
3156 };
3157 c = getopt_long(argc, argv, "f:hq",
3158 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003159 if (c == -1) {
3160 break;
3161 }
3162 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003163 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003164 case 'h':
3165 help();
3166 break;
3167 case 'f':
3168 fmt = optarg;
3169 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003170 case 'q':
3171 quiet = true;
3172 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003173 case OPTION_OBJECT: {
3174 QemuOpts *opts;
3175 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3176 optarg, true);
3177 if (!opts) {
3178 return 1;
3179 }
3180 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003181 case OPTION_IMAGE_OPTS:
3182 image_opts = true;
3183 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003184 }
3185 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003186 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003187 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003188 }
3189 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003190
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003191 if (qemu_opts_foreach(&qemu_object_opts,
3192 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003193 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003194 return 1;
3195 }
3196
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003197 /* Choose grow, shrink, or absolute resize mode */
3198 switch (size[0]) {
3199 case '+':
3200 relative = 1;
3201 size++;
3202 break;
3203 case '-':
3204 relative = -1;
3205 size++;
3206 break;
3207 default:
3208 relative = 0;
3209 break;
3210 }
3211
3212 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003213 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003214 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003215 if (err) {
3216 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003217 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003218 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003219 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003220 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003221 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3222 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003223
Max Reitzefaa7c42016-03-16 19:54:38 +01003224 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01003225 BDRV_O_RDWR, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003226 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003227 ret = -1;
3228 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003229 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003230
3231 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003232 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003233 } else {
3234 total_size = n;
3235 }
3236 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003237 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003238 ret = -1;
3239 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003240 }
3241
Max Reitzf1d3cd72015-02-05 13:58:18 -05003242 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003243 switch (ret) {
3244 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003245 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003246 break;
3247 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003248 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003249 break;
3250 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003251 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003252 break;
3253 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003254 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003255 break;
3256 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003257out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003258 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003259 if (ret) {
3260 return 1;
3261 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003262 return 0;
3263}
3264
Max Reitz76a3a342014-10-27 11:12:51 +01003265static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003266 int64_t offset, int64_t total_work_size,
3267 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003268{
3269 qemu_progress_print(100.f * offset / total_work_size, 0);
3270}
3271
Max Reitz6f176b42013-09-03 10:09:50 +02003272static int img_amend(int argc, char **argv)
3273{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003274 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003275 int c, ret = 0;
3276 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003277 QemuOptsList *create_opts = NULL;
3278 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003279 const char *fmt = NULL, *filename, *cache;
3280 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003281 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003282 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003283 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003284 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003285 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003286
Max Reitzbd39e6e2014-07-22 22:58:43 +02003287 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003288 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003289 static const struct option long_options[] = {
3290 {"help", no_argument, 0, 'h'},
3291 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003292 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003293 {0, 0, 0, 0}
3294 };
3295 c = getopt_long(argc, argv, "ho:f:t:pq",
3296 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003297 if (c == -1) {
3298 break;
3299 }
3300
3301 switch (c) {
3302 case 'h':
3303 case '?':
3304 help();
3305 break;
3306 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003307 if (!is_valid_option_list(optarg)) {
3308 error_report("Invalid option list: %s", optarg);
3309 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003310 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003311 }
3312 if (!options) {
3313 options = g_strdup(optarg);
3314 } else {
3315 char *old_options = options;
3316 options = g_strdup_printf("%s,%s", options, optarg);
3317 g_free(old_options);
3318 }
Max Reitz6f176b42013-09-03 10:09:50 +02003319 break;
3320 case 'f':
3321 fmt = optarg;
3322 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003323 case 't':
3324 cache = optarg;
3325 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003326 case 'p':
3327 progress = true;
3328 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003329 case 'q':
3330 quiet = true;
3331 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003332 case OPTION_OBJECT:
3333 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3334 optarg, true);
3335 if (!opts) {
3336 ret = -1;
3337 goto out_no_progress;
3338 }
3339 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003340 case OPTION_IMAGE_OPTS:
3341 image_opts = true;
3342 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003343 }
3344 }
3345
Max Reitz6f176b42013-09-03 10:09:50 +02003346 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003347 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003348 }
3349
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003350 if (qemu_opts_foreach(&qemu_object_opts,
3351 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003352 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003353 ret = -1;
3354 goto out_no_progress;
3355 }
3356
Max Reitz76a3a342014-10-27 11:12:51 +01003357 if (quiet) {
3358 progress = false;
3359 }
3360 qemu_progress_init(progress, 1.0);
3361
Kevin Wolfa283cb62014-02-21 16:24:07 +01003362 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3363 if (fmt && has_help_option(options)) {
3364 /* If a format is explicitly specified (and possibly no filename is
3365 * given), print option help here */
3366 ret = print_block_option_help(filename, fmt);
3367 goto out;
3368 }
3369
3370 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003371 error_report("Expecting one image file name");
3372 ret = -1;
3373 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003374 }
Max Reitz6f176b42013-09-03 10:09:50 +02003375
Kevin Wolfce099542016-03-15 13:01:04 +01003376 flags = BDRV_O_RDWR;
3377 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003378 if (ret < 0) {
3379 error_report("Invalid cache option: %s", cache);
3380 goto out;
3381 }
3382
Kevin Wolfce099542016-03-15 13:01:04 +01003383 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003384 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003385 ret = -1;
3386 goto out;
3387 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003388 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003389
3390 fmt = bs->drv->format_name;
3391
Kevin Wolf626f84f2014-02-21 16:24:06 +01003392 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003393 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003394 ret = print_block_option_help(filename, fmt);
3395 goto out;
3396 }
3397
Max Reitzb2439d22014-12-02 18:32:47 +01003398 if (!bs->drv->create_opts) {
3399 error_report("Format driver '%s' does not support any options to amend",
3400 fmt);
3401 ret = -1;
3402 goto out;
3403 }
3404
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003405 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003406 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003407 if (options) {
3408 qemu_opts_do_parse(opts, options, NULL, &err);
3409 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003410 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003411 ret = -1;
3412 goto out;
3413 }
Max Reitz6f176b42013-09-03 10:09:50 +02003414 }
3415
Max Reitz76a3a342014-10-27 11:12:51 +01003416 /* In case the driver does not call amend_status_cb() */
3417 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003418 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003419 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003420 if (ret < 0) {
3421 error_report("Error while amending options: %s", strerror(-ret));
3422 goto out;
3423 }
3424
3425out:
Max Reitz76a3a342014-10-27 11:12:51 +01003426 qemu_progress_end();
3427
Max Reitze814dff2015-08-20 16:00:38 -07003428out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003429 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003430 qemu_opts_del(opts);
3431 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003432 g_free(options);
3433
Max Reitz6f176b42013-09-03 10:09:50 +02003434 if (ret) {
3435 return 1;
3436 }
3437 return 0;
3438}
3439
Anthony Liguoric227f092009-10-01 16:12:16 -05003440static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003441#define DEF(option, callback, arg_string) \
3442 { option, callback },
3443#include "qemu-img-cmds.h"
3444#undef DEF
3445#undef GEN_DOCS
3446 { NULL, NULL, },
3447};
3448
bellardea2384d2004-08-01 21:59:26 +00003449int main(int argc, char **argv)
3450{
Anthony Liguoric227f092009-10-01 16:12:16 -05003451 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003452 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003453 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003454 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003455 static const struct option long_options[] = {
3456 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04003457 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04003458 {0, 0, 0, 0}
3459 };
bellardea2384d2004-08-01 21:59:26 +00003460
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003461#ifdef CONFIG_POSIX
3462 signal(SIGPIPE, SIG_IGN);
3463#endif
3464
Kevin Wolf53f76e52010-12-16 15:10:32 +01003465 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003466 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003467
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003468 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003469 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003470 exit(EXIT_FAILURE);
3471 }
3472
Daniel P. Berrangec2297082016-04-06 12:12:06 +01003473 if (qcrypto_init(&local_error) < 0) {
3474 error_reportf_err(local_error, "cannot initialize crypto: ");
3475 exit(1);
3476 }
3477
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003478 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003479 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003480 if (argc < 2) {
3481 error_exit("Not enough arguments");
3482 }
Stuart Brady153859b2009-06-07 00:42:17 +01003483 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01003484
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003485 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003486 qemu_add_opts(&qemu_source_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003487
Stuart Brady153859b2009-06-07 00:42:17 +01003488 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04003489 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01003490 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04003491 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01003492 }
bellardea2384d2004-08-01 21:59:26 +00003493 }
Stuart Brady153859b2009-06-07 00:42:17 +01003494
Jeff Cody5f6979c2014-04-28 14:37:18 -04003495 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04003496
3497 if (c == 'h') {
3498 help();
3499 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04003500 if (c == 'v') {
3501 printf(QEMU_IMG_VERSION);
3502 return 0;
3503 }
Jeff Cody7db16892014-04-25 17:02:32 -04003504
Stuart Brady153859b2009-06-07 00:42:17 +01003505 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003506 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003507}