blob: 480ef8dba39f0557888207cffa5c371ae164fa7b [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,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020056 OPTION_PATTERN = 260,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050057};
58
59typedef enum OutputFormat {
60 OFORMAT_JSON,
61 OFORMAT_HUMAN,
62} OutputFormat;
63
Kevin Wolfe6996142016-03-15 13:03:11 +010064/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040065#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000066
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010067static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000068{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010069 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000070}
71
Fam Zhengac1307a2014-04-22 13:36:11 +080072static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
73{
74 va_list ap;
75
76 error_printf("qemu-img: ");
77
78 va_start(ap, fmt);
79 error_vprintf(fmt, ap);
80 va_end(ap);
81
82 error_printf("\nTry 'qemu-img --help' for more information\n");
83 exit(EXIT_FAILURE);
84}
85
blueswir1d2c639d2009-01-24 18:19:25 +000086/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080087static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000088{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010089 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040090 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030091 "usage: qemu-img command [command options]\n"
92 "QEMU disk image utility\n"
93 "\n"
94 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010095#define DEF(option, callback, arg_string) \
96 " " arg_string "\n"
97#include "qemu-img-cmds.h"
98#undef DEF
99#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300100 "\n"
101 "Command parameters:\n"
102 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000103 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
104 " manual page for a description of the object properties. The most common\n"
105 " object type is a 'secret', which is used to supply passwords and/or\n"
106 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300107 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400108 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800109 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
110 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100111 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
112 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300113 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200114 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
115 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
116 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300117 " 'output_filename' is the destination disk image filename\n"
118 " 'output_fmt' is the destination format\n"
119 " 'options' is a comma separated list of format specific options in a\n"
120 " name=value format. Use -o ? for an overview of the options supported by the\n"
121 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800122 " 'snapshot_param' is param used for internal snapshot, format\n"
123 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
124 " '[ID_OR_NAME]'\n"
125 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
126 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300127 " '-c' indicates that target image must be compressed (qcow format only)\n"
128 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
129 " match exactly. The image doesn't need a working backing file before\n"
130 " rebasing in this case (useful for renaming the backing file)\n"
131 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200132 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100133 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200134 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
135 " contain only zeros for qemu-img to create a sparse image during\n"
136 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
137 " unallocated or zero sectors, and the destination image will always be\n"
138 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200139 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100140 " '-n' skips the target volume creation (useful if the volume is created\n"
141 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300142 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200143 "Parameters to check subcommand:\n"
144 " '-r' tries to repair any inconsistencies that are found during the check.\n"
145 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
146 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200147 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200148 "\n"
malc3f020d72010-02-08 12:04:56 +0300149 "Parameters to snapshot subcommand:\n"
150 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
151 " '-a' applies a snapshot (revert disk to saved state)\n"
152 " '-c' creates a snapshot\n"
153 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100154 " '-l' lists all snapshots in the given image\n"
155 "\n"
156 "Parameters to compare subcommand:\n"
157 " '-f' first image format\n"
158 " '-F' second image format\n"
159 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100160
161 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100162 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000163 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800164 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000165}
166
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000167static QemuOptsList qemu_object_opts = {
168 .name = "object",
169 .implied_opt_name = "qom-type",
170 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
171 .desc = {
172 { }
173 },
174};
175
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000176static QemuOptsList qemu_source_opts = {
177 .name = "source",
178 .implied_opt_name = "file",
179 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
180 .desc = {
181 { }
182 },
183};
184
Stefan Weil7c30f652013-06-16 17:01:05 +0200185static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100186{
187 int ret = 0;
188 if (!quiet) {
189 va_list args;
190 va_start(args, fmt);
191 ret = vprintf(fmt, args);
192 va_end(args);
193 }
194 return ret;
195}
196
bellardea2384d2004-08-01 21:59:26 +0000197
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100198static int print_block_option_help(const char *filename, const char *fmt)
199{
200 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800201 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500202 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100203
204 /* Find driver and parse its options */
205 drv = bdrv_find_format(fmt);
206 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100207 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100208 return 1;
209 }
210
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800211 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100212 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500213 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100214 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100215 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800216 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100217 return 1;
218 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800219 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100220 }
221
Chunyan Liu83d05212014-06-05 17:20:51 +0800222 qemu_opts_print_help(create_opts);
223 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100224 return 0;
225}
226
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000227
228static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000229 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000230{
231 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000232 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000233
234 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000235 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
236 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000237 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
238 if (qemu_read_password(password, sizeof(password)) < 0) {
239 error_report("No password given");
240 return -1;
241 }
242 if (bdrv_set_key(bs, password) < 0) {
243 error_report("invalid password");
244 return -1;
245 }
246 }
247 return 0;
248}
249
250
Max Reitzefaa7c42016-03-16 19:54:38 +0100251static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100252 QemuOpts *opts, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000253 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000254{
255 QDict *options;
256 Error *local_err = NULL;
257 BlockBackend *blk;
258 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100259 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000260 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100261 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000262 return NULL;
263 }
Kevin Wolfce099542016-03-15 13:01:04 +0100264 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000265
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000266 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000267 blk_unref(blk);
268 return NULL;
269 }
270 return blk;
271}
272
Max Reitzefaa7c42016-03-16 19:54:38 +0100273static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000274 const char *fmt, int flags,
Kevin Wolfce099542016-03-15 13:01:04 +0100275 bool writethrough, bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000276{
277 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200278 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500279 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100280
bellard75c23802004-08-27 21:28:58 +0000281 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500282 options = qdict_new();
283 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000284 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100285
Max Reitzefaa7c42016-03-16 19:54:38 +0100286 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500287 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100288 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289 return NULL;
bellard75c23802004-08-27 21:28:58 +0000290 }
Kevin Wolfce099542016-03-15 13:01:04 +0100291 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100292
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000293 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000294 blk_unref(blk);
295 return NULL;
bellard75c23802004-08-27 21:28:58 +0000296 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200297 return blk;
bellard75c23802004-08-27 21:28:58 +0000298}
299
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300
Max Reitzefaa7c42016-03-16 19:54:38 +0100301static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000302 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100303 const char *fmt, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000304 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000305{
306 BlockBackend *blk;
307 if (image_opts) {
308 QemuOpts *opts;
309 if (fmt) {
310 error_report("--image-opts and --format are mutually exclusive");
311 return NULL;
312 }
313 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
314 filename, true);
315 if (!opts) {
316 return NULL;
317 }
Kevin Wolfce099542016-03-15 13:01:04 +0100318 blk = img_open_opts(filename, opts, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319 } else {
Kevin Wolfce099542016-03-15 13:01:04 +0100320 blk = img_open_file(filename, fmt, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000321 }
322 return blk;
323}
324
325
Chunyan Liu83d05212014-06-05 17:20:51 +0800326static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100327 const char *base_filename,
328 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200329{
Markus Armbruster6750e792015-02-12 17:43:08 +0100330 Error *err = NULL;
331
Kevin Wolfefa84d42009-05-18 16:42:12 +0200332 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100333 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100334 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100335 error_report("Backing file not supported for file format '%s'",
336 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100337 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900338 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200339 }
340 }
341 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100342 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100343 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100344 error_report("Backing file format not supported for file "
345 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100346 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900347 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200348 }
349 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900350 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200351}
352
bellardea2384d2004-08-01 21:59:26 +0000353static int img_create(int argc, char **argv)
354{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200355 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100356 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000357 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000358 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000359 const char *filename;
360 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200361 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200362 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100363 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000364
bellardea2384d2004-08-01 21:59:26 +0000365 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000366 static const struct option long_options[] = {
367 {"help", no_argument, 0, 'h'},
368 {"object", required_argument, 0, OPTION_OBJECT},
369 {0, 0, 0, 0}
370 };
371 c = getopt_long(argc, argv, "F:b:f:he6o:q",
372 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100373 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000374 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100375 }
bellardea2384d2004-08-01 21:59:26 +0000376 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100377 case '?':
bellardea2384d2004-08-01 21:59:26 +0000378 case 'h':
379 help();
380 break;
aliguori9230eaf2009-03-28 17:55:19 +0000381 case 'F':
382 base_fmt = optarg;
383 break;
bellardea2384d2004-08-01 21:59:26 +0000384 case 'b':
385 base_filename = optarg;
386 break;
387 case 'f':
388 fmt = optarg;
389 break;
390 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200391 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100392 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100393 goto fail;
thsd8871c52007-10-24 16:11:42 +0000394 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200395 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100396 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100397 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200398 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100399 if (!is_valid_option_list(optarg)) {
400 error_report("Invalid option list: %s", optarg);
401 goto fail;
402 }
403 if (!options) {
404 options = g_strdup(optarg);
405 } else {
406 char *old_options = options;
407 options = g_strdup_printf("%s,%s", options, optarg);
408 g_free(old_options);
409 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200410 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100411 case 'q':
412 quiet = true;
413 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000414 case OPTION_OBJECT: {
415 QemuOpts *opts;
416 opts = qemu_opts_parse_noisily(&qemu_object_opts,
417 optarg, true);
418 if (!opts) {
419 goto fail;
420 }
421 } break;
bellardea2384d2004-08-01 21:59:26 +0000422 }
423 }
aliguori9230eaf2009-03-28 17:55:19 +0000424
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900425 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100426 filename = (optind < argc) ? argv[optind] : NULL;
427 if (options && has_help_option(options)) {
428 g_free(options);
429 return print_block_option_help(filename, fmt);
430 }
431
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100432 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800433 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100434 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100435 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900436
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000437 if (qemu_opts_foreach(&qemu_object_opts,
438 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200439 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000440 goto fail;
441 }
442
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100443 /* Get image size, if specified */
444 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100445 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100446 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200447 sval = qemu_strtosz_suffix(argv[optind++], &end,
448 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100449 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800450 if (sval == -ERANGE) {
451 error_report("Image size must be less than 8 EiB!");
452 } else {
453 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200454 "G, T, P or E suffixes for ");
455 error_report("kilobytes, megabytes, gigabytes, terabytes, "
456 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800457 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100458 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100459 }
460 img_size = (uint64_t)sval;
461 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200462 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800463 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200464 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100465
Luiz Capitulino9b375252012-11-30 10:52:05 -0200466 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Kevin Wolf61de4c62016-03-18 17:46:45 +0100467 options, img_size, 0, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100468 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100469 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100470 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900471 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200472
Kevin Wolf77386bf2014-02-21 16:24:04 +0100473 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000474 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100475
476fail:
477 g_free(options);
478 return 1;
bellardea2384d2004-08-01 21:59:26 +0000479}
480
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100481static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500482{
Markus Armbruster4399c432014-04-25 16:50:32 +0200483 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500484 QString *str;
485 QmpOutputVisitor *ov = qmp_output_visitor_new();
486 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -0700487 visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
488 &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500489 obj = qmp_output_get_qobject(ov);
490 str = qobject_to_json_pretty(obj);
491 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100492 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500493 qobject_decref(obj);
494 qmp_output_visitor_cleanup(ov);
495 QDECREF(str);
496}
497
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100498static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500499{
500 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100501 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500502 } else {
503 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100504 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
505 "Data may be corrupted, or further writes to the image "
506 "may corrupt it.\n",
507 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500508 }
509
510 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100511 qprintf(quiet,
512 "\n%" PRId64 " leaked clusters were found on the image.\n"
513 "This means waste of disk space, but no harm to data.\n",
514 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500515 }
516
517 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100518 qprintf(quiet,
519 "\n%" PRId64
520 " internal errors have occurred during the check.\n",
521 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500522 }
523 }
524
525 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100526 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
527 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
528 check->allocated_clusters, check->total_clusters,
529 check->allocated_clusters * 100.0 / check->total_clusters,
530 check->fragmented_clusters * 100.0 / check->allocated_clusters,
531 check->compressed_clusters * 100.0 /
532 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500533 }
534
535 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100536 qprintf(quiet,
537 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500538 }
539}
540
541static int collect_image_check(BlockDriverState *bs,
542 ImageCheck *check,
543 const char *filename,
544 const char *fmt,
545 int fix)
546{
547 int ret;
548 BdrvCheckResult result;
549
550 ret = bdrv_check(bs, &result, fix);
551 if (ret < 0) {
552 return ret;
553 }
554
555 check->filename = g_strdup(filename);
556 check->format = g_strdup(bdrv_get_format_name(bs));
557 check->check_errors = result.check_errors;
558 check->corruptions = result.corruptions;
559 check->has_corruptions = result.corruptions != 0;
560 check->leaks = result.leaks;
561 check->has_leaks = result.leaks != 0;
562 check->corruptions_fixed = result.corruptions_fixed;
563 check->has_corruptions_fixed = result.corruptions != 0;
564 check->leaks_fixed = result.leaks_fixed;
565 check->has_leaks_fixed = result.leaks != 0;
566 check->image_end_offset = result.image_end_offset;
567 check->has_image_end_offset = result.image_end_offset != 0;
568 check->total_clusters = result.bfi.total_clusters;
569 check->has_total_clusters = result.bfi.total_clusters != 0;
570 check->allocated_clusters = result.bfi.allocated_clusters;
571 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
572 check->fragmented_clusters = result.bfi.fragmented_clusters;
573 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100574 check->compressed_clusters = result.bfi.compressed_clusters;
575 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500576
577 return 0;
578}
579
Kevin Wolfe076f332010-06-29 11:43:13 +0200580/*
581 * Checks an image for consistency. Exit codes:
582 *
Max Reitzd6635c42014-06-02 22:15:21 +0200583 * 0 - Check completed, image is good
584 * 1 - Check not completed because of internal errors
585 * 2 - Check completed, image is corrupted
586 * 3 - Check completed, image has leaked clusters, but is good otherwise
587 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200588 */
aliguori15859692009-04-21 23:11:53 +0000589static int img_check(int argc, char **argv)
590{
591 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200593 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200594 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000595 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200596 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100597 int flags = BDRV_O_CHECK;
598 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200599 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100600 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000601 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000602
603 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500604 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200605 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100606
aliguori15859692009-04-21 23:11:53 +0000607 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500608 int option_index = 0;
609 static const struct option long_options[] = {
610 {"help", no_argument, 0, 'h'},
611 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530612 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500613 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000614 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000615 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500616 {0, 0, 0, 0}
617 };
Max Reitz40055952014-07-22 22:58:42 +0200618 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500619 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100620 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000621 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100622 }
aliguori15859692009-04-21 23:11:53 +0000623 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100624 case '?':
aliguori15859692009-04-21 23:11:53 +0000625 case 'h':
626 help();
627 break;
628 case 'f':
629 fmt = optarg;
630 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200631 case 'r':
632 flags |= BDRV_O_RDWR;
633
634 if (!strcmp(optarg, "leaks")) {
635 fix = BDRV_FIX_LEAKS;
636 } else if (!strcmp(optarg, "all")) {
637 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
638 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800639 error_exit("Unknown option value for -r "
640 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200641 }
642 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500643 case OPTION_OUTPUT:
644 output = optarg;
645 break;
Max Reitz40055952014-07-22 22:58:42 +0200646 case 'T':
647 cache = optarg;
648 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100649 case 'q':
650 quiet = true;
651 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000652 case OPTION_OBJECT: {
653 QemuOpts *opts;
654 opts = qemu_opts_parse_noisily(&qemu_object_opts,
655 optarg, true);
656 if (!opts) {
657 return 1;
658 }
659 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000660 case OPTION_IMAGE_OPTS:
661 image_opts = true;
662 break;
aliguori15859692009-04-21 23:11:53 +0000663 }
664 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200665 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800666 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100667 }
aliguori15859692009-04-21 23:11:53 +0000668 filename = argv[optind++];
669
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500670 if (output && !strcmp(output, "json")) {
671 output_format = OFORMAT_JSON;
672 } else if (output && !strcmp(output, "human")) {
673 output_format = OFORMAT_HUMAN;
674 } else if (output) {
675 error_report("--output must be used with human or json as argument.");
676 return 1;
677 }
678
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000679 if (qemu_opts_foreach(&qemu_object_opts,
680 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200681 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000682 return 1;
683 }
684
Kevin Wolfce099542016-03-15 13:01:04 +0100685 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200686 if (ret < 0) {
687 error_report("Invalid source cache option: %s", cache);
688 return 1;
689 }
690
Kevin Wolfce099542016-03-15 13:01:04 +0100691 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200692 if (!blk) {
693 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900694 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200695 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500696
697 check = g_new0(ImageCheck, 1);
698 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200699
700 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200701 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200702 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500703 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200704 }
705
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500706 if (check->corruptions_fixed || check->leaks_fixed) {
707 int corruptions_fixed, leaks_fixed;
708
709 leaks_fixed = check->leaks_fixed;
710 corruptions_fixed = check->corruptions_fixed;
711
712 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100713 qprintf(quiet,
714 "The following inconsistencies were found and repaired:\n\n"
715 " %" PRId64 " leaked clusters\n"
716 " %" PRId64 " corruptions\n\n"
717 "Double checking the fixed image now...\n",
718 check->leaks_fixed,
719 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500720 }
721
722 ret = collect_image_check(bs, check, filename, fmt, 0);
723
724 check->leaks_fixed = leaks_fixed;
725 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200726 }
727
Max Reitz832390a2014-10-23 15:29:12 +0200728 if (!ret) {
729 switch (output_format) {
730 case OFORMAT_HUMAN:
731 dump_human_image_check(check, quiet);
732 break;
733 case OFORMAT_JSON:
734 dump_json_image_check(check, quiet);
735 break;
736 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500737 }
738
739 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200740 if (ret) {
741 error_report("Check failed: %s", strerror(-ret));
742 } else {
743 error_report("Check failed");
744 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500745 ret = 1;
746 goto fail;
747 }
748
749 if (check->corruptions) {
750 ret = 2;
751 } else if (check->leaks) {
752 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200753 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500754 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000755 }
756
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500757fail:
758 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200759 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500760 return ret;
aliguori15859692009-04-21 23:11:53 +0000761}
762
Max Reitzd4a32382014-10-24 15:57:37 +0200763typedef struct CommonBlockJobCBInfo {
764 BlockDriverState *bs;
765 Error **errp;
766} CommonBlockJobCBInfo;
767
768static void common_block_job_cb(void *opaque, int ret)
769{
770 CommonBlockJobCBInfo *cbi = opaque;
771
772 if (ret < 0) {
773 error_setg_errno(cbi->errp, -ret, "Block job failed");
774 }
Max Reitzd4a32382014-10-24 15:57:37 +0200775}
776
777static void run_block_job(BlockJob *job, Error **errp)
778{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200779 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200780
781 do {
782 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500783 qemu_progress_print(job->len ?
784 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200785 } while (!job->ready);
786
787 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200788
789 /* A block job may finish instantaneously without publishing any progress,
790 * so just signal completion here */
791 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200792}
793
bellardea2384d2004-08-01 21:59:26 +0000794static int img_commit(int argc, char **argv)
795{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400796 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200797 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200798 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200799 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200800 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100801 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200802 Error *local_err = NULL;
803 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000804 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000805
806 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400807 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200808 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000809 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000810 static const struct option long_options[] = {
811 {"help", no_argument, 0, 'h'},
812 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000813 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000814 {0, 0, 0, 0}
815 };
816 c = getopt_long(argc, argv, "f:ht:b:dpq",
817 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100818 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000819 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100820 }
bellardea2384d2004-08-01 21:59:26 +0000821 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100822 case '?':
bellardea2384d2004-08-01 21:59:26 +0000823 case 'h':
824 help();
825 break;
826 case 'f':
827 fmt = optarg;
828 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400829 case 't':
830 cache = optarg;
831 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200832 case 'b':
833 base = optarg;
834 /* -b implies -d */
835 drop = true;
836 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200837 case 'd':
838 drop = true;
839 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200840 case 'p':
841 progress = true;
842 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100843 case 'q':
844 quiet = true;
845 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000846 case OPTION_OBJECT: {
847 QemuOpts *opts;
848 opts = qemu_opts_parse_noisily(&qemu_object_opts,
849 optarg, true);
850 if (!opts) {
851 return 1;
852 }
853 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000854 case OPTION_IMAGE_OPTS:
855 image_opts = true;
856 break;
bellardea2384d2004-08-01 21:59:26 +0000857 }
858 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200859
860 /* Progress is not shown in Quiet mode */
861 if (quiet) {
862 progress = false;
863 }
864
Kevin Wolffc11eb22013-08-05 10:53:04 +0200865 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800866 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100867 }
bellardea2384d2004-08-01 21:59:26 +0000868 filename = argv[optind++];
869
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000870 if (qemu_opts_foreach(&qemu_object_opts,
871 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200872 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000873 return 1;
874 }
875
Max Reitz9a86fe42014-10-24 15:57:38 +0200876 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100877 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400878 if (ret < 0) {
879 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100880 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400881 }
882
Kevin Wolfce099542016-03-15 13:01:04 +0100883 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200884 if (!blk) {
885 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900886 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200887 bs = blk_bs(blk);
888
Max Reitz687fa1d2014-10-24 15:57:39 +0200889 qemu_progress_init(progress, 1.f);
890 qemu_progress_print(0.f, 100);
891
Max Reitz1b22bff2014-10-24 15:57:40 +0200892 if (base) {
893 base_bs = bdrv_find_backing_image(bs, base);
894 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100895 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200896 goto done;
897 }
898 } else {
899 /* This is different from QMP, which by default uses the deepest file in
900 * the backing chain (i.e., the very base); however, the traditional
901 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200902 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200903 if (!base_bs) {
904 error_setg(&local_err, "Image does not have a backing file");
905 goto done;
906 }
bellardea2384d2004-08-01 21:59:26 +0000907 }
908
Max Reitzd4a32382014-10-24 15:57:37 +0200909 cbi = (CommonBlockJobCBInfo){
910 .errp = &local_err,
911 .bs = bs,
912 };
913
914 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
915 common_block_job_cb, &cbi, &local_err);
916 if (local_err) {
917 goto done;
918 }
919
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200920 /* When the block job completes, the BlockBackend reference will point to
921 * the old backing file. In order to avoid that the top image is already
922 * deleted, so we can still empty it afterwards, increment the reference
923 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200924 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200925 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200926 }
927
Max Reitzd4a32382014-10-24 15:57:37 +0200928 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200929 if (local_err) {
930 goto unref_backing;
931 }
932
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200933 if (!drop && bs->drv->bdrv_make_empty) {
934 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200935 if (ret) {
936 error_setg_errno(&local_err, -ret, "Could not empty %s",
937 filename);
938 goto unref_backing;
939 }
940 }
941
942unref_backing:
943 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200944 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200945 }
Max Reitzd4a32382014-10-24 15:57:37 +0200946
947done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200948 qemu_progress_end();
949
Markus Armbruster26f54e92014-10-07 13:59:04 +0200950 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200951
952 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100953 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900954 return 1;
955 }
Max Reitzd4a32382014-10-24 15:57:37 +0200956
957 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000958 return 0;
959}
960
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400961/*
thsf58c7b32008-06-05 21:53:49 +0000962 * Returns true iff the first sector pointed to by 'buf' contains at least
963 * a non-NUL byte.
964 *
965 * 'pnum' is set to the number of sectors (including and immediately following
966 * the first one) that are known to be in the same allocated/unallocated state.
967 */
bellardea2384d2004-08-01 21:59:26 +0000968static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
969{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000970 bool is_zero;
971 int i;
bellardea2384d2004-08-01 21:59:26 +0000972
973 if (n <= 0) {
974 *pnum = 0;
975 return 0;
976 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000977 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000978 for(i = 1; i < n; i++) {
979 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000980 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000981 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000982 }
bellardea2384d2004-08-01 21:59:26 +0000983 }
984 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000985 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000986}
987
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100988/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200989 * Like is_allocated_sectors, but if the buffer starts with a used sector,
990 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
991 * breaking up write requests for only small sparse areas.
992 */
993static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
994 int min)
995{
996 int ret;
997 int num_checked, num_used;
998
999 if (n < min) {
1000 min = n;
1001 }
1002
1003 ret = is_allocated_sectors(buf, n, pnum);
1004 if (!ret) {
1005 return ret;
1006 }
1007
1008 num_used = *pnum;
1009 buf += BDRV_SECTOR_SIZE * *pnum;
1010 n -= *pnum;
1011 num_checked = num_used;
1012
1013 while (n > 0) {
1014 ret = is_allocated_sectors(buf, n, pnum);
1015
1016 buf += BDRV_SECTOR_SIZE * *pnum;
1017 n -= *pnum;
1018 num_checked += *pnum;
1019 if (ret) {
1020 num_used = num_checked;
1021 } else if (*pnum >= min) {
1022 break;
1023 }
1024 }
1025
1026 *pnum = num_used;
1027 return 1;
1028}
1029
1030/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001031 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1032 * buffers matches, non-zero otherwise.
1033 *
1034 * pnum is set to the number of sectors (including and immediately following
1035 * the first one) that are known to have the same comparison result
1036 */
1037static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1038 int *pnum)
1039{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001040 bool res;
1041 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001042
1043 if (n <= 0) {
1044 *pnum = 0;
1045 return 0;
1046 }
1047
1048 res = !!memcmp(buf1, buf2, 512);
1049 for(i = 1; i < n; i++) {
1050 buf1 += 512;
1051 buf2 += 512;
1052
1053 if (!!memcmp(buf1, buf2, 512) != res) {
1054 break;
1055 }
1056 }
1057
1058 *pnum = i;
1059 return res;
1060}
1061
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001062#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001063
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001064static int64_t sectors_to_bytes(int64_t sectors)
1065{
1066 return sectors << BDRV_SECTOR_BITS;
1067}
1068
1069static int64_t sectors_to_process(int64_t total, int64_t from)
1070{
1071 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1072}
1073
1074/*
1075 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1076 *
1077 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1078 * data and negative value on error.
1079 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001080 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001081 * @param sect_num: Number of first sector to check
1082 * @param sect_count: Number of sectors to check
1083 * @param filename: Name of disk file we are checking (logging purpose)
1084 * @param buffer: Allocated buffer for storing read data
1085 * @param quiet: Flag for quiet mode
1086 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001087static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001088 int sect_count, const char *filename,
1089 uint8_t *buffer, bool quiet)
1090{
1091 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001092 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1093 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001094 if (ret < 0) {
1095 error_report("Error while reading offset %" PRId64 " of %s: %s",
1096 sectors_to_bytes(sect_num), filename, strerror(-ret));
1097 return ret;
1098 }
1099 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1100 if (ret || pnum != sect_count) {
1101 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1102 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1103 return 1;
1104 }
1105
1106 return 0;
1107}
1108
1109/*
1110 * Compares two images. Exit codes:
1111 *
1112 * 0 - Images are identical
1113 * 1 - Images differ
1114 * >1 - Error occurred
1115 */
1116static int img_compare(int argc, char **argv)
1117{
Max Reitz40055952014-07-22 22:58:42 +02001118 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001119 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001120 BlockDriverState *bs1, *bs2;
1121 int64_t total_sectors1, total_sectors2;
1122 uint8_t *buf1 = NULL, *buf2 = NULL;
1123 int pnum1, pnum2;
1124 int allocated1, allocated2;
1125 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1126 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001127 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001128 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001129 int64_t total_sectors;
1130 int64_t sector_num = 0;
1131 int64_t nb_sectors;
1132 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001133 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001134 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001135
Max Reitz40055952014-07-22 22:58:42 +02001136 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001137 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001138 static const struct option long_options[] = {
1139 {"help", no_argument, 0, 'h'},
1140 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001141 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001142 {0, 0, 0, 0}
1143 };
1144 c = getopt_long(argc, argv, "hf:F:T:pqs",
1145 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001146 if (c == -1) {
1147 break;
1148 }
1149 switch (c) {
1150 case '?':
1151 case 'h':
1152 help();
1153 break;
1154 case 'f':
1155 fmt1 = optarg;
1156 break;
1157 case 'F':
1158 fmt2 = optarg;
1159 break;
Max Reitz40055952014-07-22 22:58:42 +02001160 case 'T':
1161 cache = optarg;
1162 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001163 case 'p':
1164 progress = true;
1165 break;
1166 case 'q':
1167 quiet = true;
1168 break;
1169 case 's':
1170 strict = true;
1171 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001172 case OPTION_OBJECT: {
1173 QemuOpts *opts;
1174 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1175 optarg, true);
1176 if (!opts) {
1177 ret = 2;
1178 goto out4;
1179 }
1180 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001181 case OPTION_IMAGE_OPTS:
1182 image_opts = true;
1183 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001184 }
1185 }
1186
1187 /* Progress is not shown in Quiet mode */
1188 if (quiet) {
1189 progress = false;
1190 }
1191
1192
Kevin Wolffc11eb22013-08-05 10:53:04 +02001193 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001194 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001195 }
1196 filename1 = argv[optind++];
1197 filename2 = argv[optind++];
1198
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001199 if (qemu_opts_foreach(&qemu_object_opts,
1200 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001201 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001202 ret = 2;
1203 goto out4;
1204 }
1205
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001206 /* Initialize before goto out */
1207 qemu_progress_init(progress, 2.0);
1208
Kevin Wolfce099542016-03-15 13:01:04 +01001209 flags = 0;
1210 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001211 if (ret < 0) {
1212 error_report("Invalid source cache option: %s", cache);
1213 ret = 2;
1214 goto out3;
1215 }
1216
Kevin Wolfce099542016-03-15 13:01:04 +01001217 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001218 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001219 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001220 goto out3;
1221 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001222
Kevin Wolfce099542016-03-15 13:01:04 +01001223 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001224 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001225 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001226 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001227 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001228 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001229 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001230
Max Reitzf1d3cd72015-02-05 13:58:18 -05001231 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1232 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1233 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001234 if (total_sectors1 < 0) {
1235 error_report("Can't get size of %s: %s",
1236 filename1, strerror(-total_sectors1));
1237 ret = 4;
1238 goto out;
1239 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001240 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001241 if (total_sectors2 < 0) {
1242 error_report("Can't get size of %s: %s",
1243 filename2, strerror(-total_sectors2));
1244 ret = 4;
1245 goto out;
1246 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001247 total_sectors = MIN(total_sectors1, total_sectors2);
1248 progress_base = MAX(total_sectors1, total_sectors2);
1249
1250 qemu_progress_print(0, 100);
1251
1252 if (strict && total_sectors1 != total_sectors2) {
1253 ret = 1;
1254 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1255 goto out;
1256 }
1257
1258 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001259 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001260 BlockDriverState *file;
1261
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001262 nb_sectors = sectors_to_process(total_sectors, sector_num);
1263 if (nb_sectors <= 0) {
1264 break;
1265 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001266 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1267 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001268 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001269 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001270 ret = 3;
1271 error_report("Sector allocation test failed for %s", filename1);
1272 goto out;
1273 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001274 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275
Fam Zheng25ad8e62016-01-13 16:37:41 +08001276 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1277 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001278 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001279 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001280 ret = 3;
1281 error_report("Sector allocation test failed for %s", filename2);
1282 goto out;
1283 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001284 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1285 if (pnum1) {
1286 nb_sectors = MIN(nb_sectors, pnum1);
1287 }
1288 if (pnum2) {
1289 nb_sectors = MIN(nb_sectors, pnum2);
1290 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001291
Fam Zheng25ad8e62016-01-13 16:37:41 +08001292 if (strict) {
1293 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1294 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1295 ret = 1;
1296 qprintf(quiet, "Strict mode: Offset %" PRId64
1297 " block status mismatch!\n",
1298 sectors_to_bytes(sector_num));
1299 goto out;
1300 }
1301 }
1302 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1303 nb_sectors = MIN(pnum1, pnum2);
1304 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001305 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001306 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1307 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001308 if (ret < 0) {
1309 error_report("Error while reading offset %" PRId64 " of %s:"
1310 " %s", sectors_to_bytes(sector_num), filename1,
1311 strerror(-ret));
1312 ret = 4;
1313 goto out;
1314 }
Eric Blake91669202016-05-06 10:26:43 -06001315 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1316 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001317 if (ret < 0) {
1318 error_report("Error while reading offset %" PRId64
1319 " of %s: %s", sectors_to_bytes(sector_num),
1320 filename2, strerror(-ret));
1321 ret = 4;
1322 goto out;
1323 }
1324 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1325 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001326 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1327 sectors_to_bytes(
1328 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001329 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001330 goto out;
1331 }
1332 }
1333 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001334
1335 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001336 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001337 filename1, buf1, quiet);
1338 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001339 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001340 filename2, buf1, quiet);
1341 }
1342 if (ret) {
1343 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001344 error_report("Error while reading offset %" PRId64 ": %s",
1345 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001346 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001347 }
1348 goto out;
1349 }
1350 }
1351 sector_num += nb_sectors;
1352 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1353 }
1354
1355 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001356 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001357 int64_t total_sectors_over;
1358 const char *filename_over;
1359
1360 qprintf(quiet, "Warning: Image size mismatch!\n");
1361 if (total_sectors1 > total_sectors2) {
1362 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001363 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001364 filename_over = filename1;
1365 } else {
1366 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001367 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001368 filename_over = filename2;
1369 }
1370
1371 for (;;) {
1372 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1373 if (nb_sectors <= 0) {
1374 break;
1375 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001376 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001377 nb_sectors, &pnum);
1378 if (ret < 0) {
1379 ret = 3;
1380 error_report("Sector allocation test failed for %s",
1381 filename_over);
1382 goto out;
1383
1384 }
1385 nb_sectors = pnum;
1386 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001387 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001388 filename_over, buf1, quiet);
1389 if (ret) {
1390 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001391 error_report("Error while reading offset %" PRId64
1392 " of %s: %s", sectors_to_bytes(sector_num),
1393 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001394 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001395 }
1396 goto out;
1397 }
1398 }
1399 sector_num += nb_sectors;
1400 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1401 }
1402 }
1403
1404 qprintf(quiet, "Images are identical.\n");
1405 ret = 0;
1406
1407out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001408 qemu_vfree(buf1);
1409 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001410 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001411out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001412 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413out3:
1414 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001415out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001416 return ret;
1417}
1418
Kevin Wolf690c7302015-03-19 13:33:32 +01001419enum ImgConvertBlockStatus {
1420 BLK_DATA,
1421 BLK_ZERO,
1422 BLK_BACKING_FILE,
1423};
1424
1425typedef struct ImgConvertState {
1426 BlockBackend **src;
1427 int64_t *src_sectors;
1428 int src_cur, src_num;
1429 int64_t src_cur_offset;
1430 int64_t total_sectors;
1431 int64_t allocated_sectors;
1432 enum ImgConvertBlockStatus status;
1433 int64_t sector_next_status;
1434 BlockBackend *target;
1435 bool has_zero_init;
1436 bool compressed;
1437 bool target_has_backing;
1438 int min_sparse;
1439 size_t cluster_sectors;
1440 size_t buf_sectors;
1441} ImgConvertState;
1442
1443static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1444{
1445 assert(sector_num >= s->src_cur_offset);
1446 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1447 s->src_cur_offset += s->src_sectors[s->src_cur];
1448 s->src_cur++;
1449 assert(s->src_cur < s->src_num);
1450 }
1451}
1452
1453static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1454{
1455 int64_t ret;
1456 int n;
1457
1458 convert_select_part(s, sector_num);
1459
1460 assert(s->total_sectors > sector_num);
1461 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1462
1463 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001464 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001465 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1466 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001467 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001468 if (ret < 0) {
1469 return ret;
1470 }
1471
1472 if (ret & BDRV_BLOCK_ZERO) {
1473 s->status = BLK_ZERO;
1474 } else if (ret & BDRV_BLOCK_DATA) {
1475 s->status = BLK_DATA;
1476 } else if (!s->target_has_backing) {
1477 /* Without a target backing file we must copy over the contents of
1478 * the backing file as well. */
Ren Kimura263a6f42016-04-28 01:04:58 +09001479 /* Check block status of the backing file chain to avoid
Kevin Wolf690c7302015-03-19 13:33:32 +01001480 * needlessly reading zeroes and limiting the iteration to the
1481 * buffer size */
Ren Kimura263a6f42016-04-28 01:04:58 +09001482 ret = bdrv_get_block_status_above(blk_bs(s->src[s->src_cur]), NULL,
1483 sector_num - s->src_cur_offset,
1484 n, &n, &file);
1485 if (ret < 0) {
1486 return ret;
1487 }
1488
1489 if (ret & BDRV_BLOCK_ZERO) {
1490 s->status = BLK_ZERO;
1491 } else {
1492 s->status = BLK_DATA;
1493 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001494 } else {
1495 s->status = BLK_BACKING_FILE;
1496 }
1497
1498 s->sector_next_status = sector_num + n;
1499 }
1500
1501 n = MIN(n, s->sector_next_status - sector_num);
1502 if (s->status == BLK_DATA) {
1503 n = MIN(n, s->buf_sectors);
1504 }
1505
1506 /* We need to write complete clusters for compressed images, so if an
1507 * unallocated area is shorter than that, we must consider the whole
1508 * cluster allocated. */
1509 if (s->compressed) {
1510 if (n < s->cluster_sectors) {
1511 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1512 s->status = BLK_DATA;
1513 } else {
1514 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1515 }
1516 }
1517
1518 return n;
1519}
1520
1521static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1522 uint8_t *buf)
1523{
1524 int n;
1525 int ret;
1526
Kevin Wolf690c7302015-03-19 13:33:32 +01001527 assert(nb_sectors <= s->buf_sectors);
1528 while (nb_sectors > 0) {
1529 BlockBackend *blk;
1530 int64_t bs_sectors;
1531
1532 /* In the case of compression with multiple source files, we can get a
1533 * nb_sectors that spreads into the next part. So we must be able to
1534 * read across multiple BDSes for one convert_read() call. */
1535 convert_select_part(s, sector_num);
1536 blk = s->src[s->src_cur];
1537 bs_sectors = s->src_sectors[s->src_cur];
1538
1539 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
Eric Blake91669202016-05-06 10:26:43 -06001540 ret = blk_pread(blk,
1541 (sector_num - s->src_cur_offset) << BDRV_SECTOR_BITS,
1542 buf, n << BDRV_SECTOR_BITS);
Kevin Wolf690c7302015-03-19 13:33:32 +01001543 if (ret < 0) {
1544 return ret;
1545 }
1546
1547 sector_num += n;
1548 nb_sectors -= n;
1549 buf += n * BDRV_SECTOR_SIZE;
1550 }
1551
1552 return 0;
1553}
1554
1555static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1556 const uint8_t *buf)
1557{
1558 int ret;
1559
1560 while (nb_sectors > 0) {
1561 int n = nb_sectors;
1562
1563 switch (s->status) {
1564 case BLK_BACKING_FILE:
1565 /* If we have a backing file, leave clusters unallocated that are
1566 * unallocated in the source image, so that the backing file is
1567 * visible at the respective offset. */
1568 assert(s->target_has_backing);
1569 break;
1570
1571 case BLK_DATA:
1572 /* We must always write compressed clusters as a whole, so don't
1573 * try to find zeroed parts in the buffer. We can only save the
1574 * write if the buffer is completely zeroed and we're allowed to
1575 * keep the target sparse. */
1576 if (s->compressed) {
1577 if (s->has_zero_init && s->min_sparse &&
1578 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1579 {
1580 assert(!s->target_has_backing);
1581 break;
1582 }
1583
1584 ret = blk_write_compressed(s->target, sector_num, buf, n);
1585 if (ret < 0) {
1586 return ret;
1587 }
1588 break;
1589 }
1590
1591 /* If there is real non-zero data or we're told to keep the target
1592 * fully allocated (-S 0), we must write it. Otherwise we can treat
1593 * it as zero sectors. */
1594 if (!s->min_sparse ||
1595 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1596 {
Eric Blake91669202016-05-06 10:26:43 -06001597 ret = blk_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1598 buf, n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001599 if (ret < 0) {
1600 return ret;
1601 }
1602 break;
1603 }
1604 /* fall-through */
1605
1606 case BLK_ZERO:
1607 if (s->has_zero_init) {
1608 break;
1609 }
Eric Blaked004bd52016-05-24 16:25:20 -06001610 ret = blk_pwrite_zeroes(s->target, sector_num << BDRV_SECTOR_BITS,
1611 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001612 if (ret < 0) {
1613 return ret;
1614 }
1615 break;
1616 }
1617
1618 sector_num += n;
1619 nb_sectors -= n;
1620 buf += n * BDRV_SECTOR_SIZE;
1621 }
1622
1623 return 0;
1624}
1625
1626static int convert_do_copy(ImgConvertState *s)
1627{
1628 uint8_t *buf = NULL;
1629 int64_t sector_num, allocated_done;
1630 int ret;
1631 int n;
1632
1633 /* Check whether we have zero initialisation or can get it efficiently */
1634 s->has_zero_init = s->min_sparse && !s->target_has_backing
1635 ? bdrv_has_zero_init(blk_bs(s->target))
1636 : false;
1637
1638 if (!s->has_zero_init && !s->target_has_backing &&
1639 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1640 {
1641 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1642 if (ret == 0) {
1643 s->has_zero_init = true;
1644 }
1645 }
1646
1647 /* Allocate buffer for copied data. For compressed images, only one cluster
1648 * can be copied at a time. */
1649 if (s->compressed) {
1650 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1651 error_report("invalid cluster size");
1652 ret = -EINVAL;
1653 goto fail;
1654 }
1655 s->buf_sectors = s->cluster_sectors;
1656 }
1657 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1658
1659 /* Calculate allocated sectors for progress */
1660 s->allocated_sectors = 0;
1661 sector_num = 0;
1662 while (sector_num < s->total_sectors) {
1663 n = convert_iteration_sectors(s, sector_num);
1664 if (n < 0) {
1665 ret = n;
1666 goto fail;
1667 }
Max Reitzaad15de2016-03-24 23:33:57 +01001668 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1669 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001670 s->allocated_sectors += n;
1671 }
1672 sector_num += n;
1673 }
1674
1675 /* Do the copy */
1676 s->src_cur = 0;
1677 s->src_cur_offset = 0;
1678 s->sector_next_status = 0;
1679
1680 sector_num = 0;
1681 allocated_done = 0;
1682
1683 while (sector_num < s->total_sectors) {
1684 n = convert_iteration_sectors(s, sector_num);
1685 if (n < 0) {
1686 ret = n;
1687 goto fail;
1688 }
Max Reitzaad15de2016-03-24 23:33:57 +01001689 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1690 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001691 allocated_done += n;
1692 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1693 0);
1694 }
1695
Max Reitzaad15de2016-03-24 23:33:57 +01001696 if (s->status == BLK_DATA) {
1697 ret = convert_read(s, sector_num, n, buf);
1698 if (ret < 0) {
1699 error_report("error while reading sector %" PRId64
1700 ": %s", sector_num, strerror(-ret));
1701 goto fail;
1702 }
1703 } else if (!s->min_sparse && s->status == BLK_ZERO) {
1704 n = MIN(n, s->buf_sectors);
1705 memset(buf, 0, n * BDRV_SECTOR_SIZE);
1706 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001707 }
1708
1709 ret = convert_write(s, sector_num, n, buf);
1710 if (ret < 0) {
1711 error_report("error while writing sector %" PRId64
1712 ": %s", sector_num, strerror(-ret));
1713 goto fail;
1714 }
1715
1716 sector_num += n;
1717 }
1718
1719 if (s->compressed) {
1720 /* signal EOF to align */
1721 ret = blk_write_compressed(s->target, 0, NULL, 0);
1722 if (ret < 0) {
1723 goto fail;
1724 }
1725 }
1726
1727 ret = 0;
1728fail:
1729 qemu_vfree(buf);
1730 return ret;
1731}
1732
bellardea2384d2004-08-01 21:59:26 +00001733static int img_convert(int argc, char **argv)
1734{
Kevin Wolf690c7302015-03-19 13:33:32 +01001735 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001736 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001737 int progress = 0, flags, src_flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001738 bool writethrough, src_writethrough;
Max Reitz40055952014-07-22 22:58:42 +02001739 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001740 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001741 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001742 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001743 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001744 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001745 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001746 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001747 QemuOpts *opts = NULL;
1748 QemuOptsList *create_opts = NULL;
1749 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001750 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001751 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001752 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001753 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001754 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001755 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001756 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001757 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001758
1759 fmt = NULL;
1760 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001761 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001762 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001763 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001764 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001765 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001766 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001767 static const struct option long_options[] = {
1768 {"help", no_argument, 0, 'h'},
1769 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001770 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001771 {0, 0, 0, 0}
1772 };
1773 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1774 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001775 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001776 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001777 }
bellardea2384d2004-08-01 21:59:26 +00001778 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001779 case '?':
bellardea2384d2004-08-01 21:59:26 +00001780 case 'h':
1781 help();
1782 break;
1783 case 'f':
1784 fmt = optarg;
1785 break;
1786 case 'O':
1787 out_fmt = optarg;
1788 break;
thsf58c7b32008-06-05 21:53:49 +00001789 case 'B':
1790 out_baseimg = optarg;
1791 break;
bellardea2384d2004-08-01 21:59:26 +00001792 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001793 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001794 break;
1795 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001796 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001797 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001798 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001799 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001800 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001801 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001802 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001803 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001804 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001805 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001806 if (!is_valid_option_list(optarg)) {
1807 error_report("Invalid option list: %s", optarg);
1808 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001809 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001810 }
1811 if (!options) {
1812 options = g_strdup(optarg);
1813 } else {
1814 char *old_options = options;
1815 options = g_strdup_printf("%s,%s", options, optarg);
1816 g_free(old_options);
1817 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001818 break;
edison51ef6722010-09-21 19:58:41 -07001819 case 's':
1820 snapshot_name = optarg;
1821 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001822 case 'l':
1823 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001824 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1825 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001826 if (!sn_opts) {
1827 error_report("Failed in parsing snapshot param '%s'",
1828 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001829 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001830 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001831 }
1832 } else {
1833 snapshot_name = optarg;
1834 }
1835 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001836 case 'S':
1837 {
1838 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001839 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001840 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001841 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001842 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001843 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001844 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001845 }
1846
1847 min_sparse = sval / BDRV_SECTOR_SIZE;
1848 break;
1849 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001850 case 'p':
1851 progress = 1;
1852 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001853 case 't':
1854 cache = optarg;
1855 break;
Max Reitz40055952014-07-22 22:58:42 +02001856 case 'T':
1857 src_cache = optarg;
1858 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001859 case 'q':
1860 quiet = true;
1861 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001862 case 'n':
1863 skip_create = 1;
1864 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001865 case OPTION_OBJECT:
1866 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1867 optarg, true);
1868 if (!opts) {
1869 goto fail_getopt;
1870 }
1871 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001872 case OPTION_IMAGE_OPTS:
1873 image_opts = true;
1874 break;
bellardea2384d2004-08-01 21:59:26 +00001875 }
1876 }
ths3b46e622007-09-17 08:09:54 +00001877
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001878 if (qemu_opts_foreach(&qemu_object_opts,
1879 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001880 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001881 goto fail_getopt;
1882 }
1883
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001884 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001885 if (quiet) {
1886 progress = 0;
1887 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001888 qemu_progress_init(progress, 1.0);
1889
balrog926c2d22007-10-31 01:11:44 +00001890 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001891 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001892
Kevin Wolf2dc83282014-02-21 16:24:05 +01001893 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001894 ret = print_block_option_help(out_filename, out_fmt);
1895 goto out;
1896 }
1897
bellardea2384d2004-08-01 21:59:26 +00001898 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001899 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001900 }
bellardea2384d2004-08-01 21:59:26 +00001901
thsf58c7b32008-06-05 21:53:49 +00001902
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001903 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001904 error_report("-B makes no sense when concatenating multiple input "
1905 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001906 ret = -1;
1907 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001908 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001909
Kevin Wolfce099542016-03-15 13:01:04 +01001910 src_flags = 0;
1911 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001912 if (ret < 0) {
1913 error_report("Invalid source cache option: %s", src_cache);
1914 goto out;
1915 }
1916
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001917 qemu_progress_print(0, 100);
1918
Markus Armbruster26f54e92014-10-07 13:59:04 +02001919 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001920 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001921 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001922
1923 total_sectors = 0;
1924 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001925 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Kevin Wolfce099542016-03-15 13:01:04 +01001926 fmt, src_flags, src_writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001927 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001928 ret = -1;
1929 goto out;
1930 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001931 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001932 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001933 if (bs_sectors[bs_i] < 0) {
1934 error_report("Could not get size of %s: %s",
1935 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1936 ret = -1;
1937 goto out;
1938 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001939 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001940 }
bellardea2384d2004-08-01 21:59:26 +00001941
Wenchao Xiaef806542013-12-04 17:10:57 +08001942 if (sn_opts) {
1943 ret = bdrv_snapshot_load_tmp(bs[0],
1944 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1945 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1946 &local_err);
1947 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001948 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001949 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001950 ret = -1;
1951 goto out;
1952 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001953
1954 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001955 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001956 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001957 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001958 ret = -1;
1959 goto out;
edison51ef6722010-09-21 19:58:41 -07001960 }
1961
Kevin Wolfefa84d42009-05-18 16:42:12 +02001962 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001963 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001964 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001965 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001966 ret = -1;
1967 goto out;
1968 }
balrog926c2d22007-10-31 01:11:44 +00001969
Max Reitzb65a5e12015-02-05 13:58:12 -05001970 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001971 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001972 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001973 ret = -1;
1974 goto out;
1975 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001976
Max Reitz2e024cd2015-02-11 09:58:46 -05001977 if (!skip_create) {
1978 if (!drv->create_opts) {
1979 error_report("Format driver '%s' does not support image creation",
1980 drv->format_name);
1981 ret = -1;
1982 goto out;
1983 }
Max Reitzf75613c2014-12-02 18:32:46 +01001984
Max Reitz2e024cd2015-02-11 09:58:46 -05001985 if (!proto_drv->create_opts) {
1986 error_report("Protocol driver '%s' does not support image creation",
1987 proto_drv->format_name);
1988 ret = -1;
1989 goto out;
1990 }
Max Reitzf75613c2014-12-02 18:32:46 +01001991
Max Reitz2e024cd2015-02-11 09:58:46 -05001992 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1993 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001994
Max Reitz2e024cd2015-02-11 09:58:46 -05001995 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001996 if (options) {
1997 qemu_opts_do_parse(opts, options, NULL, &local_err);
1998 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01001999 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002000 ret = -1;
2001 goto out;
2002 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002003 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002004
Markus Armbruster39101f22015-02-12 16:46:36 +01002005 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
2006 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002007 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2008 if (ret < 0) {
2009 goto out;
2010 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002011 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002012
Kevin Wolfa18953f2010-10-14 15:46:04 +02002013 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002014 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002015 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002016 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002017 }
2018
Kevin Wolfefa84d42009-05-18 16:42:12 +02002019 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01002020 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002021 bool encryption =
2022 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2023 const char *preallocation =
2024 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002025
2026 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002027 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002028 ret = -1;
2029 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002030 }
2031
Chunyan Liu83d05212014-06-05 17:20:51 +08002032 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002033 error_report("Compression and encryption not supported at "
2034 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002035 ret = -1;
2036 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002037 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002038
Chunyan Liu83d05212014-06-05 17:20:51 +08002039 if (preallocation
2040 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002041 {
2042 error_report("Compression and preallocation not supported at "
2043 "the same time");
2044 ret = -1;
2045 goto out;
2046 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002047 }
2048
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002049 if (!skip_create) {
2050 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002051 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002052 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002053 error_reportf_err(local_err, "%s: error while converting %s: ",
2054 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002055 goto out;
bellardea2384d2004-08-01 21:59:26 +00002056 }
2057 }
ths3b46e622007-09-17 08:09:54 +00002058
Peter Lieven5a37b602013-10-24 12:07:06 +02002059 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002060 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002061 if (ret < 0) {
2062 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002063 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002064 }
2065
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002066 /* XXX we should allow --image-opts to trigger use of
2067 * img_open() here, but then we have trouble with
2068 * the bdrv_create() call which takes different params.
2069 * Not critical right now, so fix can wait...
2070 */
Kevin Wolfce099542016-03-15 13:01:04 +01002071 out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002072 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002073 ret = -1;
2074 goto out;
2075 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002076 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002077
Peter Lievenf2521c92013-11-27 11:07:06 +01002078 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
2079 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2080 * as maximum. */
2081 bufsectors = MIN(32768,
2082 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
2083 out_bs->bl.discard_alignment))
2084 );
2085
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002086 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002087 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002088 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002089 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002090 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002091 ret = -1;
2092 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002093 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002094 error_report("output file is smaller than input file");
2095 ret = -1;
2096 goto out;
2097 }
2098 }
2099
Peter Lieven24f833c2013-11-27 11:07:07 +01002100 cluster_sectors = 0;
2101 ret = bdrv_get_info(out_bs, &bdi);
2102 if (ret < 0) {
2103 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002104 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002105 goto out;
2106 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002107 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002108 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002109 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2110 }
2111
Kevin Wolf690c7302015-03-19 13:33:32 +01002112 state = (ImgConvertState) {
2113 .src = blk,
2114 .src_sectors = bs_sectors,
2115 .src_num = bs_n,
2116 .total_sectors = total_sectors,
2117 .target = out_blk,
2118 .compressed = compress,
2119 .target_has_backing = (bool) out_baseimg,
2120 .min_sparse = min_sparse,
2121 .cluster_sectors = cluster_sectors,
2122 .buf_sectors = bufsectors,
2123 };
2124 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002125
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002126out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002127 if (!ret) {
2128 qemu_progress_print(100, 0);
2129 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002130 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002131 qemu_opts_del(opts);
2132 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002133 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002134 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002135 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002136 if (blk) {
2137 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2138 blk_unref(blk[bs_i]);
2139 }
2140 g_free(blk);
2141 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002142 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002143fail_getopt:
2144 g_free(options);
2145
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002146 if (ret) {
2147 return 1;
2148 }
bellardea2384d2004-08-01 21:59:26 +00002149 return 0;
2150}
2151
bellard57d1a2b2004-08-03 21:15:11 +00002152
bellardfaea38e2006-08-05 21:31:00 +00002153static void dump_snapshots(BlockDriverState *bs)
2154{
2155 QEMUSnapshotInfo *sn_tab, *sn;
2156 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002157
2158 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2159 if (nb_sns <= 0)
2160 return;
2161 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002162 bdrv_snapshot_dump(fprintf, stdout, NULL);
2163 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002164 for(i = 0; i < nb_sns; i++) {
2165 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002166 bdrv_snapshot_dump(fprintf, stdout, sn);
2167 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002168 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002169 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002170}
2171
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002172static void dump_json_image_info_list(ImageInfoList *list)
2173{
Markus Armbruster4399c432014-04-25 16:50:32 +02002174 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002175 QString *str;
2176 QmpOutputVisitor *ov = qmp_output_visitor_new();
2177 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002178 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2179 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002180 obj = qmp_output_get_qobject(ov);
2181 str = qobject_to_json_pretty(obj);
2182 assert(str != NULL);
2183 printf("%s\n", qstring_get_str(str));
2184 qobject_decref(obj);
2185 qmp_output_visitor_cleanup(ov);
2186 QDECREF(str);
2187}
2188
Benoît Canetc054b3f2012-09-05 13:09:02 +02002189static void dump_json_image_info(ImageInfo *info)
2190{
Markus Armbruster4399c432014-04-25 16:50:32 +02002191 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002192 QString *str;
2193 QmpOutputVisitor *ov = qmp_output_visitor_new();
2194 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002195 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002196 obj = qmp_output_get_qobject(ov);
2197 str = qobject_to_json_pretty(obj);
2198 assert(str != NULL);
2199 printf("%s\n", qstring_get_str(str));
2200 qobject_decref(obj);
2201 qmp_output_visitor_cleanup(ov);
2202 QDECREF(str);
2203}
2204
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002205static void dump_human_image_info_list(ImageInfoList *list)
2206{
2207 ImageInfoList *elem;
2208 bool delim = false;
2209
2210 for (elem = list; elem; elem = elem->next) {
2211 if (delim) {
2212 printf("\n");
2213 }
2214 delim = true;
2215
Wenchao Xia5b917042013-05-25 11:09:45 +08002216 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002217 }
2218}
2219
2220static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2221{
2222 return strcmp(a, b) == 0;
2223}
2224
2225/**
2226 * Open an image file chain and return an ImageInfoList
2227 *
2228 * @filename: topmost image filename
2229 * @fmt: topmost image format (may be NULL to autodetect)
2230 * @chain: true - enumerate entire backing file chain
2231 * false - only topmost image file
2232 *
2233 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2234 * image file. If there was an error a message will have been printed to
2235 * stderr.
2236 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002237static ImageInfoList *collect_image_info_list(bool image_opts,
2238 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002239 const char *fmt,
2240 bool chain)
2241{
2242 ImageInfoList *head = NULL;
2243 ImageInfoList **last = &head;
2244 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002245 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002246
2247 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2248
2249 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002250 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002251 BlockDriverState *bs;
2252 ImageInfo *info;
2253 ImageInfoList *elem;
2254
2255 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2256 error_report("Backing file '%s' creates an infinite loop.",
2257 filename);
2258 goto err;
2259 }
2260 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2261
Max Reitzefaa7c42016-03-16 19:54:38 +01002262 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002263 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002264 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002265 goto err;
2266 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002267 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002268
Wenchao Xia43526ec2013-06-06 12:27:58 +08002269 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002270 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002271 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002272 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002273 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002274 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002275
2276 elem = g_new0(ImageInfoList, 1);
2277 elem->value = info;
2278 *last = elem;
2279 last = &elem->next;
2280
Markus Armbruster26f54e92014-10-07 13:59:04 +02002281 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002282
2283 filename = fmt = NULL;
2284 if (chain) {
2285 if (info->has_full_backing_filename) {
2286 filename = info->full_backing_filename;
2287 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002288 error_report("Could not determine absolute backing filename,"
2289 " but backing filename '%s' present",
2290 info->backing_filename);
2291 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002292 }
2293 if (info->has_backing_filename_format) {
2294 fmt = info->backing_filename_format;
2295 }
2296 }
2297 }
2298 g_hash_table_destroy(filenames);
2299 return head;
2300
2301err:
2302 qapi_free_ImageInfoList(head);
2303 g_hash_table_destroy(filenames);
2304 return NULL;
2305}
2306
Benoît Canetc054b3f2012-09-05 13:09:02 +02002307static int img_info(int argc, char **argv)
2308{
2309 int c;
2310 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002311 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002312 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002313 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002314 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002315
bellardea2384d2004-08-01 21:59:26 +00002316 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002317 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002318 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002319 int option_index = 0;
2320 static const struct option long_options[] = {
2321 {"help", no_argument, 0, 'h'},
2322 {"format", required_argument, 0, 'f'},
2323 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002324 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002325 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002326 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002327 {0, 0, 0, 0}
2328 };
2329 c = getopt_long(argc, argv, "f:h",
2330 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002331 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002332 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002333 }
bellardea2384d2004-08-01 21:59:26 +00002334 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002335 case '?':
bellardea2384d2004-08-01 21:59:26 +00002336 case 'h':
2337 help();
2338 break;
2339 case 'f':
2340 fmt = optarg;
2341 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002342 case OPTION_OUTPUT:
2343 output = optarg;
2344 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002345 case OPTION_BACKING_CHAIN:
2346 chain = true;
2347 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002348 case OPTION_OBJECT: {
2349 QemuOpts *opts;
2350 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2351 optarg, true);
2352 if (!opts) {
2353 return 1;
2354 }
2355 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002356 case OPTION_IMAGE_OPTS:
2357 image_opts = true;
2358 break;
bellardea2384d2004-08-01 21:59:26 +00002359 }
2360 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002361 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002362 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002363 }
bellardea2384d2004-08-01 21:59:26 +00002364 filename = argv[optind++];
2365
Benoît Canetc054b3f2012-09-05 13:09:02 +02002366 if (output && !strcmp(output, "json")) {
2367 output_format = OFORMAT_JSON;
2368 } else if (output && !strcmp(output, "human")) {
2369 output_format = OFORMAT_HUMAN;
2370 } else if (output) {
2371 error_report("--output must be used with human or json as argument.");
2372 return 1;
2373 }
2374
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002375 if (qemu_opts_foreach(&qemu_object_opts,
2376 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002377 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002378 return 1;
2379 }
2380
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002381 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002382 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002383 return 1;
2384 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002385
Benoît Canetc054b3f2012-09-05 13:09:02 +02002386 switch (output_format) {
2387 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002388 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002389 break;
2390 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002391 if (chain) {
2392 dump_json_image_info_list(list);
2393 } else {
2394 dump_json_image_info(list->value);
2395 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002396 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002397 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002398
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002399 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002400 return 0;
2401}
2402
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002403static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2404 MapEntry *next)
2405{
2406 switch (output_format) {
2407 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002408 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002409 error_report("File contains external, encrypted or compressed clusters.");
2410 exit(1);
2411 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002412 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002413 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002414 e->start, e->length,
2415 e->has_offset ? e->offset : 0,
2416 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002417 }
2418 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2419 * Modify the flags here to allow more coalescing.
2420 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002421 if (next && (!next->data || next->zero)) {
2422 next->data = false;
2423 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002424 }
2425 break;
2426 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002427 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2428 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002429 (e->start == 0 ? "[" : ",\n"),
2430 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002431 e->zero ? "true" : "false",
2432 e->data ? "true" : "false");
2433 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002434 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002435 }
2436 putchar('}');
2437
2438 if (!next) {
2439 printf("]\n");
2440 }
2441 break;
2442 }
2443}
2444
2445static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2446 int nb_sectors, MapEntry *e)
2447{
2448 int64_t ret;
2449 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002450 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002451 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002452
2453 /* As an optimization, we could cache the current range of unallocated
2454 * clusters in each file of the chain, and avoid querying the same
2455 * range repeatedly.
2456 */
2457
2458 depth = 0;
2459 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002460 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2461 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002462 if (ret < 0) {
2463 return ret;
2464 }
2465 assert(nb_sectors);
2466 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2467 break;
2468 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002469 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002470 if (bs == NULL) {
2471 ret = 0;
2472 break;
2473 }
2474
2475 depth++;
2476 }
2477
John Snow28756452016-02-05 13:12:33 -05002478 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2479
2480 *e = (MapEntry) {
2481 .start = sector_num * BDRV_SECTOR_SIZE,
2482 .length = nb_sectors * BDRV_SECTOR_SIZE,
2483 .data = !!(ret & BDRV_BLOCK_DATA),
2484 .zero = !!(ret & BDRV_BLOCK_ZERO),
2485 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2486 .has_offset = has_offset,
2487 .depth = depth,
2488 .has_filename = file && has_offset,
2489 .filename = file && has_offset ? file->filename : NULL,
2490 };
2491
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002492 return 0;
2493}
2494
Fam Zheng16b0d552016-01-26 11:59:02 +08002495static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2496{
2497 if (curr->length == 0) {
2498 return false;
2499 }
2500 if (curr->zero != next->zero ||
2501 curr->data != next->data ||
2502 curr->depth != next->depth ||
2503 curr->has_filename != next->has_filename ||
2504 curr->has_offset != next->has_offset) {
2505 return false;
2506 }
2507 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2508 return false;
2509 }
2510 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2511 return false;
2512 }
2513 return true;
2514}
2515
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002516static int img_map(int argc, char **argv)
2517{
2518 int c;
2519 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002520 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002521 BlockDriverState *bs;
2522 const char *filename, *fmt, *output;
2523 int64_t length;
2524 MapEntry curr = { .length = 0 }, next;
2525 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002526 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002527
2528 fmt = NULL;
2529 output = NULL;
2530 for (;;) {
2531 int option_index = 0;
2532 static const struct option long_options[] = {
2533 {"help", no_argument, 0, 'h'},
2534 {"format", required_argument, 0, 'f'},
2535 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002536 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002537 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002538 {0, 0, 0, 0}
2539 };
2540 c = getopt_long(argc, argv, "f:h",
2541 long_options, &option_index);
2542 if (c == -1) {
2543 break;
2544 }
2545 switch (c) {
2546 case '?':
2547 case 'h':
2548 help();
2549 break;
2550 case 'f':
2551 fmt = optarg;
2552 break;
2553 case OPTION_OUTPUT:
2554 output = optarg;
2555 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002556 case OPTION_OBJECT: {
2557 QemuOpts *opts;
2558 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2559 optarg, true);
2560 if (!opts) {
2561 return 1;
2562 }
2563 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002564 case OPTION_IMAGE_OPTS:
2565 image_opts = true;
2566 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002567 }
2568 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002569 if (optind != argc - 1) {
2570 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002571 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002572 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002573
2574 if (output && !strcmp(output, "json")) {
2575 output_format = OFORMAT_JSON;
2576 } else if (output && !strcmp(output, "human")) {
2577 output_format = OFORMAT_HUMAN;
2578 } else if (output) {
2579 error_report("--output must be used with human or json as argument.");
2580 return 1;
2581 }
2582
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002583 if (qemu_opts_foreach(&qemu_object_opts,
2584 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002585 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002586 return 1;
2587 }
2588
Kevin Wolfce099542016-03-15 13:01:04 +01002589 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002590 if (!blk) {
2591 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002592 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002593 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002594
2595 if (output_format == OFORMAT_HUMAN) {
2596 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2597 }
2598
Max Reitzf1d3cd72015-02-05 13:58:18 -05002599 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002600 while (curr.start + curr.length < length) {
2601 int64_t nsectors_left;
2602 int64_t sector_num;
2603 int n;
2604
2605 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2606
2607 /* Probe up to 1 GiB at a time. */
2608 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2609 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2610 ret = get_block_status(bs, sector_num, n, &next);
2611
2612 if (ret < 0) {
2613 error_report("Could not read file metadata: %s", strerror(-ret));
2614 goto out;
2615 }
2616
Fam Zheng16b0d552016-01-26 11:59:02 +08002617 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002618 curr.length += next.length;
2619 continue;
2620 }
2621
2622 if (curr.length > 0) {
2623 dump_map_entry(output_format, &curr, &next);
2624 }
2625 curr = next;
2626 }
2627
2628 dump_map_entry(output_format, &curr, NULL);
2629
2630out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002631 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002632 return ret < 0;
2633}
2634
aliguorif7b4a942009-01-07 17:40:15 +00002635#define SNAPSHOT_LIST 1
2636#define SNAPSHOT_CREATE 2
2637#define SNAPSHOT_APPLY 3
2638#define SNAPSHOT_DELETE 4
2639
Stuart Brady153859b2009-06-07 00:42:17 +01002640static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002641{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002642 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002643 BlockDriverState *bs;
2644 QEMUSnapshotInfo sn;
2645 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002646 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002647 int action = 0;
2648 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002649 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002650 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002651 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002652
Kevin Wolfce099542016-03-15 13:01:04 +01002653 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002654 /* Parse commandline parameters */
2655 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002656 static const struct option long_options[] = {
2657 {"help", no_argument, 0, 'h'},
2658 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002659 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002660 {0, 0, 0, 0}
2661 };
2662 c = getopt_long(argc, argv, "la:c:d:hq",
2663 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002664 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002665 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002666 }
aliguorif7b4a942009-01-07 17:40:15 +00002667 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002668 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002669 case 'h':
2670 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002671 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002672 case 'l':
2673 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002674 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002675 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002676 }
2677 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002678 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002679 break;
2680 case 'a':
2681 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002682 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002683 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002684 }
2685 action = SNAPSHOT_APPLY;
2686 snapshot_name = optarg;
2687 break;
2688 case 'c':
2689 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002690 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002691 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002692 }
2693 action = SNAPSHOT_CREATE;
2694 snapshot_name = optarg;
2695 break;
2696 case 'd':
2697 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002698 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002699 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002700 }
2701 action = SNAPSHOT_DELETE;
2702 snapshot_name = optarg;
2703 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002704 case 'q':
2705 quiet = true;
2706 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002707 case OPTION_OBJECT: {
2708 QemuOpts *opts;
2709 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2710 optarg, true);
2711 if (!opts) {
2712 return 1;
2713 }
2714 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002715 case OPTION_IMAGE_OPTS:
2716 image_opts = true;
2717 break;
aliguorif7b4a942009-01-07 17:40:15 +00002718 }
2719 }
2720
Kevin Wolffc11eb22013-08-05 10:53:04 +02002721 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002722 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002723 }
aliguorif7b4a942009-01-07 17:40:15 +00002724 filename = argv[optind++];
2725
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002726 if (qemu_opts_foreach(&qemu_object_opts,
2727 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002728 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002729 return 1;
2730 }
2731
aliguorif7b4a942009-01-07 17:40:15 +00002732 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002733 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002734 if (!blk) {
2735 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002736 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002737 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002738
2739 /* Perform the requested action */
2740 switch(action) {
2741 case SNAPSHOT_LIST:
2742 dump_snapshots(bs);
2743 break;
2744
2745 case SNAPSHOT_CREATE:
2746 memset(&sn, 0, sizeof(sn));
2747 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2748
2749 qemu_gettimeofday(&tv);
2750 sn.date_sec = tv.tv_sec;
2751 sn.date_nsec = tv.tv_usec * 1000;
2752
2753 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002754 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002755 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002756 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002757 }
aliguorif7b4a942009-01-07 17:40:15 +00002758 break;
2759
2760 case SNAPSHOT_APPLY:
2761 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002762 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002763 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002764 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002765 }
aliguorif7b4a942009-01-07 17:40:15 +00002766 break;
2767
2768 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002769 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002770 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002771 error_reportf_err(err, "Could not delete snapshot '%s': ",
2772 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002773 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002774 }
aliguorif7b4a942009-01-07 17:40:15 +00002775 break;
2776 }
2777
2778 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002779 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002780 if (ret) {
2781 return 1;
2782 }
Stuart Brady153859b2009-06-07 00:42:17 +01002783 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002784}
2785
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002786static int img_rebase(int argc, char **argv)
2787{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002788 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002789 uint8_t *buf_old = NULL;
2790 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002791 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002792 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002793 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2794 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002795 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002796 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002797 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002798 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002799 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002800 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002801
2802 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002803 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002804 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002805 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002806 out_baseimg = NULL;
2807 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002808 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002809 static const struct option long_options[] = {
2810 {"help", no_argument, 0, 'h'},
2811 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002812 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002813 {0, 0, 0, 0}
2814 };
2815 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2816 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002817 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002818 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002819 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002820 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002821 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002822 case 'h':
2823 help();
2824 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002825 case 'f':
2826 fmt = optarg;
2827 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002828 case 'F':
2829 out_basefmt = optarg;
2830 break;
2831 case 'b':
2832 out_baseimg = optarg;
2833 break;
2834 case 'u':
2835 unsafe = 1;
2836 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002837 case 'p':
2838 progress = 1;
2839 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002840 case 't':
2841 cache = optarg;
2842 break;
Max Reitz40055952014-07-22 22:58:42 +02002843 case 'T':
2844 src_cache = optarg;
2845 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002846 case 'q':
2847 quiet = true;
2848 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002849 case OPTION_OBJECT: {
2850 QemuOpts *opts;
2851 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2852 optarg, true);
2853 if (!opts) {
2854 return 1;
2855 }
2856 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002857 case OPTION_IMAGE_OPTS:
2858 image_opts = true;
2859 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002860 }
2861 }
2862
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002863 if (quiet) {
2864 progress = 0;
2865 }
2866
Fam Zhengac1307a2014-04-22 13:36:11 +08002867 if (optind != argc - 1) {
2868 error_exit("Expecting one image file name");
2869 }
2870 if (!unsafe && !out_baseimg) {
2871 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002872 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002873 filename = argv[optind++];
2874
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002875 if (qemu_opts_foreach(&qemu_object_opts,
2876 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002877 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002878 return 1;
2879 }
2880
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002881 qemu_progress_init(progress, 2.0);
2882 qemu_progress_print(0, 100);
2883
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002884 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01002885 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002886 if (ret < 0) {
2887 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002888 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002889 }
2890
Kevin Wolfce099542016-03-15 13:01:04 +01002891 src_flags = 0;
2892 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02002893 if (ret < 0) {
2894 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002895 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002896 }
2897
Kevin Wolfce099542016-03-15 13:01:04 +01002898 /* The source files are opened read-only, don't care about WCE */
2899 assert((src_flags & BDRV_O_RDWR) == 0);
2900 (void) src_writethrough;
2901
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002902 /*
2903 * Open the images.
2904 *
2905 * Ignore the old backing file for unsafe rebase in case we want to correct
2906 * the reference to a renamed or moved backing file.
2907 */
Kevin Wolfce099542016-03-15 13:01:04 +01002908 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002909 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002910 ret = -1;
2911 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002912 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002913 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002914
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002915 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002916 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002917 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002918 ret = -1;
2919 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002920 }
2921 }
2922
2923 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002924 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002925 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002926 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002927
Max Reitz644483d2015-02-05 13:58:17 -05002928 if (bs->backing_format[0] != '\0') {
2929 options = qdict_new();
2930 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2931 }
2932
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002933 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002934 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002935 options, src_flags, &local_err);
2936 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002937 error_reportf_err(local_err,
2938 "Could not open old backing file '%s': ",
2939 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002940 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002941 }
Max Reitz644483d2015-02-05 13:58:17 -05002942
Alex Bligha6166732012-10-16 13:46:18 +01002943 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002944 if (out_basefmt) {
2945 options = qdict_new();
2946 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2947 } else {
2948 options = NULL;
2949 }
2950
Max Reitzefaa7c42016-03-16 19:54:38 +01002951 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002952 options, src_flags, &local_err);
2953 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002954 error_reportf_err(local_err,
2955 "Could not open new backing file '%s': ",
2956 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002957 goto out;
2958 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002959 }
2960 }
2961
2962 /*
2963 * Check each unallocated cluster in the COW file. If it is unallocated,
2964 * accesses go to the backing file. We must therefore compare this cluster
2965 * in the old and new backing file, and if they differ we need to copy it
2966 * from the old backing file into the COW file.
2967 *
2968 * If qemu-img crashes during this step, no harm is done. The content of
2969 * the image is the same as the original one at any time.
2970 */
2971 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002972 int64_t num_sectors;
2973 int64_t old_backing_num_sectors;
2974 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002975 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002976 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002977 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002978
Max Reitzf1d3cd72015-02-05 13:58:18 -05002979 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2980 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002981
Max Reitzf1d3cd72015-02-05 13:58:18 -05002982 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002983 if (num_sectors < 0) {
2984 error_report("Could not get size of '%s': %s",
2985 filename, strerror(-num_sectors));
2986 ret = -1;
2987 goto out;
2988 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002989 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002990 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002991 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002992
2993 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2994 error_report("Could not get size of '%s': %s",
2995 backing_name, strerror(-old_backing_num_sectors));
2996 ret = -1;
2997 goto out;
2998 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002999 if (blk_new_backing) {
3000 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003001 if (new_backing_num_sectors < 0) {
3002 error_report("Could not get size of '%s': %s",
3003 out_baseimg, strerror(-new_backing_num_sectors));
3004 ret = -1;
3005 goto out;
3006 }
Alex Bligha6166732012-10-16 13:46:18 +01003007 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003008
Kevin Wolf1f710492012-10-12 14:29:18 +02003009 if (num_sectors != 0) {
3010 local_progress = (float)100 /
3011 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3012 }
3013
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003014 for (sector = 0; sector < num_sectors; sector += n) {
3015
3016 /* How many sectors can we handle with the next read? */
3017 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3018 n = (IO_BUF_SIZE / 512);
3019 } else {
3020 n = num_sectors - sector;
3021 }
3022
3023 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003024 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003025 if (ret < 0) {
3026 error_report("error while reading image metadata: %s",
3027 strerror(-ret));
3028 goto out;
3029 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003030 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003031 continue;
3032 }
3033
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003034 /*
3035 * Read old and new backing file and take into consideration that
3036 * backing files may be smaller than the COW image.
3037 */
3038 if (sector >= old_backing_num_sectors) {
3039 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3040 } else {
3041 if (sector + n > old_backing_num_sectors) {
3042 n = old_backing_num_sectors - sector;
3043 }
3044
Eric Blake91669202016-05-06 10:26:43 -06003045 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3046 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003047 if (ret < 0) {
3048 error_report("error while reading from old backing file");
3049 goto out;
3050 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003051 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003052
Max Reitzf1d3cd72015-02-05 13:58:18 -05003053 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003054 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3055 } else {
3056 if (sector + n > new_backing_num_sectors) {
3057 n = new_backing_num_sectors - sector;
3058 }
3059
Eric Blake91669202016-05-06 10:26:43 -06003060 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3061 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003062 if (ret < 0) {
3063 error_report("error while reading from new backing file");
3064 goto out;
3065 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003066 }
3067
3068 /* If they differ, we need to write to the COW file */
3069 uint64_t written = 0;
3070
3071 while (written < n) {
3072 int pnum;
3073
3074 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003075 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003076 {
Eric Blake91669202016-05-06 10:26:43 -06003077 ret = blk_pwrite(blk,
3078 (sector + written) << BDRV_SECTOR_BITS,
3079 buf_old + written * 512,
3080 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003081 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003082 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003083 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003084 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003085 }
3086 }
3087
3088 written += pnum;
3089 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003090 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003091 }
3092 }
3093
3094 /*
3095 * Change the backing file. All clusters that are different from the old
3096 * backing file are overwritten in the COW file now, so the visible content
3097 * doesn't change when we switch the backing file.
3098 */
Alex Bligha6166732012-10-16 13:46:18 +01003099 if (out_baseimg && *out_baseimg) {
3100 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3101 } else {
3102 ret = bdrv_change_backing_file(bs, NULL, NULL);
3103 }
3104
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003105 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003106 error_report("Could not change the backing file to '%s': No "
3107 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003108 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003109 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003110 out_baseimg, strerror(-ret));
3111 }
3112
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003113 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003114 /*
3115 * TODO At this point it is possible to check if any clusters that are
3116 * allocated in the COW file are the same in the backing file. If so, they
3117 * could be dropped from the COW file. Don't do this before switching the
3118 * backing file, in case of a crash this would lead to corruption.
3119 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003120out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003121 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003122 /* Cleanup */
3123 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003124 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003125 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003126 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003127 qemu_vfree(buf_old);
3128 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003129
Markus Armbruster26f54e92014-10-07 13:59:04 +02003130 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003131 if (ret) {
3132 return 1;
3133 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003134 return 0;
3135}
3136
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003137static int img_resize(int argc, char **argv)
3138{
Markus Armbruster6750e792015-02-12 17:43:08 +01003139 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003140 int c, ret, relative;
3141 const char *filename, *fmt, *size;
3142 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003143 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003144 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003145 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003146
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003147 static QemuOptsList resize_options = {
3148 .name = "resize_options",
3149 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3150 .desc = {
3151 {
3152 .name = BLOCK_OPT_SIZE,
3153 .type = QEMU_OPT_SIZE,
3154 .help = "Virtual disk size"
3155 }, {
3156 /* end of list */
3157 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003158 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003159 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003160 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003161
Kevin Wolfe80fec72011-04-29 10:58:12 +02003162 /* Remove size from argv manually so that negative numbers are not treated
3163 * as options by getopt. */
3164 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003165 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003166 return 1;
3167 }
3168
3169 size = argv[--argc];
3170
3171 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003172 fmt = NULL;
3173 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003174 static const struct option long_options[] = {
3175 {"help", no_argument, 0, 'h'},
3176 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003177 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003178 {0, 0, 0, 0}
3179 };
3180 c = getopt_long(argc, argv, "f:hq",
3181 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003182 if (c == -1) {
3183 break;
3184 }
3185 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003186 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003187 case 'h':
3188 help();
3189 break;
3190 case 'f':
3191 fmt = optarg;
3192 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003193 case 'q':
3194 quiet = true;
3195 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003196 case OPTION_OBJECT: {
3197 QemuOpts *opts;
3198 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3199 optarg, true);
3200 if (!opts) {
3201 return 1;
3202 }
3203 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003204 case OPTION_IMAGE_OPTS:
3205 image_opts = true;
3206 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003207 }
3208 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003209 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003210 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003211 }
3212 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003213
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003214 if (qemu_opts_foreach(&qemu_object_opts,
3215 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003216 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003217 return 1;
3218 }
3219
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003220 /* Choose grow, shrink, or absolute resize mode */
3221 switch (size[0]) {
3222 case '+':
3223 relative = 1;
3224 size++;
3225 break;
3226 case '-':
3227 relative = -1;
3228 size++;
3229 break;
3230 default:
3231 relative = 0;
3232 break;
3233 }
3234
3235 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003236 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003237 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003238 if (err) {
3239 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003240 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003241 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003242 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003243 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003244 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3245 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003246
Max Reitzefaa7c42016-03-16 19:54:38 +01003247 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01003248 BDRV_O_RDWR, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003249 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003250 ret = -1;
3251 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003252 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003253
3254 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003255 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003256 } else {
3257 total_size = n;
3258 }
3259 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003260 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003261 ret = -1;
3262 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003263 }
3264
Max Reitzf1d3cd72015-02-05 13:58:18 -05003265 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003266 switch (ret) {
3267 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003268 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003269 break;
3270 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003271 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003272 break;
3273 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003274 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003275 break;
3276 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003277 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003278 break;
3279 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003280out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003281 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003282 if (ret) {
3283 return 1;
3284 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003285 return 0;
3286}
3287
Max Reitz76a3a342014-10-27 11:12:51 +01003288static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003289 int64_t offset, int64_t total_work_size,
3290 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003291{
3292 qemu_progress_print(100.f * offset / total_work_size, 0);
3293}
3294
Max Reitz6f176b42013-09-03 10:09:50 +02003295static int img_amend(int argc, char **argv)
3296{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003297 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003298 int c, ret = 0;
3299 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003300 QemuOptsList *create_opts = NULL;
3301 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003302 const char *fmt = NULL, *filename, *cache;
3303 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003304 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003305 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003306 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003307 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003308 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003309
Max Reitzbd39e6e2014-07-22 22:58:43 +02003310 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003311 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003312 static const struct option long_options[] = {
3313 {"help", no_argument, 0, 'h'},
3314 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003315 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003316 {0, 0, 0, 0}
3317 };
3318 c = getopt_long(argc, argv, "ho:f:t:pq",
3319 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003320 if (c == -1) {
3321 break;
3322 }
3323
3324 switch (c) {
3325 case 'h':
3326 case '?':
3327 help();
3328 break;
3329 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003330 if (!is_valid_option_list(optarg)) {
3331 error_report("Invalid option list: %s", optarg);
3332 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003333 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003334 }
3335 if (!options) {
3336 options = g_strdup(optarg);
3337 } else {
3338 char *old_options = options;
3339 options = g_strdup_printf("%s,%s", options, optarg);
3340 g_free(old_options);
3341 }
Max Reitz6f176b42013-09-03 10:09:50 +02003342 break;
3343 case 'f':
3344 fmt = optarg;
3345 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003346 case 't':
3347 cache = optarg;
3348 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003349 case 'p':
3350 progress = true;
3351 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003352 case 'q':
3353 quiet = true;
3354 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003355 case OPTION_OBJECT:
3356 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3357 optarg, true);
3358 if (!opts) {
3359 ret = -1;
3360 goto out_no_progress;
3361 }
3362 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003363 case OPTION_IMAGE_OPTS:
3364 image_opts = true;
3365 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003366 }
3367 }
3368
Max Reitz6f176b42013-09-03 10:09:50 +02003369 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003370 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003371 }
3372
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003373 if (qemu_opts_foreach(&qemu_object_opts,
3374 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003375 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003376 ret = -1;
3377 goto out_no_progress;
3378 }
3379
Max Reitz76a3a342014-10-27 11:12:51 +01003380 if (quiet) {
3381 progress = false;
3382 }
3383 qemu_progress_init(progress, 1.0);
3384
Kevin Wolfa283cb62014-02-21 16:24:07 +01003385 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3386 if (fmt && has_help_option(options)) {
3387 /* If a format is explicitly specified (and possibly no filename is
3388 * given), print option help here */
3389 ret = print_block_option_help(filename, fmt);
3390 goto out;
3391 }
3392
3393 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003394 error_report("Expecting one image file name");
3395 ret = -1;
3396 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003397 }
Max Reitz6f176b42013-09-03 10:09:50 +02003398
Kevin Wolfce099542016-03-15 13:01:04 +01003399 flags = BDRV_O_RDWR;
3400 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003401 if (ret < 0) {
3402 error_report("Invalid cache option: %s", cache);
3403 goto out;
3404 }
3405
Kevin Wolfce099542016-03-15 13:01:04 +01003406 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003407 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003408 ret = -1;
3409 goto out;
3410 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003411 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003412
3413 fmt = bs->drv->format_name;
3414
Kevin Wolf626f84f2014-02-21 16:24:06 +01003415 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003416 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003417 ret = print_block_option_help(filename, fmt);
3418 goto out;
3419 }
3420
Max Reitzb2439d22014-12-02 18:32:47 +01003421 if (!bs->drv->create_opts) {
3422 error_report("Format driver '%s' does not support any options to amend",
3423 fmt);
3424 ret = -1;
3425 goto out;
3426 }
3427
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003428 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003429 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003430 if (options) {
3431 qemu_opts_do_parse(opts, options, NULL, &err);
3432 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003433 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003434 ret = -1;
3435 goto out;
3436 }
Max Reitz6f176b42013-09-03 10:09:50 +02003437 }
3438
Max Reitz76a3a342014-10-27 11:12:51 +01003439 /* In case the driver does not call amend_status_cb() */
3440 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003441 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003442 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003443 if (ret < 0) {
3444 error_report("Error while amending options: %s", strerror(-ret));
3445 goto out;
3446 }
3447
3448out:
Max Reitz76a3a342014-10-27 11:12:51 +01003449 qemu_progress_end();
3450
Max Reitze814dff2015-08-20 16:00:38 -07003451out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003452 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003453 qemu_opts_del(opts);
3454 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003455 g_free(options);
3456
Max Reitz6f176b42013-09-03 10:09:50 +02003457 if (ret) {
3458 return 1;
3459 }
3460 return 0;
3461}
3462
Kevin Wolfb6133b82014-08-05 14:17:13 +02003463typedef struct BenchData {
3464 BlockBackend *blk;
3465 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003466 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003467 int bufsize;
3468 int nrreq;
3469 int n;
3470 uint8_t *buf;
3471 QEMUIOVector *qiov;
3472
3473 int in_flight;
3474 uint64_t offset;
3475} BenchData;
3476
3477static void bench_cb(void *opaque, int ret)
3478{
3479 BenchData *b = opaque;
3480 BlockAIOCB *acb;
3481
3482 if (ret < 0) {
3483 error_report("Failed request: %s\n", strerror(-ret));
3484 exit(EXIT_FAILURE);
3485 }
3486 if (b->in_flight > 0) {
3487 b->n--;
3488 b->in_flight--;
3489 }
3490
3491 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003492 if (b->write) {
3493 acb = blk_aio_pwritev(b->blk, b->offset, b->qiov, 0,
3494 bench_cb, b);
3495 } else {
3496 acb = blk_aio_preadv(b->blk, b->offset, b->qiov, 0,
3497 bench_cb, b);
3498 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003499 if (!acb) {
3500 error_report("Failed to issue request");
3501 exit(EXIT_FAILURE);
3502 }
3503 b->in_flight++;
3504 b->offset += b->bufsize;
3505 b->offset %= b->image_size;
3506 }
3507}
3508
3509static int img_bench(int argc, char **argv)
3510{
3511 int c, ret = 0;
3512 const char *fmt = NULL, *filename;
3513 bool quiet = false;
3514 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003515 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003516 int count = 75000;
3517 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003518 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003519 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003520 int pattern = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003521 int64_t image_size;
3522 BlockBackend *blk = NULL;
3523 BenchData data = {};
3524 int flags = 0;
3525 bool writethrough;
3526 struct timeval t1, t2;
3527 int i;
3528
3529 for (;;) {
3530 static const struct option long_options[] = {
3531 {"help", no_argument, 0, 'h'},
3532 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003533 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003534 {0, 0, 0, 0}
3535 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003536 c = getopt_long(argc, argv, "hc:d:f:no:qs:t:w", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003537 if (c == -1) {
3538 break;
3539 }
3540
3541 switch (c) {
3542 case 'h':
3543 case '?':
3544 help();
3545 break;
3546 case 'c':
3547 {
3548 char *end;
3549 errno = 0;
3550 count = strtoul(optarg, &end, 0);
3551 if (errno || *end || count > INT_MAX) {
3552 error_report("Invalid request count specified");
3553 return 1;
3554 }
3555 break;
3556 }
3557 case 'd':
3558 {
3559 char *end;
3560 errno = 0;
3561 depth = strtoul(optarg, &end, 0);
3562 if (errno || *end || depth > INT_MAX) {
3563 error_report("Invalid queue depth specified");
3564 return 1;
3565 }
3566 break;
3567 }
3568 case 'f':
3569 fmt = optarg;
3570 break;
3571 case 'n':
3572 flags |= BDRV_O_NATIVE_AIO;
3573 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003574 case 'o':
3575 {
3576 char *end;
3577 errno = 0;
3578 offset = qemu_strtosz_suffix(optarg, &end,
3579 QEMU_STRTOSZ_DEFSUFFIX_B);
3580 if (offset < 0|| *end) {
3581 error_report("Invalid offset specified");
3582 return 1;
3583 }
3584 break;
3585 }
3586 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003587 case 'q':
3588 quiet = true;
3589 break;
3590 case 's':
3591 {
3592 int64_t sval;
3593 char *end;
3594
3595 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
3596 if (sval < 0 || sval > INT_MAX || *end) {
3597 error_report("Invalid buffer size specified");
3598 return 1;
3599 }
3600
3601 bufsize = sval;
3602 break;
3603 }
3604 case 't':
3605 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3606 if (ret < 0) {
3607 error_report("Invalid cache mode");
3608 ret = -1;
3609 goto out;
3610 }
3611 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003612 case 'w':
3613 flags |= BDRV_O_RDWR;
3614 is_write = true;
3615 break;
3616 case OPTION_PATTERN:
3617 {
3618 char *end;
3619 errno = 0;
3620 pattern = strtoul(optarg, &end, 0);
3621 if (errno || *end || pattern > 0xff) {
3622 error_report("Invalid pattern byte specified");
3623 return 1;
3624 }
3625 break;
3626 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003627 case OPTION_IMAGE_OPTS:
3628 image_opts = true;
3629 break;
3630 }
3631 }
3632
3633 if (optind != argc - 1) {
3634 error_exit("Expecting one image file name");
3635 }
3636 filename = argv[argc - 1];
3637
3638 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
3639 if (!blk) {
3640 ret = -1;
3641 goto out;
3642 }
3643
3644 image_size = blk_getlength(blk);
3645 if (image_size < 0) {
3646 ret = image_size;
3647 goto out;
3648 }
3649
3650 data = (BenchData) {
3651 .blk = blk,
3652 .image_size = image_size,
3653 .bufsize = bufsize,
3654 .nrreq = depth,
3655 .n = count,
Kevin Wolfd3199a32015-07-10 18:09:18 +02003656 .offset = offset,
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003657 .write = is_write,
Kevin Wolfb6133b82014-08-05 14:17:13 +02003658 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003659 printf("Sending %d %s requests, %d bytes each, %d in parallel "
3660 "(starting at offset %" PRId64 ")\n",
3661 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
3662 data.offset);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003663
3664 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003665 memset(data.buf, pattern, data.nrreq * data.bufsize);
3666
Kevin Wolfb6133b82014-08-05 14:17:13 +02003667 data.qiov = g_new(QEMUIOVector, data.nrreq);
3668 for (i = 0; i < data.nrreq; i++) {
3669 qemu_iovec_init(&data.qiov[i], 1);
3670 qemu_iovec_add(&data.qiov[i],
3671 data.buf + i * data.bufsize, data.bufsize);
3672 }
3673
3674 gettimeofday(&t1, NULL);
3675 bench_cb(&data, 0);
3676
3677 while (data.n > 0) {
3678 main_loop_wait(false);
3679 }
3680 gettimeofday(&t2, NULL);
3681
3682 printf("Run completed in %3.3f seconds.\n",
3683 (t2.tv_sec - t1.tv_sec)
3684 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
3685
3686out:
3687 qemu_vfree(data.buf);
3688 blk_unref(blk);
3689
3690 if (ret) {
3691 return 1;
3692 }
3693 return 0;
3694}
3695
3696
Anthony Liguoric227f092009-10-01 16:12:16 -05003697static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003698#define DEF(option, callback, arg_string) \
3699 { option, callback },
3700#include "qemu-img-cmds.h"
3701#undef DEF
3702#undef GEN_DOCS
3703 { NULL, NULL, },
3704};
3705
bellardea2384d2004-08-01 21:59:26 +00003706int main(int argc, char **argv)
3707{
Anthony Liguoric227f092009-10-01 16:12:16 -05003708 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003709 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003710 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003711 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003712 static const struct option long_options[] = {
3713 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04003714 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04003715 {0, 0, 0, 0}
3716 };
bellardea2384d2004-08-01 21:59:26 +00003717
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003718#ifdef CONFIG_POSIX
3719 signal(SIGPIPE, SIG_IGN);
3720#endif
3721
Kevin Wolf53f76e52010-12-16 15:10:32 +01003722 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003723 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003724
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003725 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003726 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003727 exit(EXIT_FAILURE);
3728 }
3729
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03003730 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01003731
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003732 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003733 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003734 if (argc < 2) {
3735 error_exit("Not enough arguments");
3736 }
Stuart Brady153859b2009-06-07 00:42:17 +01003737 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01003738
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003739 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003740 qemu_add_opts(&qemu_source_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003741
Stuart Brady153859b2009-06-07 00:42:17 +01003742 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04003743 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01003744 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04003745 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01003746 }
bellardea2384d2004-08-01 21:59:26 +00003747 }
Stuart Brady153859b2009-06-07 00:42:17 +01003748
Jeff Cody5f6979c2014-04-28 14:37:18 -04003749 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04003750
3751 if (c == 'h') {
3752 help();
3753 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04003754 if (c == 'v') {
3755 printf(QEMU_IMG_VERSION);
3756 return 0;
3757 }
Jeff Cody7db16892014-04-25 17:02:32 -04003758
Stuart Brady153859b2009-06-07 00:42:17 +01003759 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003760 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003761}