blob: fc8070cef5e27d49834756d4cbb4a4c32154b723 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020025#include "qapi-visit.h"
26#include "qapi/qmp-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010028#include "qapi/qmp/qjson.h"
pbrookfaf07962007-11-11 02:51:17 +000029#include "qemu-common.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000030#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/option.h"
32#include "qemu/error-report.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000033#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020035#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010036#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020037#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080038#include "block/qapi.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020039#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000040
Don Slutz61979a62015-01-09 10:17:35 -050041#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Jeff Cody5f6979c2014-04-28 14:37:18 -040042 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
43
Anthony Liguoric227f092009-10-01 16:12:16 -050044typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010045 const char *name;
46 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050047} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010048
Federico Simoncelli8599ea42013-01-28 06:59:47 -050049enum {
50 OPTION_OUTPUT = 256,
51 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000052 OPTION_OBJECT = 258,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050053};
54
55typedef enum OutputFormat {
56 OFORMAT_JSON,
57 OFORMAT_HUMAN,
58} OutputFormat;
59
aurel32137519c2008-11-30 19:12:49 +000060/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010061#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040062#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000063
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010064static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000065{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010066 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000067}
68
Fam Zhengac1307a2014-04-22 13:36:11 +080069static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
70{
71 va_list ap;
72
73 error_printf("qemu-img: ");
74
75 va_start(ap, fmt);
76 error_vprintf(fmt, ap);
77 va_end(ap);
78
79 error_printf("\nTry 'qemu-img --help' for more information\n");
80 exit(EXIT_FAILURE);
81}
82
blueswir1d2c639d2009-01-24 18:19:25 +000083/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080084static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000085{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010086 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040087 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030088 "usage: qemu-img command [command options]\n"
89 "QEMU disk image utility\n"
90 "\n"
91 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010092#define DEF(option, callback, arg_string) \
93 " " arg_string "\n"
94#include "qemu-img-cmds.h"
95#undef DEF
96#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030097 "\n"
98 "Command parameters:\n"
99 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000100 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
101 " manual page for a description of the object properties. The most common\n"
102 " object type is a 'secret', which is used to supply passwords and/or\n"
103 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300104 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400105 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800106 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
107 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100108 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
109 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300110 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200111 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
112 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
113 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300114 " 'output_filename' is the destination disk image filename\n"
115 " 'output_fmt' is the destination format\n"
116 " 'options' is a comma separated list of format specific options in a\n"
117 " name=value format. Use -o ? for an overview of the options supported by the\n"
118 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800119 " 'snapshot_param' is param used for internal snapshot, format\n"
120 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
121 " '[ID_OR_NAME]'\n"
122 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
123 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300124 " '-c' indicates that target image must be compressed (qcow format only)\n"
125 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
126 " match exactly. The image doesn't need a working backing file before\n"
127 " rebasing in this case (useful for renaming the backing file)\n"
128 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200129 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100130 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200131 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
132 " contain only zeros for qemu-img to create a sparse image during\n"
133 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
134 " unallocated or zero sectors, and the destination image will always be\n"
135 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200136 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100137 " '-n' skips the target volume creation (useful if the volume is created\n"
138 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300139 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200140 "Parameters to check subcommand:\n"
141 " '-r' tries to repair any inconsistencies that are found during the check.\n"
142 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
143 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200144 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200145 "\n"
malc3f020d72010-02-08 12:04:56 +0300146 "Parameters to snapshot subcommand:\n"
147 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
148 " '-a' applies a snapshot (revert disk to saved state)\n"
149 " '-c' creates a snapshot\n"
150 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100151 " '-l' lists all snapshots in the given image\n"
152 "\n"
153 "Parameters to compare subcommand:\n"
154 " '-f' first image format\n"
155 " '-F' second image format\n"
156 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100157
158 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100159 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000160 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800161 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000162}
163
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000164static QemuOptsList qemu_object_opts = {
165 .name = "object",
166 .implied_opt_name = "qom-type",
167 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
168 .desc = {
169 { }
170 },
171};
172
Stefan Weil7c30f652013-06-16 17:01:05 +0200173static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100174{
175 int ret = 0;
176 if (!quiet) {
177 va_list args;
178 va_start(args, fmt);
179 ret = vprintf(fmt, args);
180 va_end(args);
181 }
182 return ret;
183}
184
bellardea2384d2004-08-01 21:59:26 +0000185
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100186static int print_block_option_help(const char *filename, const char *fmt)
187{
188 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800189 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500190 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100191
192 /* Find driver and parse its options */
193 drv = bdrv_find_format(fmt);
194 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100195 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100196 return 1;
197 }
198
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800199 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100200 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500201 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100202 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100203 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800204 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100205 return 1;
206 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800207 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100208 }
209
Chunyan Liu83d05212014-06-05 17:20:51 +0800210 qemu_opts_print_help(create_opts);
211 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100212 return 0;
213}
214
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200215static BlockBackend *img_open(const char *id, const char *filename,
216 const char *fmt, int flags,
217 bool require_io, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000218{
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200219 BlockBackend *blk;
bellard75c23802004-08-27 21:28:58 +0000220 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000221 char password[256];
Max Reitz34b5d2c2013-09-05 14:45:29 +0200222 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500223 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100224
bellard75c23802004-08-27 21:28:58 +0000225 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500226 options = qdict_new();
227 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000228 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100229
Max Reitz5bd31322015-02-05 13:58:16 -0500230 blk = blk_new_open(id, filename, NULL, options, flags, &local_err);
231 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100232 error_reportf_err(local_err, "Could not open '%s': ", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900233 goto fail;
bellard75c23802004-08-27 21:28:58 +0000234 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100235
Max Reitz5bd31322015-02-05 13:58:16 -0500236 bs = blk_bs(blk);
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100237 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100238 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
Daniel P. Berranged57e4e42015-05-12 17:09:19 +0100239 if (qemu_read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100240 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900241 goto fail;
242 }
243 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100244 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900245 goto fail;
246 }
bellard75c23802004-08-27 21:28:58 +0000247 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200248 return blk;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900249fail:
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200250 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900251 return NULL;
bellard75c23802004-08-27 21:28:58 +0000252}
253
Chunyan Liu83d05212014-06-05 17:20:51 +0800254static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100255 const char *base_filename,
256 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200257{
Markus Armbruster6750e792015-02-12 17:43:08 +0100258 Error *err = NULL;
259
Kevin Wolfefa84d42009-05-18 16:42:12 +0200260 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100261 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100262 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100263 error_report("Backing file not supported for file format '%s'",
264 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100265 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900266 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200267 }
268 }
269 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100270 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100271 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100272 error_report("Backing file format not supported for file "
273 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100274 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900275 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200276 }
277 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900278 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200279}
280
bellardea2384d2004-08-01 21:59:26 +0000281static int img_create(int argc, char **argv)
282{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200283 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100284 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000285 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000286 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000287 const char *filename;
288 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200289 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200290 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100291 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000292
bellardea2384d2004-08-01 21:59:26 +0000293 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000294 static const struct option long_options[] = {
295 {"help", no_argument, 0, 'h'},
296 {"object", required_argument, 0, OPTION_OBJECT},
297 {0, 0, 0, 0}
298 };
299 c = getopt_long(argc, argv, "F:b:f:he6o:q",
300 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100301 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000302 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100303 }
bellardea2384d2004-08-01 21:59:26 +0000304 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100305 case '?':
bellardea2384d2004-08-01 21:59:26 +0000306 case 'h':
307 help();
308 break;
aliguori9230eaf2009-03-28 17:55:19 +0000309 case 'F':
310 base_fmt = optarg;
311 break;
bellardea2384d2004-08-01 21:59:26 +0000312 case 'b':
313 base_filename = optarg;
314 break;
315 case 'f':
316 fmt = optarg;
317 break;
318 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200319 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100320 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100321 goto fail;
thsd8871c52007-10-24 16:11:42 +0000322 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200323 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100324 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100325 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200326 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100327 if (!is_valid_option_list(optarg)) {
328 error_report("Invalid option list: %s", optarg);
329 goto fail;
330 }
331 if (!options) {
332 options = g_strdup(optarg);
333 } else {
334 char *old_options = options;
335 options = g_strdup_printf("%s,%s", options, optarg);
336 g_free(old_options);
337 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200338 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100339 case 'q':
340 quiet = true;
341 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000342 case OPTION_OBJECT: {
343 QemuOpts *opts;
344 opts = qemu_opts_parse_noisily(&qemu_object_opts,
345 optarg, true);
346 if (!opts) {
347 goto fail;
348 }
349 } break;
bellardea2384d2004-08-01 21:59:26 +0000350 }
351 }
aliguori9230eaf2009-03-28 17:55:19 +0000352
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900353 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100354 filename = (optind < argc) ? argv[optind] : NULL;
355 if (options && has_help_option(options)) {
356 g_free(options);
357 return print_block_option_help(filename, fmt);
358 }
359
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100360 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800361 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100362 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100363 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900364
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000365 if (qemu_opts_foreach(&qemu_object_opts,
366 user_creatable_add_opts_foreach,
367 NULL, &local_err)) {
368 error_report_err(local_err);
369 goto fail;
370 }
371
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100372 /* Get image size, if specified */
373 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100374 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100375 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200376 sval = qemu_strtosz_suffix(argv[optind++], &end,
377 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100378 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800379 if (sval == -ERANGE) {
380 error_report("Image size must be less than 8 EiB!");
381 } else {
382 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200383 "G, T, P or E suffixes for ");
384 error_report("kilobytes, megabytes, gigabytes, terabytes, "
385 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800386 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100387 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100388 }
389 img_size = (uint64_t)sval;
390 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200391 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800392 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200393 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100394
Luiz Capitulino9b375252012-11-30 10:52:05 -0200395 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100396 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100397 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100398 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100399 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900400 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200401
Kevin Wolf77386bf2014-02-21 16:24:04 +0100402 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000403 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100404
405fail:
406 g_free(options);
407 return 1;
bellardea2384d2004-08-01 21:59:26 +0000408}
409
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100410static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500411{
Markus Armbruster4399c432014-04-25 16:50:32 +0200412 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500413 QString *str;
414 QmpOutputVisitor *ov = qmp_output_visitor_new();
415 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -0700416 visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
417 &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500418 obj = qmp_output_get_qobject(ov);
419 str = qobject_to_json_pretty(obj);
420 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100421 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500422 qobject_decref(obj);
423 qmp_output_visitor_cleanup(ov);
424 QDECREF(str);
425}
426
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100427static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500428{
429 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100430 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500431 } else {
432 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100433 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
434 "Data may be corrupted, or further writes to the image "
435 "may corrupt it.\n",
436 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500437 }
438
439 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100440 qprintf(quiet,
441 "\n%" PRId64 " leaked clusters were found on the image.\n"
442 "This means waste of disk space, but no harm to data.\n",
443 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500444 }
445
446 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100447 qprintf(quiet,
448 "\n%" PRId64
449 " internal errors have occurred during the check.\n",
450 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500451 }
452 }
453
454 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100455 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
456 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
457 check->allocated_clusters, check->total_clusters,
458 check->allocated_clusters * 100.0 / check->total_clusters,
459 check->fragmented_clusters * 100.0 / check->allocated_clusters,
460 check->compressed_clusters * 100.0 /
461 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500462 }
463
464 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100465 qprintf(quiet,
466 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500467 }
468}
469
470static int collect_image_check(BlockDriverState *bs,
471 ImageCheck *check,
472 const char *filename,
473 const char *fmt,
474 int fix)
475{
476 int ret;
477 BdrvCheckResult result;
478
479 ret = bdrv_check(bs, &result, fix);
480 if (ret < 0) {
481 return ret;
482 }
483
484 check->filename = g_strdup(filename);
485 check->format = g_strdup(bdrv_get_format_name(bs));
486 check->check_errors = result.check_errors;
487 check->corruptions = result.corruptions;
488 check->has_corruptions = result.corruptions != 0;
489 check->leaks = result.leaks;
490 check->has_leaks = result.leaks != 0;
491 check->corruptions_fixed = result.corruptions_fixed;
492 check->has_corruptions_fixed = result.corruptions != 0;
493 check->leaks_fixed = result.leaks_fixed;
494 check->has_leaks_fixed = result.leaks != 0;
495 check->image_end_offset = result.image_end_offset;
496 check->has_image_end_offset = result.image_end_offset != 0;
497 check->total_clusters = result.bfi.total_clusters;
498 check->has_total_clusters = result.bfi.total_clusters != 0;
499 check->allocated_clusters = result.bfi.allocated_clusters;
500 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
501 check->fragmented_clusters = result.bfi.fragmented_clusters;
502 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100503 check->compressed_clusters = result.bfi.compressed_clusters;
504 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500505
506 return 0;
507}
508
Kevin Wolfe076f332010-06-29 11:43:13 +0200509/*
510 * Checks an image for consistency. Exit codes:
511 *
Max Reitzd6635c42014-06-02 22:15:21 +0200512 * 0 - Check completed, image is good
513 * 1 - Check not completed because of internal errors
514 * 2 - Check completed, image is corrupted
515 * 3 - Check completed, image has leaked clusters, but is good otherwise
516 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200517 */
aliguori15859692009-04-21 23:11:53 +0000518static int img_check(int argc, char **argv)
519{
520 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500521 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200522 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200523 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000524 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200525 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100526 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200527 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100528 bool quiet = false;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000529 Error *local_err = NULL;
aliguori15859692009-04-21 23:11:53 +0000530
531 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500532 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200533 cache = BDRV_DEFAULT_CACHE;
aliguori15859692009-04-21 23:11:53 +0000534 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500535 int option_index = 0;
536 static const struct option long_options[] = {
537 {"help", no_argument, 0, 'h'},
538 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530539 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500540 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000541 {"object", required_argument, 0, OPTION_OBJECT},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500542 {0, 0, 0, 0}
543 };
Max Reitz40055952014-07-22 22:58:42 +0200544 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500545 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100546 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000547 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100548 }
aliguori15859692009-04-21 23:11:53 +0000549 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100550 case '?':
aliguori15859692009-04-21 23:11:53 +0000551 case 'h':
552 help();
553 break;
554 case 'f':
555 fmt = optarg;
556 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200557 case 'r':
558 flags |= BDRV_O_RDWR;
559
560 if (!strcmp(optarg, "leaks")) {
561 fix = BDRV_FIX_LEAKS;
562 } else if (!strcmp(optarg, "all")) {
563 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
564 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800565 error_exit("Unknown option value for -r "
566 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200567 }
568 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500569 case OPTION_OUTPUT:
570 output = optarg;
571 break;
Max Reitz40055952014-07-22 22:58:42 +0200572 case 'T':
573 cache = optarg;
574 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100575 case 'q':
576 quiet = true;
577 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000578 case OPTION_OBJECT: {
579 QemuOpts *opts;
580 opts = qemu_opts_parse_noisily(&qemu_object_opts,
581 optarg, true);
582 if (!opts) {
583 return 1;
584 }
585 } break;
aliguori15859692009-04-21 23:11:53 +0000586 }
587 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200588 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800589 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100590 }
aliguori15859692009-04-21 23:11:53 +0000591 filename = argv[optind++];
592
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500593 if (output && !strcmp(output, "json")) {
594 output_format = OFORMAT_JSON;
595 } else if (output && !strcmp(output, "human")) {
596 output_format = OFORMAT_HUMAN;
597 } else if (output) {
598 error_report("--output must be used with human or json as argument.");
599 return 1;
600 }
601
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000602 if (qemu_opts_foreach(&qemu_object_opts,
603 user_creatable_add_opts_foreach,
604 NULL, &local_err)) {
605 error_report_err(local_err);
606 return 1;
607 }
608
Max Reitz40055952014-07-22 22:58:42 +0200609 ret = bdrv_parse_cache_flags(cache, &flags);
610 if (ret < 0) {
611 error_report("Invalid source cache option: %s", cache);
612 return 1;
613 }
614
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200615 blk = img_open("image", filename, fmt, flags, true, quiet);
616 if (!blk) {
617 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900618 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200619 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500620
621 check = g_new0(ImageCheck, 1);
622 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200623
624 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200625 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200626 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500627 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200628 }
629
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500630 if (check->corruptions_fixed || check->leaks_fixed) {
631 int corruptions_fixed, leaks_fixed;
632
633 leaks_fixed = check->leaks_fixed;
634 corruptions_fixed = check->corruptions_fixed;
635
636 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100637 qprintf(quiet,
638 "The following inconsistencies were found and repaired:\n\n"
639 " %" PRId64 " leaked clusters\n"
640 " %" PRId64 " corruptions\n\n"
641 "Double checking the fixed image now...\n",
642 check->leaks_fixed,
643 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500644 }
645
646 ret = collect_image_check(bs, check, filename, fmt, 0);
647
648 check->leaks_fixed = leaks_fixed;
649 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200650 }
651
Max Reitz832390a2014-10-23 15:29:12 +0200652 if (!ret) {
653 switch (output_format) {
654 case OFORMAT_HUMAN:
655 dump_human_image_check(check, quiet);
656 break;
657 case OFORMAT_JSON:
658 dump_json_image_check(check, quiet);
659 break;
660 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500661 }
662
663 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200664 if (ret) {
665 error_report("Check failed: %s", strerror(-ret));
666 } else {
667 error_report("Check failed");
668 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500669 ret = 1;
670 goto fail;
671 }
672
673 if (check->corruptions) {
674 ret = 2;
675 } else if (check->leaks) {
676 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200677 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500678 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000679 }
680
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500681fail:
682 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200683 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500684 return ret;
aliguori15859692009-04-21 23:11:53 +0000685}
686
Max Reitzd4a32382014-10-24 15:57:37 +0200687typedef struct CommonBlockJobCBInfo {
688 BlockDriverState *bs;
689 Error **errp;
690} CommonBlockJobCBInfo;
691
692static void common_block_job_cb(void *opaque, int ret)
693{
694 CommonBlockJobCBInfo *cbi = opaque;
695
696 if (ret < 0) {
697 error_setg_errno(cbi->errp, -ret, "Block job failed");
698 }
Max Reitzd4a32382014-10-24 15:57:37 +0200699}
700
701static void run_block_job(BlockJob *job, Error **errp)
702{
703 AioContext *aio_context = bdrv_get_aio_context(job->bs);
704
705 do {
706 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500707 qemu_progress_print(job->len ?
708 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200709 } while (!job->ready);
710
711 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200712
713 /* A block job may finish instantaneously without publishing any progress,
714 * so just signal completion here */
715 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200716}
717
bellardea2384d2004-08-01 21:59:26 +0000718static int img_commit(int argc, char **argv)
719{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400720 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200721 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200722 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200723 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200724 bool progress = false, quiet = false, drop = false;
Max Reitzd4a32382014-10-24 15:57:37 +0200725 Error *local_err = NULL;
726 CommonBlockJobCBInfo cbi;
bellardea2384d2004-08-01 21:59:26 +0000727
728 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400729 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200730 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000731 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000732 static const struct option long_options[] = {
733 {"help", no_argument, 0, 'h'},
734 {"object", required_argument, 0, OPTION_OBJECT},
735 {0, 0, 0, 0}
736 };
737 c = getopt_long(argc, argv, "f:ht:b:dpq",
738 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100739 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000740 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100741 }
bellardea2384d2004-08-01 21:59:26 +0000742 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100743 case '?':
bellardea2384d2004-08-01 21:59:26 +0000744 case 'h':
745 help();
746 break;
747 case 'f':
748 fmt = optarg;
749 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400750 case 't':
751 cache = optarg;
752 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200753 case 'b':
754 base = optarg;
755 /* -b implies -d */
756 drop = true;
757 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200758 case 'd':
759 drop = true;
760 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200761 case 'p':
762 progress = true;
763 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100764 case 'q':
765 quiet = true;
766 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000767 case OPTION_OBJECT: {
768 QemuOpts *opts;
769 opts = qemu_opts_parse_noisily(&qemu_object_opts,
770 optarg, true);
771 if (!opts) {
772 return 1;
773 }
774 } break;
bellardea2384d2004-08-01 21:59:26 +0000775 }
776 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200777
778 /* Progress is not shown in Quiet mode */
779 if (quiet) {
780 progress = false;
781 }
782
Kevin Wolffc11eb22013-08-05 10:53:04 +0200783 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800784 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100785 }
bellardea2384d2004-08-01 21:59:26 +0000786 filename = argv[optind++];
787
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000788 if (qemu_opts_foreach(&qemu_object_opts,
789 user_creatable_add_opts_foreach,
790 NULL, &local_err)) {
791 error_report_err(local_err);
792 return 1;
793 }
794
Max Reitz9a86fe42014-10-24 15:57:38 +0200795 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100796 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400797 if (ret < 0) {
798 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100799 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400800 }
801
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200802 blk = img_open("image", filename, fmt, flags, true, quiet);
803 if (!blk) {
804 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900805 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200806 bs = blk_bs(blk);
807
Max Reitz687fa1d2014-10-24 15:57:39 +0200808 qemu_progress_init(progress, 1.f);
809 qemu_progress_print(0.f, 100);
810
Max Reitz1b22bff2014-10-24 15:57:40 +0200811 if (base) {
812 base_bs = bdrv_find_backing_image(bs, base);
813 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100814 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200815 goto done;
816 }
817 } else {
818 /* This is different from QMP, which by default uses the deepest file in
819 * the backing chain (i.e., the very base); however, the traditional
820 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200821 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200822 if (!base_bs) {
823 error_setg(&local_err, "Image does not have a backing file");
824 goto done;
825 }
bellardea2384d2004-08-01 21:59:26 +0000826 }
827
Max Reitzd4a32382014-10-24 15:57:37 +0200828 cbi = (CommonBlockJobCBInfo){
829 .errp = &local_err,
830 .bs = bs,
831 };
832
833 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
834 common_block_job_cb, &cbi, &local_err);
835 if (local_err) {
836 goto done;
837 }
838
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200839 /* When the block job completes, the BlockBackend reference will point to
840 * the old backing file. In order to avoid that the top image is already
841 * deleted, so we can still empty it afterwards, increment the reference
842 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200843 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200844 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200845 }
846
Max Reitzd4a32382014-10-24 15:57:37 +0200847 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200848 if (local_err) {
849 goto unref_backing;
850 }
851
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200852 if (!drop && bs->drv->bdrv_make_empty) {
853 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200854 if (ret) {
855 error_setg_errno(&local_err, -ret, "Could not empty %s",
856 filename);
857 goto unref_backing;
858 }
859 }
860
861unref_backing:
862 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200863 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200864 }
Max Reitzd4a32382014-10-24 15:57:37 +0200865
866done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200867 qemu_progress_end();
868
Markus Armbruster26f54e92014-10-07 13:59:04 +0200869 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200870
871 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100872 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900873 return 1;
874 }
Max Reitzd4a32382014-10-24 15:57:37 +0200875
876 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000877 return 0;
878}
879
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400880/*
thsf58c7b32008-06-05 21:53:49 +0000881 * Returns true iff the first sector pointed to by 'buf' contains at least
882 * a non-NUL byte.
883 *
884 * 'pnum' is set to the number of sectors (including and immediately following
885 * the first one) that are known to be in the same allocated/unallocated state.
886 */
bellardea2384d2004-08-01 21:59:26 +0000887static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
888{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000889 bool is_zero;
890 int i;
bellardea2384d2004-08-01 21:59:26 +0000891
892 if (n <= 0) {
893 *pnum = 0;
894 return 0;
895 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000896 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000897 for(i = 1; i < n; i++) {
898 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000899 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000900 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000901 }
bellardea2384d2004-08-01 21:59:26 +0000902 }
903 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000904 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000905}
906
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100907/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200908 * Like is_allocated_sectors, but if the buffer starts with a used sector,
909 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
910 * breaking up write requests for only small sparse areas.
911 */
912static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
913 int min)
914{
915 int ret;
916 int num_checked, num_used;
917
918 if (n < min) {
919 min = n;
920 }
921
922 ret = is_allocated_sectors(buf, n, pnum);
923 if (!ret) {
924 return ret;
925 }
926
927 num_used = *pnum;
928 buf += BDRV_SECTOR_SIZE * *pnum;
929 n -= *pnum;
930 num_checked = num_used;
931
932 while (n > 0) {
933 ret = is_allocated_sectors(buf, n, pnum);
934
935 buf += BDRV_SECTOR_SIZE * *pnum;
936 n -= *pnum;
937 num_checked += *pnum;
938 if (ret) {
939 num_used = num_checked;
940 } else if (*pnum >= min) {
941 break;
942 }
943 }
944
945 *pnum = num_used;
946 return 1;
947}
948
949/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100950 * Compares two buffers sector by sector. Returns 0 if the first sector of both
951 * buffers matches, non-zero otherwise.
952 *
953 * pnum is set to the number of sectors (including and immediately following
954 * the first one) that are known to have the same comparison result
955 */
956static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
957 int *pnum)
958{
Radim Krčmář8c1ac472015-02-20 17:06:15 +0100959 bool res;
960 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100961
962 if (n <= 0) {
963 *pnum = 0;
964 return 0;
965 }
966
967 res = !!memcmp(buf1, buf2, 512);
968 for(i = 1; i < n; i++) {
969 buf1 += 512;
970 buf2 += 512;
971
972 if (!!memcmp(buf1, buf2, 512) != res) {
973 break;
974 }
975 }
976
977 *pnum = i;
978 return res;
979}
980
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200981#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000982
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100983static int64_t sectors_to_bytes(int64_t sectors)
984{
985 return sectors << BDRV_SECTOR_BITS;
986}
987
988static int64_t sectors_to_process(int64_t total, int64_t from)
989{
990 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
991}
992
993/*
994 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
995 *
996 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
997 * data and negative value on error.
998 *
Max Reitzf1d3cd72015-02-05 13:58:18 -0500999 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001000 * @param sect_num: Number of first sector to check
1001 * @param sect_count: Number of sectors to check
1002 * @param filename: Name of disk file we are checking (logging purpose)
1003 * @param buffer: Allocated buffer for storing read data
1004 * @param quiet: Flag for quiet mode
1005 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001006static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001007 int sect_count, const char *filename,
1008 uint8_t *buffer, bool quiet)
1009{
1010 int pnum, ret = 0;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001011 ret = blk_read(blk, sect_num, buffer, sect_count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001012 if (ret < 0) {
1013 error_report("Error while reading offset %" PRId64 " of %s: %s",
1014 sectors_to_bytes(sect_num), filename, strerror(-ret));
1015 return ret;
1016 }
1017 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1018 if (ret || pnum != sect_count) {
1019 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1020 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1021 return 1;
1022 }
1023
1024 return 0;
1025}
1026
1027/*
1028 * Compares two images. Exit codes:
1029 *
1030 * 0 - Images are identical
1031 * 1 - Images differ
1032 * >1 - Error occurred
1033 */
1034static int img_compare(int argc, char **argv)
1035{
Max Reitz40055952014-07-22 22:58:42 +02001036 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001037 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001038 BlockDriverState *bs1, *bs2;
1039 int64_t total_sectors1, total_sectors2;
1040 uint8_t *buf1 = NULL, *buf2 = NULL;
1041 int pnum1, pnum2;
1042 int allocated1, allocated2;
1043 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1044 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001045 int flags;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001046 int64_t total_sectors;
1047 int64_t sector_num = 0;
1048 int64_t nb_sectors;
1049 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001050 uint64_t progress_base;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001051 Error *local_err = NULL;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001052
Max Reitz40055952014-07-22 22:58:42 +02001053 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001054 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001055 static const struct option long_options[] = {
1056 {"help", no_argument, 0, 'h'},
1057 {"object", required_argument, 0, OPTION_OBJECT},
1058 {0, 0, 0, 0}
1059 };
1060 c = getopt_long(argc, argv, "hf:F:T:pqs",
1061 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001062 if (c == -1) {
1063 break;
1064 }
1065 switch (c) {
1066 case '?':
1067 case 'h':
1068 help();
1069 break;
1070 case 'f':
1071 fmt1 = optarg;
1072 break;
1073 case 'F':
1074 fmt2 = optarg;
1075 break;
Max Reitz40055952014-07-22 22:58:42 +02001076 case 'T':
1077 cache = optarg;
1078 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001079 case 'p':
1080 progress = true;
1081 break;
1082 case 'q':
1083 quiet = true;
1084 break;
1085 case 's':
1086 strict = true;
1087 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001088 case OPTION_OBJECT: {
1089 QemuOpts *opts;
1090 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1091 optarg, true);
1092 if (!opts) {
1093 ret = 2;
1094 goto out4;
1095 }
1096 } break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001097 }
1098 }
1099
1100 /* Progress is not shown in Quiet mode */
1101 if (quiet) {
1102 progress = false;
1103 }
1104
1105
Kevin Wolffc11eb22013-08-05 10:53:04 +02001106 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001107 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001108 }
1109 filename1 = argv[optind++];
1110 filename2 = argv[optind++];
1111
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001112 if (qemu_opts_foreach(&qemu_object_opts,
1113 user_creatable_add_opts_foreach,
1114 NULL, &local_err)) {
1115 error_report_err(local_err);
1116 ret = 2;
1117 goto out4;
1118 }
1119
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001120 /* Initialize before goto out */
1121 qemu_progress_init(progress, 2.0);
1122
Max Reitz40055952014-07-22 22:58:42 +02001123 flags = BDRV_O_FLAGS;
1124 ret = bdrv_parse_cache_flags(cache, &flags);
1125 if (ret < 0) {
1126 error_report("Invalid source cache option: %s", cache);
1127 ret = 2;
1128 goto out3;
1129 }
1130
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001131 blk1 = img_open("image_1", filename1, fmt1, flags, true, quiet);
1132 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001133 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001134 goto out3;
1135 }
1136 bs1 = blk_bs(blk1);
1137
1138 blk2 = img_open("image_2", filename2, fmt2, flags, true, quiet);
1139 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001140 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001141 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001142 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001143 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001144
Max Reitzf1d3cd72015-02-05 13:58:18 -05001145 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1146 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1147 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001148 if (total_sectors1 < 0) {
1149 error_report("Can't get size of %s: %s",
1150 filename1, strerror(-total_sectors1));
1151 ret = 4;
1152 goto out;
1153 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001154 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001155 if (total_sectors2 < 0) {
1156 error_report("Can't get size of %s: %s",
1157 filename2, strerror(-total_sectors2));
1158 ret = 4;
1159 goto out;
1160 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001161 total_sectors = MIN(total_sectors1, total_sectors2);
1162 progress_base = MAX(total_sectors1, total_sectors2);
1163
1164 qemu_progress_print(0, 100);
1165
1166 if (strict && total_sectors1 != total_sectors2) {
1167 ret = 1;
1168 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1169 goto out;
1170 }
1171
1172 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001173 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001174 BlockDriverState *file;
1175
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001176 nb_sectors = sectors_to_process(total_sectors, sector_num);
1177 if (nb_sectors <= 0) {
1178 break;
1179 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001180 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1181 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001182 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001183 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001184 ret = 3;
1185 error_report("Sector allocation test failed for %s", filename1);
1186 goto out;
1187 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001188 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001189
Fam Zheng25ad8e62016-01-13 16:37:41 +08001190 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1191 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001192 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001193 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001194 ret = 3;
1195 error_report("Sector allocation test failed for %s", filename2);
1196 goto out;
1197 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001198 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1199 if (pnum1) {
1200 nb_sectors = MIN(nb_sectors, pnum1);
1201 }
1202 if (pnum2) {
1203 nb_sectors = MIN(nb_sectors, pnum2);
1204 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001205
Fam Zheng25ad8e62016-01-13 16:37:41 +08001206 if (strict) {
1207 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1208 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1209 ret = 1;
1210 qprintf(quiet, "Strict mode: Offset %" PRId64
1211 " block status mismatch!\n",
1212 sectors_to_bytes(sector_num));
1213 goto out;
1214 }
1215 }
1216 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1217 nb_sectors = MIN(pnum1, pnum2);
1218 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001219 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001220 ret = blk_read(blk1, sector_num, buf1, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 if (ret < 0) {
1222 error_report("Error while reading offset %" PRId64 " of %s:"
1223 " %s", sectors_to_bytes(sector_num), filename1,
1224 strerror(-ret));
1225 ret = 4;
1226 goto out;
1227 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001228 ret = blk_read(blk2, sector_num, buf2, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001229 if (ret < 0) {
1230 error_report("Error while reading offset %" PRId64
1231 " of %s: %s", sectors_to_bytes(sector_num),
1232 filename2, strerror(-ret));
1233 ret = 4;
1234 goto out;
1235 }
1236 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1237 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001238 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1239 sectors_to_bytes(
1240 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001241 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001242 goto out;
1243 }
1244 }
1245 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001246
1247 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001248 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001249 filename1, buf1, quiet);
1250 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001251 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001252 filename2, buf1, quiet);
1253 }
1254 if (ret) {
1255 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001256 error_report("Error while reading offset %" PRId64 ": %s",
1257 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001258 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001259 }
1260 goto out;
1261 }
1262 }
1263 sector_num += nb_sectors;
1264 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1265 }
1266
1267 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001268 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001269 int64_t total_sectors_over;
1270 const char *filename_over;
1271
1272 qprintf(quiet, "Warning: Image size mismatch!\n");
1273 if (total_sectors1 > total_sectors2) {
1274 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001275 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001276 filename_over = filename1;
1277 } else {
1278 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001279 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001280 filename_over = filename2;
1281 }
1282
1283 for (;;) {
1284 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1285 if (nb_sectors <= 0) {
1286 break;
1287 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001288 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001289 nb_sectors, &pnum);
1290 if (ret < 0) {
1291 ret = 3;
1292 error_report("Sector allocation test failed for %s",
1293 filename_over);
1294 goto out;
1295
1296 }
1297 nb_sectors = pnum;
1298 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001299 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001300 filename_over, buf1, quiet);
1301 if (ret) {
1302 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001303 error_report("Error while reading offset %" PRId64
1304 " of %s: %s", sectors_to_bytes(sector_num),
1305 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001306 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001307 }
1308 goto out;
1309 }
1310 }
1311 sector_num += nb_sectors;
1312 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1313 }
1314 }
1315
1316 qprintf(quiet, "Images are identical.\n");
1317 ret = 0;
1318
1319out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001320 qemu_vfree(buf1);
1321 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001322 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001323out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001324 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001325out3:
1326 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001327out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001328 return ret;
1329}
1330
Kevin Wolf690c7302015-03-19 13:33:32 +01001331enum ImgConvertBlockStatus {
1332 BLK_DATA,
1333 BLK_ZERO,
1334 BLK_BACKING_FILE,
1335};
1336
1337typedef struct ImgConvertState {
1338 BlockBackend **src;
1339 int64_t *src_sectors;
1340 int src_cur, src_num;
1341 int64_t src_cur_offset;
1342 int64_t total_sectors;
1343 int64_t allocated_sectors;
1344 enum ImgConvertBlockStatus status;
1345 int64_t sector_next_status;
1346 BlockBackend *target;
1347 bool has_zero_init;
1348 bool compressed;
1349 bool target_has_backing;
1350 int min_sparse;
1351 size_t cluster_sectors;
1352 size_t buf_sectors;
1353} ImgConvertState;
1354
1355static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1356{
1357 assert(sector_num >= s->src_cur_offset);
1358 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1359 s->src_cur_offset += s->src_sectors[s->src_cur];
1360 s->src_cur++;
1361 assert(s->src_cur < s->src_num);
1362 }
1363}
1364
1365static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1366{
1367 int64_t ret;
1368 int n;
1369
1370 convert_select_part(s, sector_num);
1371
1372 assert(s->total_sectors > sector_num);
1373 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1374
1375 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001376 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001377 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1378 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001379 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001380 if (ret < 0) {
1381 return ret;
1382 }
1383
1384 if (ret & BDRV_BLOCK_ZERO) {
1385 s->status = BLK_ZERO;
1386 } else if (ret & BDRV_BLOCK_DATA) {
1387 s->status = BLK_DATA;
1388 } else if (!s->target_has_backing) {
1389 /* Without a target backing file we must copy over the contents of
1390 * the backing file as well. */
1391 /* TODO Check block status of the backing file chain to avoid
1392 * needlessly reading zeroes and limiting the iteration to the
1393 * buffer size */
1394 s->status = BLK_DATA;
1395 } else {
1396 s->status = BLK_BACKING_FILE;
1397 }
1398
1399 s->sector_next_status = sector_num + n;
1400 }
1401
1402 n = MIN(n, s->sector_next_status - sector_num);
1403 if (s->status == BLK_DATA) {
1404 n = MIN(n, s->buf_sectors);
1405 }
1406
1407 /* We need to write complete clusters for compressed images, so if an
1408 * unallocated area is shorter than that, we must consider the whole
1409 * cluster allocated. */
1410 if (s->compressed) {
1411 if (n < s->cluster_sectors) {
1412 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1413 s->status = BLK_DATA;
1414 } else {
1415 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1416 }
1417 }
1418
1419 return n;
1420}
1421
1422static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1423 uint8_t *buf)
1424{
1425 int n;
1426 int ret;
1427
1428 if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) {
1429 return 0;
1430 }
1431
1432 assert(nb_sectors <= s->buf_sectors);
1433 while (nb_sectors > 0) {
1434 BlockBackend *blk;
1435 int64_t bs_sectors;
1436
1437 /* In the case of compression with multiple source files, we can get a
1438 * nb_sectors that spreads into the next part. So we must be able to
1439 * read across multiple BDSes for one convert_read() call. */
1440 convert_select_part(s, sector_num);
1441 blk = s->src[s->src_cur];
1442 bs_sectors = s->src_sectors[s->src_cur];
1443
1444 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
1445 ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n);
1446 if (ret < 0) {
1447 return ret;
1448 }
1449
1450 sector_num += n;
1451 nb_sectors -= n;
1452 buf += n * BDRV_SECTOR_SIZE;
1453 }
1454
1455 return 0;
1456}
1457
1458static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1459 const uint8_t *buf)
1460{
1461 int ret;
1462
1463 while (nb_sectors > 0) {
1464 int n = nb_sectors;
1465
1466 switch (s->status) {
1467 case BLK_BACKING_FILE:
1468 /* If we have a backing file, leave clusters unallocated that are
1469 * unallocated in the source image, so that the backing file is
1470 * visible at the respective offset. */
1471 assert(s->target_has_backing);
1472 break;
1473
1474 case BLK_DATA:
1475 /* We must always write compressed clusters as a whole, so don't
1476 * try to find zeroed parts in the buffer. We can only save the
1477 * write if the buffer is completely zeroed and we're allowed to
1478 * keep the target sparse. */
1479 if (s->compressed) {
1480 if (s->has_zero_init && s->min_sparse &&
1481 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1482 {
1483 assert(!s->target_has_backing);
1484 break;
1485 }
1486
1487 ret = blk_write_compressed(s->target, sector_num, buf, n);
1488 if (ret < 0) {
1489 return ret;
1490 }
1491 break;
1492 }
1493
1494 /* If there is real non-zero data or we're told to keep the target
1495 * fully allocated (-S 0), we must write it. Otherwise we can treat
1496 * it as zero sectors. */
1497 if (!s->min_sparse ||
1498 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1499 {
1500 ret = blk_write(s->target, sector_num, buf, n);
1501 if (ret < 0) {
1502 return ret;
1503 }
1504 break;
1505 }
1506 /* fall-through */
1507
1508 case BLK_ZERO:
1509 if (s->has_zero_init) {
1510 break;
1511 }
1512 ret = blk_write_zeroes(s->target, sector_num, n, 0);
1513 if (ret < 0) {
1514 return ret;
1515 }
1516 break;
1517 }
1518
1519 sector_num += n;
1520 nb_sectors -= n;
1521 buf += n * BDRV_SECTOR_SIZE;
1522 }
1523
1524 return 0;
1525}
1526
1527static int convert_do_copy(ImgConvertState *s)
1528{
1529 uint8_t *buf = NULL;
1530 int64_t sector_num, allocated_done;
1531 int ret;
1532 int n;
1533
1534 /* Check whether we have zero initialisation or can get it efficiently */
1535 s->has_zero_init = s->min_sparse && !s->target_has_backing
1536 ? bdrv_has_zero_init(blk_bs(s->target))
1537 : false;
1538
1539 if (!s->has_zero_init && !s->target_has_backing &&
1540 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1541 {
1542 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1543 if (ret == 0) {
1544 s->has_zero_init = true;
1545 }
1546 }
1547
1548 /* Allocate buffer for copied data. For compressed images, only one cluster
1549 * can be copied at a time. */
1550 if (s->compressed) {
1551 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1552 error_report("invalid cluster size");
1553 ret = -EINVAL;
1554 goto fail;
1555 }
1556 s->buf_sectors = s->cluster_sectors;
1557 }
1558 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1559
1560 /* Calculate allocated sectors for progress */
1561 s->allocated_sectors = 0;
1562 sector_num = 0;
1563 while (sector_num < s->total_sectors) {
1564 n = convert_iteration_sectors(s, sector_num);
1565 if (n < 0) {
1566 ret = n;
1567 goto fail;
1568 }
1569 if (s->status == BLK_DATA) {
1570 s->allocated_sectors += n;
1571 }
1572 sector_num += n;
1573 }
1574
1575 /* Do the copy */
1576 s->src_cur = 0;
1577 s->src_cur_offset = 0;
1578 s->sector_next_status = 0;
1579
1580 sector_num = 0;
1581 allocated_done = 0;
1582
1583 while (sector_num < s->total_sectors) {
1584 n = convert_iteration_sectors(s, sector_num);
1585 if (n < 0) {
1586 ret = n;
1587 goto fail;
1588 }
1589 if (s->status == BLK_DATA) {
1590 allocated_done += n;
1591 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1592 0);
1593 }
1594
1595 ret = convert_read(s, sector_num, n, buf);
1596 if (ret < 0) {
1597 error_report("error while reading sector %" PRId64
1598 ": %s", sector_num, strerror(-ret));
1599 goto fail;
1600 }
1601
1602 ret = convert_write(s, sector_num, n, buf);
1603 if (ret < 0) {
1604 error_report("error while writing sector %" PRId64
1605 ": %s", sector_num, strerror(-ret));
1606 goto fail;
1607 }
1608
1609 sector_num += n;
1610 }
1611
1612 if (s->compressed) {
1613 /* signal EOF to align */
1614 ret = blk_write_compressed(s->target, 0, NULL, 0);
1615 if (ret < 0) {
1616 goto fail;
1617 }
1618 }
1619
1620 ret = 0;
1621fail:
1622 qemu_vfree(buf);
1623 return ret;
1624}
1625
bellardea2384d2004-08-01 21:59:26 +00001626static int img_convert(int argc, char **argv)
1627{
Kevin Wolf690c7302015-03-19 13:33:32 +01001628 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001629 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001630 int progress = 0, flags, src_flags;
1631 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001632 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001633 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001634 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001635 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001636 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001637 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001638 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001639 QemuOpts *opts = NULL;
1640 QemuOptsList *create_opts = NULL;
1641 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001642 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001643 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001644 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001645 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001646 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001647 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001648 ImgConvertState state;
bellardea2384d2004-08-01 21:59:26 +00001649
1650 fmt = NULL;
1651 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001652 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001653 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001654 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001655 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001656 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001657 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001658 static const struct option long_options[] = {
1659 {"help", no_argument, 0, 'h'},
1660 {"object", required_argument, 0, OPTION_OBJECT},
1661 {0, 0, 0, 0}
1662 };
1663 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1664 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001665 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001666 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001667 }
bellardea2384d2004-08-01 21:59:26 +00001668 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001669 case '?':
bellardea2384d2004-08-01 21:59:26 +00001670 case 'h':
1671 help();
1672 break;
1673 case 'f':
1674 fmt = optarg;
1675 break;
1676 case 'O':
1677 out_fmt = optarg;
1678 break;
thsf58c7b32008-06-05 21:53:49 +00001679 case 'B':
1680 out_baseimg = optarg;
1681 break;
bellardea2384d2004-08-01 21:59:26 +00001682 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001683 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001684 break;
1685 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001686 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001687 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001688 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001689 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001690 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001691 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001692 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001693 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001694 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001695 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001696 if (!is_valid_option_list(optarg)) {
1697 error_report("Invalid option list: %s", optarg);
1698 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001699 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001700 }
1701 if (!options) {
1702 options = g_strdup(optarg);
1703 } else {
1704 char *old_options = options;
1705 options = g_strdup_printf("%s,%s", options, optarg);
1706 g_free(old_options);
1707 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001708 break;
edison51ef6722010-09-21 19:58:41 -07001709 case 's':
1710 snapshot_name = optarg;
1711 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001712 case 'l':
1713 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001714 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1715 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001716 if (!sn_opts) {
1717 error_report("Failed in parsing snapshot param '%s'",
1718 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001719 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001720 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001721 }
1722 } else {
1723 snapshot_name = optarg;
1724 }
1725 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001726 case 'S':
1727 {
1728 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001729 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001730 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001731 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001732 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001733 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001734 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001735 }
1736
1737 min_sparse = sval / BDRV_SECTOR_SIZE;
1738 break;
1739 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001740 case 'p':
1741 progress = 1;
1742 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001743 case 't':
1744 cache = optarg;
1745 break;
Max Reitz40055952014-07-22 22:58:42 +02001746 case 'T':
1747 src_cache = optarg;
1748 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001749 case 'q':
1750 quiet = true;
1751 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001752 case 'n':
1753 skip_create = 1;
1754 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001755 case OPTION_OBJECT:
1756 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1757 optarg, true);
1758 if (!opts) {
1759 goto fail_getopt;
1760 }
1761 break;
bellardea2384d2004-08-01 21:59:26 +00001762 }
1763 }
ths3b46e622007-09-17 08:09:54 +00001764
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001765 if (qemu_opts_foreach(&qemu_object_opts,
1766 user_creatable_add_opts_foreach,
1767 NULL, &local_err)) {
1768 error_report_err(local_err);
1769 goto fail_getopt;
1770 }
1771
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001772 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001773 if (quiet) {
1774 progress = 0;
1775 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001776 qemu_progress_init(progress, 1.0);
1777
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001778
balrog926c2d22007-10-31 01:11:44 +00001779 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001780 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001781
Kevin Wolf2dc83282014-02-21 16:24:05 +01001782 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001783 ret = print_block_option_help(out_filename, out_fmt);
1784 goto out;
1785 }
1786
bellardea2384d2004-08-01 21:59:26 +00001787 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001788 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001789 }
bellardea2384d2004-08-01 21:59:26 +00001790
thsf58c7b32008-06-05 21:53:49 +00001791
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001792 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001793 error_report("-B makes no sense when concatenating multiple input "
1794 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001795 ret = -1;
1796 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001797 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001798
Max Reitz40055952014-07-22 22:58:42 +02001799 src_flags = BDRV_O_FLAGS;
1800 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
1801 if (ret < 0) {
1802 error_report("Invalid source cache option: %s", src_cache);
1803 goto out;
1804 }
1805
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001806 qemu_progress_print(0, 100);
1807
Markus Armbruster26f54e92014-10-07 13:59:04 +02001808 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001809 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001810 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001811
1812 total_sectors = 0;
1813 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Kevin Wolf9aebf3b2014-09-25 09:54:02 +02001814 char *id = bs_n > 1 ? g_strdup_printf("source_%d", bs_i)
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001815 : g_strdup("source");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001816 blk[bs_i] = img_open(id, argv[optind + bs_i], fmt, src_flags,
1817 true, quiet);
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001818 g_free(id);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001819 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001820 ret = -1;
1821 goto out;
1822 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001823 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001824 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001825 if (bs_sectors[bs_i] < 0) {
1826 error_report("Could not get size of %s: %s",
1827 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1828 ret = -1;
1829 goto out;
1830 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001831 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001832 }
bellardea2384d2004-08-01 21:59:26 +00001833
Wenchao Xiaef806542013-12-04 17:10:57 +08001834 if (sn_opts) {
1835 ret = bdrv_snapshot_load_tmp(bs[0],
1836 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1837 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1838 &local_err);
1839 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001840 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001841 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001842 ret = -1;
1843 goto out;
1844 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001845
1846 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001847 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001848 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001849 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001850 ret = -1;
1851 goto out;
edison51ef6722010-09-21 19:58:41 -07001852 }
1853
Kevin Wolfefa84d42009-05-18 16:42:12 +02001854 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001855 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001856 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001857 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001858 ret = -1;
1859 goto out;
1860 }
balrog926c2d22007-10-31 01:11:44 +00001861
Max Reitzb65a5e12015-02-05 13:58:12 -05001862 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001863 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001864 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001865 ret = -1;
1866 goto out;
1867 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001868
Max Reitz2e024cd2015-02-11 09:58:46 -05001869 if (!skip_create) {
1870 if (!drv->create_opts) {
1871 error_report("Format driver '%s' does not support image creation",
1872 drv->format_name);
1873 ret = -1;
1874 goto out;
1875 }
Max Reitzf75613c2014-12-02 18:32:46 +01001876
Max Reitz2e024cd2015-02-11 09:58:46 -05001877 if (!proto_drv->create_opts) {
1878 error_report("Protocol driver '%s' does not support image creation",
1879 proto_drv->format_name);
1880 ret = -1;
1881 goto out;
1882 }
Max Reitzf75613c2014-12-02 18:32:46 +01001883
Max Reitz2e024cd2015-02-11 09:58:46 -05001884 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1885 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001886
Max Reitz2e024cd2015-02-11 09:58:46 -05001887 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001888 if (options) {
1889 qemu_opts_do_parse(opts, options, NULL, &local_err);
1890 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01001891 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001892 ret = -1;
1893 goto out;
1894 }
Max Reitz2e024cd2015-02-11 09:58:46 -05001895 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001896
Markus Armbruster39101f22015-02-12 16:46:36 +01001897 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
1898 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05001899 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
1900 if (ret < 0) {
1901 goto out;
1902 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001903 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001904
Kevin Wolfa18953f2010-10-14 15:46:04 +02001905 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001906 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001907 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001908 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001909 }
1910
Kevin Wolfefa84d42009-05-18 16:42:12 +02001911 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001912 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001913 bool encryption =
1914 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
1915 const char *preallocation =
1916 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001917
1918 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001919 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001920 ret = -1;
1921 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001922 }
1923
Chunyan Liu83d05212014-06-05 17:20:51 +08001924 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001925 error_report("Compression and encryption not supported at "
1926 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001927 ret = -1;
1928 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001929 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001930
Chunyan Liu83d05212014-06-05 17:20:51 +08001931 if (preallocation
1932 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02001933 {
1934 error_report("Compression and preallocation not supported at "
1935 "the same time");
1936 ret = -1;
1937 goto out;
1938 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001939 }
1940
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001941 if (!skip_create) {
1942 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001943 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001944 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001945 error_reportf_err(local_err, "%s: error while converting %s: ",
1946 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001947 goto out;
bellardea2384d2004-08-01 21:59:26 +00001948 }
1949 }
ths3b46e622007-09-17 08:09:54 +00001950
Peter Lieven5a37b602013-10-24 12:07:06 +02001951 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001952 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001953 if (ret < 0) {
1954 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02001955 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001956 }
1957
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001958 out_blk = img_open("target", out_filename, out_fmt, flags, true, quiet);
1959 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001960 ret = -1;
1961 goto out;
1962 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001963 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00001964
Peter Lievenf2521c92013-11-27 11:07:06 +01001965 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1966 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1967 * as maximum. */
1968 bufsectors = MIN(32768,
1969 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1970 out_bs->bl.discard_alignment))
1971 );
1972
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001973 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001974 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02001975 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08001976 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02001977 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001978 ret = -1;
1979 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02001980 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001981 error_report("output file is smaller than input file");
1982 ret = -1;
1983 goto out;
1984 }
1985 }
1986
Peter Lieven24f833c2013-11-27 11:07:07 +01001987 cluster_sectors = 0;
1988 ret = bdrv_get_info(out_bs, &bdi);
1989 if (ret < 0) {
1990 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001991 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001992 goto out;
1993 }
Peter Lieven24f833c2013-11-27 11:07:07 +01001994 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08001995 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01001996 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1997 }
1998
Kevin Wolf690c7302015-03-19 13:33:32 +01001999 state = (ImgConvertState) {
2000 .src = blk,
2001 .src_sectors = bs_sectors,
2002 .src_num = bs_n,
2003 .total_sectors = total_sectors,
2004 .target = out_blk,
2005 .compressed = compress,
2006 .target_has_backing = (bool) out_baseimg,
2007 .min_sparse = min_sparse,
2008 .cluster_sectors = cluster_sectors,
2009 .buf_sectors = bufsectors,
2010 };
2011 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002012
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002013out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002014 if (!ret) {
2015 qemu_progress_print(100, 0);
2016 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002017 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002018 qemu_opts_del(opts);
2019 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002020 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002021 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002022 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002023 if (blk) {
2024 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2025 blk_unref(blk[bs_i]);
2026 }
2027 g_free(blk);
2028 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002029 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002030fail_getopt:
2031 g_free(options);
2032
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002033 if (ret) {
2034 return 1;
2035 }
bellardea2384d2004-08-01 21:59:26 +00002036 return 0;
2037}
2038
bellard57d1a2b2004-08-03 21:15:11 +00002039
bellardfaea38e2006-08-05 21:31:00 +00002040static void dump_snapshots(BlockDriverState *bs)
2041{
2042 QEMUSnapshotInfo *sn_tab, *sn;
2043 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002044
2045 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2046 if (nb_sns <= 0)
2047 return;
2048 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002049 bdrv_snapshot_dump(fprintf, stdout, NULL);
2050 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002051 for(i = 0; i < nb_sns; i++) {
2052 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002053 bdrv_snapshot_dump(fprintf, stdout, sn);
2054 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002055 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002056 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002057}
2058
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002059static void dump_json_image_info_list(ImageInfoList *list)
2060{
Markus Armbruster4399c432014-04-25 16:50:32 +02002061 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002062 QString *str;
2063 QmpOutputVisitor *ov = qmp_output_visitor_new();
2064 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002065 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2066 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002067 obj = qmp_output_get_qobject(ov);
2068 str = qobject_to_json_pretty(obj);
2069 assert(str != NULL);
2070 printf("%s\n", qstring_get_str(str));
2071 qobject_decref(obj);
2072 qmp_output_visitor_cleanup(ov);
2073 QDECREF(str);
2074}
2075
Benoît Canetc054b3f2012-09-05 13:09:02 +02002076static void dump_json_image_info(ImageInfo *info)
2077{
Markus Armbruster4399c432014-04-25 16:50:32 +02002078 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002079 QString *str;
2080 QmpOutputVisitor *ov = qmp_output_visitor_new();
2081 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002082 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002083 obj = qmp_output_get_qobject(ov);
2084 str = qobject_to_json_pretty(obj);
2085 assert(str != NULL);
2086 printf("%s\n", qstring_get_str(str));
2087 qobject_decref(obj);
2088 qmp_output_visitor_cleanup(ov);
2089 QDECREF(str);
2090}
2091
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002092static void dump_human_image_info_list(ImageInfoList *list)
2093{
2094 ImageInfoList *elem;
2095 bool delim = false;
2096
2097 for (elem = list; elem; elem = elem->next) {
2098 if (delim) {
2099 printf("\n");
2100 }
2101 delim = true;
2102
Wenchao Xia5b917042013-05-25 11:09:45 +08002103 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002104 }
2105}
2106
2107static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2108{
2109 return strcmp(a, b) == 0;
2110}
2111
2112/**
2113 * Open an image file chain and return an ImageInfoList
2114 *
2115 * @filename: topmost image filename
2116 * @fmt: topmost image format (may be NULL to autodetect)
2117 * @chain: true - enumerate entire backing file chain
2118 * false - only topmost image file
2119 *
2120 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2121 * image file. If there was an error a message will have been printed to
2122 * stderr.
2123 */
2124static ImageInfoList *collect_image_info_list(const char *filename,
2125 const char *fmt,
2126 bool chain)
2127{
2128 ImageInfoList *head = NULL;
2129 ImageInfoList **last = &head;
2130 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002131 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002132
2133 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2134
2135 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002136 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002137 BlockDriverState *bs;
2138 ImageInfo *info;
2139 ImageInfoList *elem;
2140
2141 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2142 error_report("Backing file '%s' creates an infinite loop.",
2143 filename);
2144 goto err;
2145 }
2146 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2147
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002148 blk = img_open("image", filename, fmt,
2149 BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
2150 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002151 goto err;
2152 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002153 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002154
Wenchao Xia43526ec2013-06-06 12:27:58 +08002155 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002156 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002157 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002158 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002159 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002160 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002161
2162 elem = g_new0(ImageInfoList, 1);
2163 elem->value = info;
2164 *last = elem;
2165 last = &elem->next;
2166
Markus Armbruster26f54e92014-10-07 13:59:04 +02002167 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002168
2169 filename = fmt = NULL;
2170 if (chain) {
2171 if (info->has_full_backing_filename) {
2172 filename = info->full_backing_filename;
2173 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002174 error_report("Could not determine absolute backing filename,"
2175 " but backing filename '%s' present",
2176 info->backing_filename);
2177 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002178 }
2179 if (info->has_backing_filename_format) {
2180 fmt = info->backing_filename_format;
2181 }
2182 }
2183 }
2184 g_hash_table_destroy(filenames);
2185 return head;
2186
2187err:
2188 qapi_free_ImageInfoList(head);
2189 g_hash_table_destroy(filenames);
2190 return NULL;
2191}
2192
Benoît Canetc054b3f2012-09-05 13:09:02 +02002193static int img_info(int argc, char **argv)
2194{
2195 int c;
2196 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002197 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002198 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002199 ImageInfoList *list;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002200 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002201
bellardea2384d2004-08-01 21:59:26 +00002202 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002203 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002204 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002205 int option_index = 0;
2206 static const struct option long_options[] = {
2207 {"help", no_argument, 0, 'h'},
2208 {"format", required_argument, 0, 'f'},
2209 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002210 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002211 {"object", required_argument, 0, OPTION_OBJECT},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002212 {0, 0, 0, 0}
2213 };
2214 c = getopt_long(argc, argv, "f:h",
2215 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002216 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002217 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002218 }
bellardea2384d2004-08-01 21:59:26 +00002219 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002220 case '?':
bellardea2384d2004-08-01 21:59:26 +00002221 case 'h':
2222 help();
2223 break;
2224 case 'f':
2225 fmt = optarg;
2226 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002227 case OPTION_OUTPUT:
2228 output = optarg;
2229 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002230 case OPTION_BACKING_CHAIN:
2231 chain = true;
2232 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002233 case OPTION_OBJECT: {
2234 QemuOpts *opts;
2235 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2236 optarg, true);
2237 if (!opts) {
2238 return 1;
2239 }
2240 } break;
bellardea2384d2004-08-01 21:59:26 +00002241 }
2242 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002243 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002244 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002245 }
bellardea2384d2004-08-01 21:59:26 +00002246 filename = argv[optind++];
2247
Benoît Canetc054b3f2012-09-05 13:09:02 +02002248 if (output && !strcmp(output, "json")) {
2249 output_format = OFORMAT_JSON;
2250 } else if (output && !strcmp(output, "human")) {
2251 output_format = OFORMAT_HUMAN;
2252 } else if (output) {
2253 error_report("--output must be used with human or json as argument.");
2254 return 1;
2255 }
2256
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002257 if (qemu_opts_foreach(&qemu_object_opts,
2258 user_creatable_add_opts_foreach,
2259 NULL, &local_err)) {
2260 error_report_err(local_err);
2261 return 1;
2262 }
2263
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002264 list = collect_image_info_list(filename, fmt, chain);
2265 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002266 return 1;
2267 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002268
Benoît Canetc054b3f2012-09-05 13:09:02 +02002269 switch (output_format) {
2270 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002271 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002272 break;
2273 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002274 if (chain) {
2275 dump_json_image_info_list(list);
2276 } else {
2277 dump_json_image_info(list->value);
2278 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002279 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002280 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002281
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002282 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002283 return 0;
2284}
2285
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002286static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2287 MapEntry *next)
2288{
2289 switch (output_format) {
2290 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002291 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002292 error_report("File contains external, encrypted or compressed clusters.");
2293 exit(1);
2294 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002295 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002296 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002297 e->start, e->length,
2298 e->has_offset ? e->offset : 0,
2299 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002300 }
2301 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2302 * Modify the flags here to allow more coalescing.
2303 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002304 if (next && (!next->data || next->zero)) {
2305 next->data = false;
2306 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002307 }
2308 break;
2309 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002310 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2311 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002312 (e->start == 0 ? "[" : ",\n"),
2313 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002314 e->zero ? "true" : "false",
2315 e->data ? "true" : "false");
2316 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002317 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002318 }
2319 putchar('}');
2320
2321 if (!next) {
2322 printf("]\n");
2323 }
2324 break;
2325 }
2326}
2327
2328static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2329 int nb_sectors, MapEntry *e)
2330{
2331 int64_t ret;
2332 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002333 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002334 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002335
2336 /* As an optimization, we could cache the current range of unallocated
2337 * clusters in each file of the chain, and avoid querying the same
2338 * range repeatedly.
2339 */
2340
2341 depth = 0;
2342 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002343 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2344 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002345 if (ret < 0) {
2346 return ret;
2347 }
2348 assert(nb_sectors);
2349 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2350 break;
2351 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002352 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002353 if (bs == NULL) {
2354 ret = 0;
2355 break;
2356 }
2357
2358 depth++;
2359 }
2360
John Snow28756452016-02-05 13:12:33 -05002361 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2362
2363 *e = (MapEntry) {
2364 .start = sector_num * BDRV_SECTOR_SIZE,
2365 .length = nb_sectors * BDRV_SECTOR_SIZE,
2366 .data = !!(ret & BDRV_BLOCK_DATA),
2367 .zero = !!(ret & BDRV_BLOCK_ZERO),
2368 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2369 .has_offset = has_offset,
2370 .depth = depth,
2371 .has_filename = file && has_offset,
2372 .filename = file && has_offset ? file->filename : NULL,
2373 };
2374
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002375 return 0;
2376}
2377
Fam Zheng16b0d552016-01-26 11:59:02 +08002378static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2379{
2380 if (curr->length == 0) {
2381 return false;
2382 }
2383 if (curr->zero != next->zero ||
2384 curr->data != next->data ||
2385 curr->depth != next->depth ||
2386 curr->has_filename != next->has_filename ||
2387 curr->has_offset != next->has_offset) {
2388 return false;
2389 }
2390 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2391 return false;
2392 }
2393 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2394 return false;
2395 }
2396 return true;
2397}
2398
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002399static int img_map(int argc, char **argv)
2400{
2401 int c;
2402 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002403 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002404 BlockDriverState *bs;
2405 const char *filename, *fmt, *output;
2406 int64_t length;
2407 MapEntry curr = { .length = 0 }, next;
2408 int ret = 0;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002409 Error *local_err = NULL;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002410
2411 fmt = NULL;
2412 output = NULL;
2413 for (;;) {
2414 int option_index = 0;
2415 static const struct option long_options[] = {
2416 {"help", no_argument, 0, 'h'},
2417 {"format", required_argument, 0, 'f'},
2418 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002419 {"object", required_argument, 0, OPTION_OBJECT},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002420 {0, 0, 0, 0}
2421 };
2422 c = getopt_long(argc, argv, "f:h",
2423 long_options, &option_index);
2424 if (c == -1) {
2425 break;
2426 }
2427 switch (c) {
2428 case '?':
2429 case 'h':
2430 help();
2431 break;
2432 case 'f':
2433 fmt = optarg;
2434 break;
2435 case OPTION_OUTPUT:
2436 output = optarg;
2437 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002438 case OPTION_OBJECT: {
2439 QemuOpts *opts;
2440 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2441 optarg, true);
2442 if (!opts) {
2443 return 1;
2444 }
2445 } break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002446 }
2447 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002448 if (optind != argc - 1) {
2449 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002450 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002451 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002452
2453 if (output && !strcmp(output, "json")) {
2454 output_format = OFORMAT_JSON;
2455 } else if (output && !strcmp(output, "human")) {
2456 output_format = OFORMAT_HUMAN;
2457 } else if (output) {
2458 error_report("--output must be used with human or json as argument.");
2459 return 1;
2460 }
2461
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002462 if (qemu_opts_foreach(&qemu_object_opts,
2463 user_creatable_add_opts_foreach,
2464 NULL, &local_err)) {
2465 error_report_err(local_err);
2466 return 1;
2467 }
2468
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002469 blk = img_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
2470 if (!blk) {
2471 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002472 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002473 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002474
2475 if (output_format == OFORMAT_HUMAN) {
2476 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2477 }
2478
Max Reitzf1d3cd72015-02-05 13:58:18 -05002479 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002480 while (curr.start + curr.length < length) {
2481 int64_t nsectors_left;
2482 int64_t sector_num;
2483 int n;
2484
2485 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2486
2487 /* Probe up to 1 GiB at a time. */
2488 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2489 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2490 ret = get_block_status(bs, sector_num, n, &next);
2491
2492 if (ret < 0) {
2493 error_report("Could not read file metadata: %s", strerror(-ret));
2494 goto out;
2495 }
2496
Fam Zheng16b0d552016-01-26 11:59:02 +08002497 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002498 curr.length += next.length;
2499 continue;
2500 }
2501
2502 if (curr.length > 0) {
2503 dump_map_entry(output_format, &curr, &next);
2504 }
2505 curr = next;
2506 }
2507
2508 dump_map_entry(output_format, &curr, NULL);
2509
2510out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002511 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002512 return ret < 0;
2513}
2514
aliguorif7b4a942009-01-07 17:40:15 +00002515#define SNAPSHOT_LIST 1
2516#define SNAPSHOT_CREATE 2
2517#define SNAPSHOT_APPLY 3
2518#define SNAPSHOT_DELETE 4
2519
Stuart Brady153859b2009-06-07 00:42:17 +01002520static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002521{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002522 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002523 BlockDriverState *bs;
2524 QEMUSnapshotInfo sn;
2525 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002526 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002527 int action = 0;
2528 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002529 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002530 Error *err = NULL;
aliguorif7b4a942009-01-07 17:40:15 +00002531
Kevin Wolf710da702011-01-10 12:33:02 +01002532 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002533 /* Parse commandline parameters */
2534 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002535 static const struct option long_options[] = {
2536 {"help", no_argument, 0, 'h'},
2537 {"object", required_argument, 0, OPTION_OBJECT},
2538 {0, 0, 0, 0}
2539 };
2540 c = getopt_long(argc, argv, "la:c:d:hq",
2541 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002542 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002543 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002544 }
aliguorif7b4a942009-01-07 17:40:15 +00002545 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002546 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002547 case 'h':
2548 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002549 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002550 case 'l':
2551 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002552 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002553 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002554 }
2555 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002556 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002557 break;
2558 case 'a':
2559 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002560 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002561 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002562 }
2563 action = SNAPSHOT_APPLY;
2564 snapshot_name = optarg;
2565 break;
2566 case 'c':
2567 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002568 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002569 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002570 }
2571 action = SNAPSHOT_CREATE;
2572 snapshot_name = optarg;
2573 break;
2574 case 'd':
2575 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002576 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002577 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002578 }
2579 action = SNAPSHOT_DELETE;
2580 snapshot_name = optarg;
2581 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002582 case 'q':
2583 quiet = true;
2584 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002585 case OPTION_OBJECT: {
2586 QemuOpts *opts;
2587 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2588 optarg, true);
2589 if (!opts) {
2590 return 1;
2591 }
2592 } break;
aliguorif7b4a942009-01-07 17:40:15 +00002593 }
2594 }
2595
Kevin Wolffc11eb22013-08-05 10:53:04 +02002596 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002597 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002598 }
aliguorif7b4a942009-01-07 17:40:15 +00002599 filename = argv[optind++];
2600
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002601 if (qemu_opts_foreach(&qemu_object_opts,
2602 user_creatable_add_opts_foreach,
2603 NULL, &err)) {
2604 error_report_err(err);
2605 return 1;
2606 }
2607
aliguorif7b4a942009-01-07 17:40:15 +00002608 /* Open the image */
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002609 blk = img_open("image", filename, NULL, bdrv_oflags, true, quiet);
2610 if (!blk) {
2611 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002612 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002613 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002614
2615 /* Perform the requested action */
2616 switch(action) {
2617 case SNAPSHOT_LIST:
2618 dump_snapshots(bs);
2619 break;
2620
2621 case SNAPSHOT_CREATE:
2622 memset(&sn, 0, sizeof(sn));
2623 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2624
2625 qemu_gettimeofday(&tv);
2626 sn.date_sec = tv.tv_sec;
2627 sn.date_nsec = tv.tv_usec * 1000;
2628
2629 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002630 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002631 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002632 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002633 }
aliguorif7b4a942009-01-07 17:40:15 +00002634 break;
2635
2636 case SNAPSHOT_APPLY:
2637 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002638 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002639 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002640 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002641 }
aliguorif7b4a942009-01-07 17:40:15 +00002642 break;
2643
2644 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002645 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002646 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002647 error_reportf_err(err, "Could not delete snapshot '%s': ",
2648 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002649 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002650 }
aliguorif7b4a942009-01-07 17:40:15 +00002651 break;
2652 }
2653
2654 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002655 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002656 if (ret) {
2657 return 1;
2658 }
Stuart Brady153859b2009-06-07 00:42:17 +01002659 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002660}
2661
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002662static int img_rebase(int argc, char **argv)
2663{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002664 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002665 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002666 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002667 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2668 int c, flags, src_flags, ret;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002669 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002670 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002671 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002672 Error *local_err = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002673
2674 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002675 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002676 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002677 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002678 out_baseimg = NULL;
2679 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002680 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002681 static const struct option long_options[] = {
2682 {"help", no_argument, 0, 'h'},
2683 {"object", required_argument, 0, OPTION_OBJECT},
2684 {0, 0, 0, 0}
2685 };
2686 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2687 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002688 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002689 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002690 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002691 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002692 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002693 case 'h':
2694 help();
2695 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002696 case 'f':
2697 fmt = optarg;
2698 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002699 case 'F':
2700 out_basefmt = optarg;
2701 break;
2702 case 'b':
2703 out_baseimg = optarg;
2704 break;
2705 case 'u':
2706 unsafe = 1;
2707 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002708 case 'p':
2709 progress = 1;
2710 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002711 case 't':
2712 cache = optarg;
2713 break;
Max Reitz40055952014-07-22 22:58:42 +02002714 case 'T':
2715 src_cache = optarg;
2716 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002717 case 'q':
2718 quiet = true;
2719 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002720 case OPTION_OBJECT: {
2721 QemuOpts *opts;
2722 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2723 optarg, true);
2724 if (!opts) {
2725 return 1;
2726 }
2727 } break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002728 }
2729 }
2730
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002731 if (quiet) {
2732 progress = 0;
2733 }
2734
Fam Zhengac1307a2014-04-22 13:36:11 +08002735 if (optind != argc - 1) {
2736 error_exit("Expecting one image file name");
2737 }
2738 if (!unsafe && !out_baseimg) {
2739 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002740 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002741 filename = argv[optind++];
2742
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002743 if (qemu_opts_foreach(&qemu_object_opts,
2744 user_creatable_add_opts_foreach,
2745 NULL, &local_err)) {
2746 error_report_err(local_err);
2747 return 1;
2748 }
2749
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002750 qemu_progress_init(progress, 2.0);
2751 qemu_progress_print(0, 100);
2752
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002753 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002754 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002755 if (ret < 0) {
2756 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002757 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002758 }
2759
Max Reitz40055952014-07-22 22:58:42 +02002760 src_flags = BDRV_O_FLAGS;
2761 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
2762 if (ret < 0) {
2763 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002764 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002765 }
2766
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002767 /*
2768 * Open the images.
2769 *
2770 * Ignore the old backing file for unsafe rebase in case we want to correct
2771 * the reference to a renamed or moved backing file.
2772 */
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002773 blk = img_open("image", filename, fmt, flags, true, quiet);
2774 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002775 ret = -1;
2776 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002777 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002778 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002779
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002780 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002781 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002782 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002783 ret = -1;
2784 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002785 }
2786 }
2787
2788 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002789 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002790 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002791 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002792
Max Reitz644483d2015-02-05 13:58:17 -05002793 if (bs->backing_format[0] != '\0') {
2794 options = qdict_new();
2795 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2796 }
2797
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002798 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitz644483d2015-02-05 13:58:17 -05002799 blk_old_backing = blk_new_open("old_backing", backing_name, NULL,
2800 options, src_flags, &local_err);
2801 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002802 error_reportf_err(local_err,
2803 "Could not open old backing file '%s': ",
2804 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002805 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002806 }
Max Reitz644483d2015-02-05 13:58:17 -05002807
Alex Bligha6166732012-10-16 13:46:18 +01002808 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002809 if (out_basefmt) {
2810 options = qdict_new();
2811 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2812 } else {
2813 options = NULL;
2814 }
2815
2816 blk_new_backing = blk_new_open("new_backing", out_baseimg, NULL,
2817 options, src_flags, &local_err);
2818 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002819 error_reportf_err(local_err,
2820 "Could not open new backing file '%s': ",
2821 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002822 goto out;
2823 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002824 }
2825 }
2826
2827 /*
2828 * Check each unallocated cluster in the COW file. If it is unallocated,
2829 * accesses go to the backing file. We must therefore compare this cluster
2830 * in the old and new backing file, and if they differ we need to copy it
2831 * from the old backing file into the COW file.
2832 *
2833 * If qemu-img crashes during this step, no harm is done. The content of
2834 * the image is the same as the original one at any time.
2835 */
2836 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002837 int64_t num_sectors;
2838 int64_t old_backing_num_sectors;
2839 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002840 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002841 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08002842 uint8_t * buf_old;
2843 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02002844 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002845
Max Reitzf1d3cd72015-02-05 13:58:18 -05002846 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2847 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002848
Max Reitzf1d3cd72015-02-05 13:58:18 -05002849 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002850 if (num_sectors < 0) {
2851 error_report("Could not get size of '%s': %s",
2852 filename, strerror(-num_sectors));
2853 ret = -1;
2854 goto out;
2855 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002856 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002857 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002858 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002859
2860 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2861 error_report("Could not get size of '%s': %s",
2862 backing_name, strerror(-old_backing_num_sectors));
2863 ret = -1;
2864 goto out;
2865 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002866 if (blk_new_backing) {
2867 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002868 if (new_backing_num_sectors < 0) {
2869 error_report("Could not get size of '%s': %s",
2870 out_baseimg, strerror(-new_backing_num_sectors));
2871 ret = -1;
2872 goto out;
2873 }
Alex Bligha6166732012-10-16 13:46:18 +01002874 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002875
Kevin Wolf1f710492012-10-12 14:29:18 +02002876 if (num_sectors != 0) {
2877 local_progress = (float)100 /
2878 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2879 }
2880
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002881 for (sector = 0; sector < num_sectors; sector += n) {
2882
2883 /* How many sectors can we handle with the next read? */
2884 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2885 n = (IO_BUF_SIZE / 512);
2886 } else {
2887 n = num_sectors - sector;
2888 }
2889
2890 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02002891 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02002892 if (ret < 0) {
2893 error_report("error while reading image metadata: %s",
2894 strerror(-ret));
2895 goto out;
2896 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02002897 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002898 continue;
2899 }
2900
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002901 /*
2902 * Read old and new backing file and take into consideration that
2903 * backing files may be smaller than the COW image.
2904 */
2905 if (sector >= old_backing_num_sectors) {
2906 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2907 } else {
2908 if (sector + n > old_backing_num_sectors) {
2909 n = old_backing_num_sectors - sector;
2910 }
2911
Max Reitzf1d3cd72015-02-05 13:58:18 -05002912 ret = blk_read(blk_old_backing, sector, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002913 if (ret < 0) {
2914 error_report("error while reading from old backing file");
2915 goto out;
2916 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002917 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002918
Max Reitzf1d3cd72015-02-05 13:58:18 -05002919 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002920 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2921 } else {
2922 if (sector + n > new_backing_num_sectors) {
2923 n = new_backing_num_sectors - sector;
2924 }
2925
Max Reitzf1d3cd72015-02-05 13:58:18 -05002926 ret = blk_read(blk_new_backing, sector, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002927 if (ret < 0) {
2928 error_report("error while reading from new backing file");
2929 goto out;
2930 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002931 }
2932
2933 /* If they differ, we need to write to the COW file */
2934 uint64_t written = 0;
2935
2936 while (written < n) {
2937 int pnum;
2938
2939 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01002940 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002941 {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002942 ret = blk_write(blk, sector + written,
2943 buf_old + written * 512, pnum);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002944 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002945 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002946 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002947 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002948 }
2949 }
2950
2951 written += pnum;
2952 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002953 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002954 }
TeLeMand6771bf2010-02-08 16:20:00 +08002955
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002956 qemu_vfree(buf_old);
2957 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002958 }
2959
2960 /*
2961 * Change the backing file. All clusters that are different from the old
2962 * backing file are overwritten in the COW file now, so the visible content
2963 * doesn't change when we switch the backing file.
2964 */
Alex Bligha6166732012-10-16 13:46:18 +01002965 if (out_baseimg && *out_baseimg) {
2966 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2967 } else {
2968 ret = bdrv_change_backing_file(bs, NULL, NULL);
2969 }
2970
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002971 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002972 error_report("Could not change the backing file to '%s': No "
2973 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002974 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002975 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002976 out_baseimg, strerror(-ret));
2977 }
2978
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002979 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002980 /*
2981 * TODO At this point it is possible to check if any clusters that are
2982 * allocated in the COW file are the same in the backing file. If so, they
2983 * could be dropped from the COW file. Don't do this before switching the
2984 * backing file, in case of a crash this would lead to corruption.
2985 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002986out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002987 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002988 /* Cleanup */
2989 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002990 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002991 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002992 }
2993
Markus Armbruster26f54e92014-10-07 13:59:04 +02002994 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002995 if (ret) {
2996 return 1;
2997 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002998 return 0;
2999}
3000
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003001static int img_resize(int argc, char **argv)
3002{
Markus Armbruster6750e792015-02-12 17:43:08 +01003003 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003004 int c, ret, relative;
3005 const char *filename, *fmt, *size;
3006 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003007 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003008 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003009 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003010 Error *local_err = NULL;
3011
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003012 static QemuOptsList resize_options = {
3013 .name = "resize_options",
3014 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3015 .desc = {
3016 {
3017 .name = BLOCK_OPT_SIZE,
3018 .type = QEMU_OPT_SIZE,
3019 .help = "Virtual disk size"
3020 }, {
3021 /* end of list */
3022 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003023 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003024 };
3025
Kevin Wolfe80fec72011-04-29 10:58:12 +02003026 /* Remove size from argv manually so that negative numbers are not treated
3027 * as options by getopt. */
3028 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003029 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003030 return 1;
3031 }
3032
3033 size = argv[--argc];
3034
3035 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003036 fmt = NULL;
3037 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003038 static const struct option long_options[] = {
3039 {"help", no_argument, 0, 'h'},
3040 {"object", required_argument, 0, OPTION_OBJECT},
3041 {0, 0, 0, 0}
3042 };
3043 c = getopt_long(argc, argv, "f:hq",
3044 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003045 if (c == -1) {
3046 break;
3047 }
3048 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003049 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003050 case 'h':
3051 help();
3052 break;
3053 case 'f':
3054 fmt = optarg;
3055 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003056 case 'q':
3057 quiet = true;
3058 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003059 case OPTION_OBJECT: {
3060 QemuOpts *opts;
3061 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3062 optarg, true);
3063 if (!opts) {
3064 return 1;
3065 }
3066 } break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003067 }
3068 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003069 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003070 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003071 }
3072 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003073
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003074 if (qemu_opts_foreach(&qemu_object_opts,
3075 user_creatable_add_opts_foreach,
3076 NULL, &local_err)) {
3077 error_report_err(local_err);
3078 return 1;
3079 }
3080
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003081 /* Choose grow, shrink, or absolute resize mode */
3082 switch (size[0]) {
3083 case '+':
3084 relative = 1;
3085 size++;
3086 break;
3087 case '-':
3088 relative = -1;
3089 size++;
3090 break;
3091 default:
3092 relative = 0;
3093 break;
3094 }
3095
3096 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003097 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003098 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003099 if (err) {
3100 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003101 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003102 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003103 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003104 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003105 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3106 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003107
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003108 blk = img_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
3109 true, quiet);
3110 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003111 ret = -1;
3112 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003113 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003114
3115 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003116 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003117 } else {
3118 total_size = n;
3119 }
3120 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003121 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003122 ret = -1;
3123 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003124 }
3125
Max Reitzf1d3cd72015-02-05 13:58:18 -05003126 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003127 switch (ret) {
3128 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003129 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003130 break;
3131 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003132 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003133 break;
3134 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003135 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003136 break;
3137 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003138 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003139 break;
3140 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003141out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003142 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003143 if (ret) {
3144 return 1;
3145 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003146 return 0;
3147}
3148
Max Reitz76a3a342014-10-27 11:12:51 +01003149static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003150 int64_t offset, int64_t total_work_size,
3151 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003152{
3153 qemu_progress_print(100.f * offset / total_work_size, 0);
3154}
3155
Max Reitz6f176b42013-09-03 10:09:50 +02003156static int img_amend(int argc, char **argv)
3157{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003158 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003159 int c, ret = 0;
3160 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003161 QemuOptsList *create_opts = NULL;
3162 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003163 const char *fmt = NULL, *filename, *cache;
3164 int flags;
Max Reitz76a3a342014-10-27 11:12:51 +01003165 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003166 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003167 BlockDriverState *bs = NULL;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003168 Error *local_err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003169
Max Reitzbd39e6e2014-07-22 22:58:43 +02003170 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003171 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003172 static const struct option long_options[] = {
3173 {"help", no_argument, 0, 'h'},
3174 {"object", required_argument, 0, OPTION_OBJECT},
3175 {0, 0, 0, 0}
3176 };
3177 c = getopt_long(argc, argv, "ho:f:t:pq",
3178 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003179 if (c == -1) {
3180 break;
3181 }
3182
3183 switch (c) {
3184 case 'h':
3185 case '?':
3186 help();
3187 break;
3188 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003189 if (!is_valid_option_list(optarg)) {
3190 error_report("Invalid option list: %s", optarg);
3191 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003192 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003193 }
3194 if (!options) {
3195 options = g_strdup(optarg);
3196 } else {
3197 char *old_options = options;
3198 options = g_strdup_printf("%s,%s", options, optarg);
3199 g_free(old_options);
3200 }
Max Reitz6f176b42013-09-03 10:09:50 +02003201 break;
3202 case 'f':
3203 fmt = optarg;
3204 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003205 case 't':
3206 cache = optarg;
3207 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003208 case 'p':
3209 progress = true;
3210 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003211 case 'q':
3212 quiet = true;
3213 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003214 case OPTION_OBJECT:
3215 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3216 optarg, true);
3217 if (!opts) {
3218 ret = -1;
3219 goto out_no_progress;
3220 }
3221 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003222 }
3223 }
3224
Max Reitz6f176b42013-09-03 10:09:50 +02003225 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003226 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003227 }
3228
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003229 if (qemu_opts_foreach(&qemu_object_opts,
3230 user_creatable_add_opts_foreach,
3231 NULL, &local_err)) {
3232 error_report_err(local_err);
3233 ret = -1;
3234 goto out_no_progress;
3235 }
3236
Max Reitz76a3a342014-10-27 11:12:51 +01003237 if (quiet) {
3238 progress = false;
3239 }
3240 qemu_progress_init(progress, 1.0);
3241
Kevin Wolfa283cb62014-02-21 16:24:07 +01003242 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3243 if (fmt && has_help_option(options)) {
3244 /* If a format is explicitly specified (and possibly no filename is
3245 * given), print option help here */
3246 ret = print_block_option_help(filename, fmt);
3247 goto out;
3248 }
3249
3250 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003251 error_report("Expecting one image file name");
3252 ret = -1;
3253 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003254 }
Max Reitz6f176b42013-09-03 10:09:50 +02003255
Max Reitzbd39e6e2014-07-22 22:58:43 +02003256 flags = BDRV_O_FLAGS | BDRV_O_RDWR;
3257 ret = bdrv_parse_cache_flags(cache, &flags);
3258 if (ret < 0) {
3259 error_report("Invalid cache option: %s", cache);
3260 goto out;
3261 }
3262
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003263 blk = img_open("image", filename, fmt, flags, true, quiet);
3264 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003265 ret = -1;
3266 goto out;
3267 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003268 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003269
3270 fmt = bs->drv->format_name;
3271
Kevin Wolf626f84f2014-02-21 16:24:06 +01003272 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003273 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003274 ret = print_block_option_help(filename, fmt);
3275 goto out;
3276 }
3277
Max Reitzb2439d22014-12-02 18:32:47 +01003278 if (!bs->drv->create_opts) {
3279 error_report("Format driver '%s' does not support any options to amend",
3280 fmt);
3281 ret = -1;
3282 goto out;
3283 }
3284
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003285 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003286 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003287 if (options) {
3288 qemu_opts_do_parse(opts, options, NULL, &err);
3289 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003290 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003291 ret = -1;
3292 goto out;
3293 }
Max Reitz6f176b42013-09-03 10:09:50 +02003294 }
3295
Max Reitz76a3a342014-10-27 11:12:51 +01003296 /* In case the driver does not call amend_status_cb() */
3297 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003298 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003299 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003300 if (ret < 0) {
3301 error_report("Error while amending options: %s", strerror(-ret));
3302 goto out;
3303 }
3304
3305out:
Max Reitz76a3a342014-10-27 11:12:51 +01003306 qemu_progress_end();
3307
Max Reitze814dff2015-08-20 16:00:38 -07003308out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003309 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003310 qemu_opts_del(opts);
3311 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003312 g_free(options);
3313
Max Reitz6f176b42013-09-03 10:09:50 +02003314 if (ret) {
3315 return 1;
3316 }
3317 return 0;
3318}
3319
Anthony Liguoric227f092009-10-01 16:12:16 -05003320static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003321#define DEF(option, callback, arg_string) \
3322 { option, callback },
3323#include "qemu-img-cmds.h"
3324#undef DEF
3325#undef GEN_DOCS
3326 { NULL, NULL, },
3327};
3328
bellardea2384d2004-08-01 21:59:26 +00003329int main(int argc, char **argv)
3330{
Anthony Liguoric227f092009-10-01 16:12:16 -05003331 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003332 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003333 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003334 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003335 static const struct option long_options[] = {
3336 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04003337 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04003338 {0, 0, 0, 0}
3339 };
bellardea2384d2004-08-01 21:59:26 +00003340
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003341#ifdef CONFIG_POSIX
3342 signal(SIGPIPE, SIG_IGN);
3343#endif
3344
Kevin Wolf53f76e52010-12-16 15:10:32 +01003345 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003346 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003347
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003348 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003349 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003350 exit(EXIT_FAILURE);
3351 }
3352
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003353 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003354 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003355 if (argc < 2) {
3356 error_exit("Not enough arguments");
3357 }
Stuart Brady153859b2009-06-07 00:42:17 +01003358 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01003359
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003360 qemu_add_opts(&qemu_object_opts);
3361
Stuart Brady153859b2009-06-07 00:42:17 +01003362 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04003363 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01003364 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04003365 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01003366 }
bellardea2384d2004-08-01 21:59:26 +00003367 }
Stuart Brady153859b2009-06-07 00:42:17 +01003368
Jeff Cody5f6979c2014-04-28 14:37:18 -04003369 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04003370
3371 if (c == 'h') {
3372 help();
3373 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04003374 if (c == 'v') {
3375 printf(QEMU_IMG_VERSION);
3376 return 0;
3377 }
Jeff Cody7db16892014-04-25 17:02:32 -04003378
Stuart Brady153859b2009-06-07 00:42:17 +01003379 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003380 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003381}