blob: c57898ee51e0c7e370ff2eb7915142d91268bd01 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020026#include "qapi-visit.h"
27#include "qapi/qmp-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010028#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010029#include "qapi/qmp/qjson.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020030#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000031#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/option.h"
33#include "qemu/error-report.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000034#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020036#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010037#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020038#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080039#include "block/qapi.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020040#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000041
Don Slutz61979a62015-01-09 10:17:35 -050042#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Jeff Cody5f6979c2014-04-28 14:37:18 -040043 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
44
Anthony Liguoric227f092009-10-01 16:12:16 -050045typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010046 const char *name;
47 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050048} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010049
Federico Simoncelli8599ea42013-01-28 06:59:47 -050050enum {
51 OPTION_OUTPUT = 256,
52 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000053 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000054 OPTION_IMAGE_OPTS = 259,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050055};
56
57typedef enum OutputFormat {
58 OFORMAT_JSON,
59 OFORMAT_HUMAN,
60} OutputFormat;
61
aurel32137519c2008-11-30 19:12:49 +000062/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010063#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040064#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000065
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010066static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000067{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010068 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000069}
70
Fam Zhengac1307a2014-04-22 13:36:11 +080071static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
72{
73 va_list ap;
74
75 error_printf("qemu-img: ");
76
77 va_start(ap, fmt);
78 error_vprintf(fmt, ap);
79 va_end(ap);
80
81 error_printf("\nTry 'qemu-img --help' for more information\n");
82 exit(EXIT_FAILURE);
83}
84
blueswir1d2c639d2009-01-24 18:19:25 +000085/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080086static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000087{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010088 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040089 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030090 "usage: qemu-img command [command options]\n"
91 "QEMU disk image utility\n"
92 "\n"
93 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010094#define DEF(option, callback, arg_string) \
95 " " arg_string "\n"
96#include "qemu-img-cmds.h"
97#undef DEF
98#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030099 "\n"
100 "Command parameters:\n"
101 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000102 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
103 " manual page for a description of the object properties. The most common\n"
104 " object type is a 'secret', which is used to supply passwords and/or\n"
105 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300106 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400107 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800108 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
109 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100110 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
111 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300112 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200113 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
114 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
115 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300116 " 'output_filename' is the destination disk image filename\n"
117 " 'output_fmt' is the destination format\n"
118 " 'options' is a comma separated list of format specific options in a\n"
119 " name=value format. Use -o ? for an overview of the options supported by the\n"
120 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800121 " 'snapshot_param' is param used for internal snapshot, format\n"
122 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
123 " '[ID_OR_NAME]'\n"
124 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
125 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300126 " '-c' indicates that target image must be compressed (qcow format only)\n"
127 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
128 " match exactly. The image doesn't need a working backing file before\n"
129 " rebasing in this case (useful for renaming the backing file)\n"
130 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200131 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100132 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200133 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
134 " contain only zeros for qemu-img to create a sparse image during\n"
135 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
136 " unallocated or zero sectors, and the destination image will always be\n"
137 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200138 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100139 " '-n' skips the target volume creation (useful if the volume is created\n"
140 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300141 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200142 "Parameters to check subcommand:\n"
143 " '-r' tries to repair any inconsistencies that are found during the check.\n"
144 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
145 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200146 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200147 "\n"
malc3f020d72010-02-08 12:04:56 +0300148 "Parameters to snapshot subcommand:\n"
149 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
150 " '-a' applies a snapshot (revert disk to saved state)\n"
151 " '-c' creates a snapshot\n"
152 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100153 " '-l' lists all snapshots in the given image\n"
154 "\n"
155 "Parameters to compare subcommand:\n"
156 " '-f' first image format\n"
157 " '-F' second image format\n"
158 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100159
160 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100161 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000162 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800163 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000164}
165
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000166static QemuOptsList qemu_object_opts = {
167 .name = "object",
168 .implied_opt_name = "qom-type",
169 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
170 .desc = {
171 { }
172 },
173};
174
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000175static QemuOptsList qemu_source_opts = {
176 .name = "source",
177 .implied_opt_name = "file",
178 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
179 .desc = {
180 { }
181 },
182};
183
Stefan Weil7c30f652013-06-16 17:01:05 +0200184static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100185{
186 int ret = 0;
187 if (!quiet) {
188 va_list args;
189 va_start(args, fmt);
190 ret = vprintf(fmt, args);
191 va_end(args);
192 }
193 return ret;
194}
195
bellardea2384d2004-08-01 21:59:26 +0000196
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100197static int print_block_option_help(const char *filename, const char *fmt)
198{
199 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800200 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500201 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100202
203 /* Find driver and parse its options */
204 drv = bdrv_find_format(fmt);
205 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100206 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100207 return 1;
208 }
209
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800210 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100211 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500212 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100213 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100214 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800215 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100216 return 1;
217 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800218 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100219 }
220
Chunyan Liu83d05212014-06-05 17:20:51 +0800221 qemu_opts_print_help(create_opts);
222 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100223 return 0;
224}
225
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000226
227static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000228 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000229{
230 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000231 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000232
233 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000234 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
235 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000236 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
237 if (qemu_read_password(password, sizeof(password)) < 0) {
238 error_report("No password given");
239 return -1;
240 }
241 if (bdrv_set_key(bs, password) < 0) {
242 error_report("invalid password");
243 return -1;
244 }
245 }
246 return 0;
247}
248
249
Max Reitzefaa7c42016-03-16 19:54:38 +0100250static BlockBackend *img_open_opts(const char *optstr,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000251 QemuOpts *opts, int flags,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000252 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000253{
254 QDict *options;
255 Error *local_err = NULL;
256 BlockBackend *blk;
257 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100258 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000259 if (!blk) {
260 error_reportf_err(local_err, "Could not open '%s'", optstr);
261 return NULL;
262 }
263
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000264 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000265 blk_unref(blk);
266 return NULL;
267 }
268 return blk;
269}
270
Max Reitzefaa7c42016-03-16 19:54:38 +0100271static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000272 const char *fmt, int flags,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000273 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000274{
275 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200276 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500277 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100278
bellard75c23802004-08-27 21:28:58 +0000279 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500280 options = qdict_new();
281 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000282 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100283
Max Reitzefaa7c42016-03-16 19:54:38 +0100284 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500285 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100286 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000287 return NULL;
bellard75c23802004-08-27 21:28:58 +0000288 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100289
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000290 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000291 blk_unref(blk);
292 return NULL;
bellard75c23802004-08-27 21:28:58 +0000293 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200294 return blk;
bellard75c23802004-08-27 21:28:58 +0000295}
296
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000297
Max Reitzefaa7c42016-03-16 19:54:38 +0100298static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000299 const char *filename,
300 const char *fmt, int flags,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000301 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000302{
303 BlockBackend *blk;
304 if (image_opts) {
305 QemuOpts *opts;
306 if (fmt) {
307 error_report("--image-opts and --format are mutually exclusive");
308 return NULL;
309 }
310 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
311 filename, true);
312 if (!opts) {
313 return NULL;
314 }
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000315 blk = img_open_opts(filename, opts, flags, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000316 } else {
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000317 blk = img_open_file(filename, fmt, flags, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000318 }
319 return blk;
320}
321
322
Chunyan Liu83d05212014-06-05 17:20:51 +0800323static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100324 const char *base_filename,
325 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200326{
Markus Armbruster6750e792015-02-12 17:43:08 +0100327 Error *err = NULL;
328
Kevin Wolfefa84d42009-05-18 16:42:12 +0200329 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100330 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100331 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100332 error_report("Backing file not supported for file format '%s'",
333 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100334 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900335 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200336 }
337 }
338 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100339 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100340 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100341 error_report("Backing file format not supported for file "
342 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100343 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900344 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200345 }
346 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900347 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200348}
349
bellardea2384d2004-08-01 21:59:26 +0000350static int img_create(int argc, char **argv)
351{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200352 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100353 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000354 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000355 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000356 const char *filename;
357 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200358 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200359 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100360 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000361
bellardea2384d2004-08-01 21:59:26 +0000362 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000363 static const struct option long_options[] = {
364 {"help", no_argument, 0, 'h'},
365 {"object", required_argument, 0, OPTION_OBJECT},
366 {0, 0, 0, 0}
367 };
368 c = getopt_long(argc, argv, "F:b:f:he6o:q",
369 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100370 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000371 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100372 }
bellardea2384d2004-08-01 21:59:26 +0000373 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100374 case '?':
bellardea2384d2004-08-01 21:59:26 +0000375 case 'h':
376 help();
377 break;
aliguori9230eaf2009-03-28 17:55:19 +0000378 case 'F':
379 base_fmt = optarg;
380 break;
bellardea2384d2004-08-01 21:59:26 +0000381 case 'b':
382 base_filename = optarg;
383 break;
384 case 'f':
385 fmt = optarg;
386 break;
387 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200388 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100389 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100390 goto fail;
thsd8871c52007-10-24 16:11:42 +0000391 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200392 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100393 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100394 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200395 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100396 if (!is_valid_option_list(optarg)) {
397 error_report("Invalid option list: %s", optarg);
398 goto fail;
399 }
400 if (!options) {
401 options = g_strdup(optarg);
402 } else {
403 char *old_options = options;
404 options = g_strdup_printf("%s,%s", options, optarg);
405 g_free(old_options);
406 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200407 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100408 case 'q':
409 quiet = true;
410 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000411 case OPTION_OBJECT: {
412 QemuOpts *opts;
413 opts = qemu_opts_parse_noisily(&qemu_object_opts,
414 optarg, true);
415 if (!opts) {
416 goto fail;
417 }
418 } break;
bellardea2384d2004-08-01 21:59:26 +0000419 }
420 }
aliguori9230eaf2009-03-28 17:55:19 +0000421
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900422 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100423 filename = (optind < argc) ? argv[optind] : NULL;
424 if (options && has_help_option(options)) {
425 g_free(options);
426 return print_block_option_help(filename, fmt);
427 }
428
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100429 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800430 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100431 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100432 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900433
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000434 if (qemu_opts_foreach(&qemu_object_opts,
435 user_creatable_add_opts_foreach,
436 NULL, &local_err)) {
437 error_report_err(local_err);
438 goto fail;
439 }
440
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100441 /* Get image size, if specified */
442 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100443 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100444 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +0200445 sval = qemu_strtosz_suffix(argv[optind++], &end,
446 QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +0100447 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800448 if (sval == -ERANGE) {
449 error_report("Image size must be less than 8 EiB!");
450 } else {
451 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200452 "G, T, P or E suffixes for ");
453 error_report("kilobytes, megabytes, gigabytes, terabytes, "
454 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800455 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100456 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100457 }
458 img_size = (uint64_t)sval;
459 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200460 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800461 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200462 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100463
Luiz Capitulino9b375252012-11-30 10:52:05 -0200464 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100465 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100466 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100467 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100468 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900469 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200470
Kevin Wolf77386bf2014-02-21 16:24:04 +0100471 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000472 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100473
474fail:
475 g_free(options);
476 return 1;
bellardea2384d2004-08-01 21:59:26 +0000477}
478
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100479static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500480{
Markus Armbruster4399c432014-04-25 16:50:32 +0200481 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500482 QString *str;
483 QmpOutputVisitor *ov = qmp_output_visitor_new();
484 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -0700485 visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
486 &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500487 obj = qmp_output_get_qobject(ov);
488 str = qobject_to_json_pretty(obj);
489 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100490 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500491 qobject_decref(obj);
492 qmp_output_visitor_cleanup(ov);
493 QDECREF(str);
494}
495
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100496static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500497{
498 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100499 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500500 } else {
501 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100502 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
503 "Data may be corrupted, or further writes to the image "
504 "may corrupt it.\n",
505 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500506 }
507
508 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100509 qprintf(quiet,
510 "\n%" PRId64 " leaked clusters were found on the image.\n"
511 "This means waste of disk space, but no harm to data.\n",
512 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500513 }
514
515 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100516 qprintf(quiet,
517 "\n%" PRId64
518 " internal errors have occurred during the check.\n",
519 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500520 }
521 }
522
523 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100524 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
525 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
526 check->allocated_clusters, check->total_clusters,
527 check->allocated_clusters * 100.0 / check->total_clusters,
528 check->fragmented_clusters * 100.0 / check->allocated_clusters,
529 check->compressed_clusters * 100.0 /
530 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500531 }
532
533 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100534 qprintf(quiet,
535 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500536 }
537}
538
539static int collect_image_check(BlockDriverState *bs,
540 ImageCheck *check,
541 const char *filename,
542 const char *fmt,
543 int fix)
544{
545 int ret;
546 BdrvCheckResult result;
547
548 ret = bdrv_check(bs, &result, fix);
549 if (ret < 0) {
550 return ret;
551 }
552
553 check->filename = g_strdup(filename);
554 check->format = g_strdup(bdrv_get_format_name(bs));
555 check->check_errors = result.check_errors;
556 check->corruptions = result.corruptions;
557 check->has_corruptions = result.corruptions != 0;
558 check->leaks = result.leaks;
559 check->has_leaks = result.leaks != 0;
560 check->corruptions_fixed = result.corruptions_fixed;
561 check->has_corruptions_fixed = result.corruptions != 0;
562 check->leaks_fixed = result.leaks_fixed;
563 check->has_leaks_fixed = result.leaks != 0;
564 check->image_end_offset = result.image_end_offset;
565 check->has_image_end_offset = result.image_end_offset != 0;
566 check->total_clusters = result.bfi.total_clusters;
567 check->has_total_clusters = result.bfi.total_clusters != 0;
568 check->allocated_clusters = result.bfi.allocated_clusters;
569 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
570 check->fragmented_clusters = result.bfi.fragmented_clusters;
571 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100572 check->compressed_clusters = result.bfi.compressed_clusters;
573 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500574
575 return 0;
576}
577
Kevin Wolfe076f332010-06-29 11:43:13 +0200578/*
579 * Checks an image for consistency. Exit codes:
580 *
Max Reitzd6635c42014-06-02 22:15:21 +0200581 * 0 - Check completed, image is good
582 * 1 - Check not completed because of internal errors
583 * 2 - Check completed, image is corrupted
584 * 3 - Check completed, image has leaked clusters, but is good otherwise
585 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200586 */
aliguori15859692009-04-21 23:11:53 +0000587static int img_check(int argc, char **argv)
588{
589 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500590 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200591 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200592 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000593 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200594 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100595 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200596 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100597 bool quiet = false;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000598 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000599 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000600
601 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500602 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200603 cache = BDRV_DEFAULT_CACHE;
aliguori15859692009-04-21 23:11:53 +0000604 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500605 int option_index = 0;
606 static const struct option long_options[] = {
607 {"help", no_argument, 0, 'h'},
608 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530609 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500610 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000611 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000612 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500613 {0, 0, 0, 0}
614 };
Max Reitz40055952014-07-22 22:58:42 +0200615 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500616 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100617 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000618 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100619 }
aliguori15859692009-04-21 23:11:53 +0000620 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100621 case '?':
aliguori15859692009-04-21 23:11:53 +0000622 case 'h':
623 help();
624 break;
625 case 'f':
626 fmt = optarg;
627 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200628 case 'r':
629 flags |= BDRV_O_RDWR;
630
631 if (!strcmp(optarg, "leaks")) {
632 fix = BDRV_FIX_LEAKS;
633 } else if (!strcmp(optarg, "all")) {
634 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
635 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800636 error_exit("Unknown option value for -r "
637 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200638 }
639 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500640 case OPTION_OUTPUT:
641 output = optarg;
642 break;
Max Reitz40055952014-07-22 22:58:42 +0200643 case 'T':
644 cache = optarg;
645 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100646 case 'q':
647 quiet = true;
648 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000649 case OPTION_OBJECT: {
650 QemuOpts *opts;
651 opts = qemu_opts_parse_noisily(&qemu_object_opts,
652 optarg, true);
653 if (!opts) {
654 return 1;
655 }
656 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000657 case OPTION_IMAGE_OPTS:
658 image_opts = true;
659 break;
aliguori15859692009-04-21 23:11:53 +0000660 }
661 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200662 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800663 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100664 }
aliguori15859692009-04-21 23:11:53 +0000665 filename = argv[optind++];
666
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500667 if (output && !strcmp(output, "json")) {
668 output_format = OFORMAT_JSON;
669 } else if (output && !strcmp(output, "human")) {
670 output_format = OFORMAT_HUMAN;
671 } else if (output) {
672 error_report("--output must be used with human or json as argument.");
673 return 1;
674 }
675
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000676 if (qemu_opts_foreach(&qemu_object_opts,
677 user_creatable_add_opts_foreach,
678 NULL, &local_err)) {
679 error_report_err(local_err);
680 return 1;
681 }
682
Max Reitz40055952014-07-22 22:58:42 +0200683 ret = bdrv_parse_cache_flags(cache, &flags);
684 if (ret < 0) {
685 error_report("Invalid source cache option: %s", cache);
686 return 1;
687 }
688
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000689 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200690 if (!blk) {
691 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900692 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200693 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500694
695 check = g_new0(ImageCheck, 1);
696 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200697
698 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200699 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200700 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500701 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200702 }
703
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500704 if (check->corruptions_fixed || check->leaks_fixed) {
705 int corruptions_fixed, leaks_fixed;
706
707 leaks_fixed = check->leaks_fixed;
708 corruptions_fixed = check->corruptions_fixed;
709
710 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100711 qprintf(quiet,
712 "The following inconsistencies were found and repaired:\n\n"
713 " %" PRId64 " leaked clusters\n"
714 " %" PRId64 " corruptions\n\n"
715 "Double checking the fixed image now...\n",
716 check->leaks_fixed,
717 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500718 }
719
720 ret = collect_image_check(bs, check, filename, fmt, 0);
721
722 check->leaks_fixed = leaks_fixed;
723 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200724 }
725
Max Reitz832390a2014-10-23 15:29:12 +0200726 if (!ret) {
727 switch (output_format) {
728 case OFORMAT_HUMAN:
729 dump_human_image_check(check, quiet);
730 break;
731 case OFORMAT_JSON:
732 dump_json_image_check(check, quiet);
733 break;
734 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500735 }
736
737 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200738 if (ret) {
739 error_report("Check failed: %s", strerror(-ret));
740 } else {
741 error_report("Check failed");
742 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500743 ret = 1;
744 goto fail;
745 }
746
747 if (check->corruptions) {
748 ret = 2;
749 } else if (check->leaks) {
750 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200751 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500752 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000753 }
754
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500755fail:
756 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200757 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500758 return ret;
aliguori15859692009-04-21 23:11:53 +0000759}
760
Max Reitzd4a32382014-10-24 15:57:37 +0200761typedef struct CommonBlockJobCBInfo {
762 BlockDriverState *bs;
763 Error **errp;
764} CommonBlockJobCBInfo;
765
766static void common_block_job_cb(void *opaque, int ret)
767{
768 CommonBlockJobCBInfo *cbi = opaque;
769
770 if (ret < 0) {
771 error_setg_errno(cbi->errp, -ret, "Block job failed");
772 }
Max Reitzd4a32382014-10-24 15:57:37 +0200773}
774
775static void run_block_job(BlockJob *job, Error **errp)
776{
777 AioContext *aio_context = bdrv_get_aio_context(job->bs);
778
779 do {
780 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500781 qemu_progress_print(job->len ?
782 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200783 } while (!job->ready);
784
785 block_job_complete_sync(job, errp);
Max Reitz687fa1d2014-10-24 15:57:39 +0200786
787 /* A block job may finish instantaneously without publishing any progress,
788 * so just signal completion here */
789 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200790}
791
bellardea2384d2004-08-01 21:59:26 +0000792static int img_commit(int argc, char **argv)
793{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400794 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200795 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200796 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200797 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200798 bool progress = false, quiet = false, drop = false;
Max Reitzd4a32382014-10-24 15:57:37 +0200799 Error *local_err = NULL;
800 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000801 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +0000802
803 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400804 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200805 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000806 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000807 static const struct option long_options[] = {
808 {"help", no_argument, 0, 'h'},
809 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000810 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000811 {0, 0, 0, 0}
812 };
813 c = getopt_long(argc, argv, "f:ht:b:dpq",
814 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100815 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000816 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100817 }
bellardea2384d2004-08-01 21:59:26 +0000818 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100819 case '?':
bellardea2384d2004-08-01 21:59:26 +0000820 case 'h':
821 help();
822 break;
823 case 'f':
824 fmt = optarg;
825 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400826 case 't':
827 cache = optarg;
828 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200829 case 'b':
830 base = optarg;
831 /* -b implies -d */
832 drop = true;
833 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200834 case 'd':
835 drop = true;
836 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200837 case 'p':
838 progress = true;
839 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100840 case 'q':
841 quiet = true;
842 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000843 case OPTION_OBJECT: {
844 QemuOpts *opts;
845 opts = qemu_opts_parse_noisily(&qemu_object_opts,
846 optarg, true);
847 if (!opts) {
848 return 1;
849 }
850 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000851 case OPTION_IMAGE_OPTS:
852 image_opts = true;
853 break;
bellardea2384d2004-08-01 21:59:26 +0000854 }
855 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200856
857 /* Progress is not shown in Quiet mode */
858 if (quiet) {
859 progress = false;
860 }
861
Kevin Wolffc11eb22013-08-05 10:53:04 +0200862 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800863 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100864 }
bellardea2384d2004-08-01 21:59:26 +0000865 filename = argv[optind++];
866
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000867 if (qemu_opts_foreach(&qemu_object_opts,
868 user_creatable_add_opts_foreach,
869 NULL, &local_err)) {
870 error_report_err(local_err);
871 return 1;
872 }
873
Max Reitz9a86fe42014-10-24 15:57:38 +0200874 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100875 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400876 if (ret < 0) {
877 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100878 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400879 }
880
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000881 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200882 if (!blk) {
883 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900884 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200885 bs = blk_bs(blk);
886
Max Reitz687fa1d2014-10-24 15:57:39 +0200887 qemu_progress_init(progress, 1.f);
888 qemu_progress_print(0.f, 100);
889
Max Reitz1b22bff2014-10-24 15:57:40 +0200890 if (base) {
891 base_bs = bdrv_find_backing_image(bs, base);
892 if (!base_bs) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100893 error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
Max Reitz1b22bff2014-10-24 15:57:40 +0200894 goto done;
895 }
896 } else {
897 /* This is different from QMP, which by default uses the deepest file in
898 * the backing chain (i.e., the very base); however, the traditional
899 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200900 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200901 if (!base_bs) {
902 error_setg(&local_err, "Image does not have a backing file");
903 goto done;
904 }
bellardea2384d2004-08-01 21:59:26 +0000905 }
906
Max Reitzd4a32382014-10-24 15:57:37 +0200907 cbi = (CommonBlockJobCBInfo){
908 .errp = &local_err,
909 .bs = bs,
910 };
911
912 commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
913 common_block_job_cb, &cbi, &local_err);
914 if (local_err) {
915 goto done;
916 }
917
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200918 /* When the block job completes, the BlockBackend reference will point to
919 * the old backing file. In order to avoid that the top image is already
920 * deleted, so we can still empty it afterwards, increment the reference
921 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200922 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200923 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200924 }
925
Max Reitzd4a32382014-10-24 15:57:37 +0200926 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200927 if (local_err) {
928 goto unref_backing;
929 }
930
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200931 if (!drop && bs->drv->bdrv_make_empty) {
932 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200933 if (ret) {
934 error_setg_errno(&local_err, -ret, "Could not empty %s",
935 filename);
936 goto unref_backing;
937 }
938 }
939
940unref_backing:
941 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200942 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200943 }
Max Reitzd4a32382014-10-24 15:57:37 +0200944
945done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200946 qemu_progress_end();
947
Markus Armbruster26f54e92014-10-07 13:59:04 +0200948 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200949
950 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100951 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900952 return 1;
953 }
Max Reitzd4a32382014-10-24 15:57:37 +0200954
955 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000956 return 0;
957}
958
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400959/*
thsf58c7b32008-06-05 21:53:49 +0000960 * Returns true iff the first sector pointed to by 'buf' contains at least
961 * a non-NUL byte.
962 *
963 * 'pnum' is set to the number of sectors (including and immediately following
964 * the first one) that are known to be in the same allocated/unallocated state.
965 */
bellardea2384d2004-08-01 21:59:26 +0000966static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
967{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000968 bool is_zero;
969 int i;
bellardea2384d2004-08-01 21:59:26 +0000970
971 if (n <= 0) {
972 *pnum = 0;
973 return 0;
974 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000975 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000976 for(i = 1; i < n; i++) {
977 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000978 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000979 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000980 }
bellardea2384d2004-08-01 21:59:26 +0000981 }
982 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000983 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000984}
985
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100986/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200987 * Like is_allocated_sectors, but if the buffer starts with a used sector,
988 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
989 * breaking up write requests for only small sparse areas.
990 */
991static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
992 int min)
993{
994 int ret;
995 int num_checked, num_used;
996
997 if (n < min) {
998 min = n;
999 }
1000
1001 ret = is_allocated_sectors(buf, n, pnum);
1002 if (!ret) {
1003 return ret;
1004 }
1005
1006 num_used = *pnum;
1007 buf += BDRV_SECTOR_SIZE * *pnum;
1008 n -= *pnum;
1009 num_checked = num_used;
1010
1011 while (n > 0) {
1012 ret = is_allocated_sectors(buf, n, pnum);
1013
1014 buf += BDRV_SECTOR_SIZE * *pnum;
1015 n -= *pnum;
1016 num_checked += *pnum;
1017 if (ret) {
1018 num_used = num_checked;
1019 } else if (*pnum >= min) {
1020 break;
1021 }
1022 }
1023
1024 *pnum = num_used;
1025 return 1;
1026}
1027
1028/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001029 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1030 * buffers matches, non-zero otherwise.
1031 *
1032 * pnum is set to the number of sectors (including and immediately following
1033 * the first one) that are known to have the same comparison result
1034 */
1035static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1036 int *pnum)
1037{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001038 bool res;
1039 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001040
1041 if (n <= 0) {
1042 *pnum = 0;
1043 return 0;
1044 }
1045
1046 res = !!memcmp(buf1, buf2, 512);
1047 for(i = 1; i < n; i++) {
1048 buf1 += 512;
1049 buf2 += 512;
1050
1051 if (!!memcmp(buf1, buf2, 512) != res) {
1052 break;
1053 }
1054 }
1055
1056 *pnum = i;
1057 return res;
1058}
1059
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001060#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001061
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001062static int64_t sectors_to_bytes(int64_t sectors)
1063{
1064 return sectors << BDRV_SECTOR_BITS;
1065}
1066
1067static int64_t sectors_to_process(int64_t total, int64_t from)
1068{
1069 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1070}
1071
1072/*
1073 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1074 *
1075 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1076 * data and negative value on error.
1077 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001078 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001079 * @param sect_num: Number of first sector to check
1080 * @param sect_count: Number of sectors to check
1081 * @param filename: Name of disk file we are checking (logging purpose)
1082 * @param buffer: Allocated buffer for storing read data
1083 * @param quiet: Flag for quiet mode
1084 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001085static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001086 int sect_count, const char *filename,
1087 uint8_t *buffer, bool quiet)
1088{
1089 int pnum, ret = 0;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001090 ret = blk_read(blk, sect_num, buffer, sect_count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001091 if (ret < 0) {
1092 error_report("Error while reading offset %" PRId64 " of %s: %s",
1093 sectors_to_bytes(sect_num), filename, strerror(-ret));
1094 return ret;
1095 }
1096 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1097 if (ret || pnum != sect_count) {
1098 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1099 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1100 return 1;
1101 }
1102
1103 return 0;
1104}
1105
1106/*
1107 * Compares two images. Exit codes:
1108 *
1109 * 0 - Images are identical
1110 * 1 - Images differ
1111 * >1 - Error occurred
1112 */
1113static int img_compare(int argc, char **argv)
1114{
Max Reitz40055952014-07-22 22:58:42 +02001115 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001116 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001117 BlockDriverState *bs1, *bs2;
1118 int64_t total_sectors1, total_sectors2;
1119 uint8_t *buf1 = NULL, *buf2 = NULL;
1120 int pnum1, pnum2;
1121 int allocated1, allocated2;
1122 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1123 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001124 int flags;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001125 int64_t total_sectors;
1126 int64_t sector_num = 0;
1127 int64_t nb_sectors;
1128 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001129 uint64_t progress_base;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001130 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001131 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001132
Max Reitz40055952014-07-22 22:58:42 +02001133 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001134 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001135 static const struct option long_options[] = {
1136 {"help", no_argument, 0, 'h'},
1137 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001138 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001139 {0, 0, 0, 0}
1140 };
1141 c = getopt_long(argc, argv, "hf:F:T:pqs",
1142 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001143 if (c == -1) {
1144 break;
1145 }
1146 switch (c) {
1147 case '?':
1148 case 'h':
1149 help();
1150 break;
1151 case 'f':
1152 fmt1 = optarg;
1153 break;
1154 case 'F':
1155 fmt2 = optarg;
1156 break;
Max Reitz40055952014-07-22 22:58:42 +02001157 case 'T':
1158 cache = optarg;
1159 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001160 case 'p':
1161 progress = true;
1162 break;
1163 case 'q':
1164 quiet = true;
1165 break;
1166 case 's':
1167 strict = true;
1168 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001169 case OPTION_OBJECT: {
1170 QemuOpts *opts;
1171 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1172 optarg, true);
1173 if (!opts) {
1174 ret = 2;
1175 goto out4;
1176 }
1177 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001178 case OPTION_IMAGE_OPTS:
1179 image_opts = true;
1180 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001181 }
1182 }
1183
1184 /* Progress is not shown in Quiet mode */
1185 if (quiet) {
1186 progress = false;
1187 }
1188
1189
Kevin Wolffc11eb22013-08-05 10:53:04 +02001190 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001191 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001192 }
1193 filename1 = argv[optind++];
1194 filename2 = argv[optind++];
1195
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001196 if (qemu_opts_foreach(&qemu_object_opts,
1197 user_creatable_add_opts_foreach,
1198 NULL, &local_err)) {
1199 error_report_err(local_err);
1200 ret = 2;
1201 goto out4;
1202 }
1203
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001204 /* Initialize before goto out */
1205 qemu_progress_init(progress, 2.0);
1206
Max Reitz40055952014-07-22 22:58:42 +02001207 flags = BDRV_O_FLAGS;
1208 ret = bdrv_parse_cache_flags(cache, &flags);
1209 if (ret < 0) {
1210 error_report("Invalid source cache option: %s", cache);
1211 ret = 2;
1212 goto out3;
1213 }
1214
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00001215 blk1 = img_open(image_opts, filename1, fmt1, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001216 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001217 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001218 goto out3;
1219 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001220
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00001221 blk2 = img_open(image_opts, filename2, fmt2, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001222 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001223 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001224 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001226 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001227 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001228
Max Reitzf1d3cd72015-02-05 13:58:18 -05001229 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1230 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1231 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001232 if (total_sectors1 < 0) {
1233 error_report("Can't get size of %s: %s",
1234 filename1, strerror(-total_sectors1));
1235 ret = 4;
1236 goto out;
1237 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001238 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001239 if (total_sectors2 < 0) {
1240 error_report("Can't get size of %s: %s",
1241 filename2, strerror(-total_sectors2));
1242 ret = 4;
1243 goto out;
1244 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001245 total_sectors = MIN(total_sectors1, total_sectors2);
1246 progress_base = MAX(total_sectors1, total_sectors2);
1247
1248 qemu_progress_print(0, 100);
1249
1250 if (strict && total_sectors1 != total_sectors2) {
1251 ret = 1;
1252 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1253 goto out;
1254 }
1255
1256 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001257 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001258 BlockDriverState *file;
1259
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001260 nb_sectors = sectors_to_process(total_sectors, sector_num);
1261 if (nb_sectors <= 0) {
1262 break;
1263 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001264 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1265 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001266 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001267 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001268 ret = 3;
1269 error_report("Sector allocation test failed for %s", filename1);
1270 goto out;
1271 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001272 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001273
Fam Zheng25ad8e62016-01-13 16:37:41 +08001274 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1275 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001276 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001277 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001278 ret = 3;
1279 error_report("Sector allocation test failed for %s", filename2);
1280 goto out;
1281 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001282 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1283 if (pnum1) {
1284 nb_sectors = MIN(nb_sectors, pnum1);
1285 }
1286 if (pnum2) {
1287 nb_sectors = MIN(nb_sectors, pnum2);
1288 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001289
Fam Zheng25ad8e62016-01-13 16:37:41 +08001290 if (strict) {
1291 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1292 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1293 ret = 1;
1294 qprintf(quiet, "Strict mode: Offset %" PRId64
1295 " block status mismatch!\n",
1296 sectors_to_bytes(sector_num));
1297 goto out;
1298 }
1299 }
1300 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1301 nb_sectors = MIN(pnum1, pnum2);
1302 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001303 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001304 ret = blk_read(blk1, sector_num, buf1, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001305 if (ret < 0) {
1306 error_report("Error while reading offset %" PRId64 " of %s:"
1307 " %s", sectors_to_bytes(sector_num), filename1,
1308 strerror(-ret));
1309 ret = 4;
1310 goto out;
1311 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001312 ret = blk_read(blk2, sector_num, buf2, nb_sectors);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001313 if (ret < 0) {
1314 error_report("Error while reading offset %" PRId64
1315 " of %s: %s", sectors_to_bytes(sector_num),
1316 filename2, strerror(-ret));
1317 ret = 4;
1318 goto out;
1319 }
1320 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1321 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001322 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1323 sectors_to_bytes(
1324 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001325 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001326 goto out;
1327 }
1328 }
1329 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001330
1331 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001332 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001333 filename1, buf1, quiet);
1334 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001335 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336 filename2, buf1, quiet);
1337 }
1338 if (ret) {
1339 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001340 error_report("Error while reading offset %" PRId64 ": %s",
1341 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001342 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001343 }
1344 goto out;
1345 }
1346 }
1347 sector_num += nb_sectors;
1348 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1349 }
1350
1351 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001352 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001353 int64_t total_sectors_over;
1354 const char *filename_over;
1355
1356 qprintf(quiet, "Warning: Image size mismatch!\n");
1357 if (total_sectors1 > total_sectors2) {
1358 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001359 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001360 filename_over = filename1;
1361 } else {
1362 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001363 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001364 filename_over = filename2;
1365 }
1366
1367 for (;;) {
1368 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1369 if (nb_sectors <= 0) {
1370 break;
1371 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001372 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001373 nb_sectors, &pnum);
1374 if (ret < 0) {
1375 ret = 3;
1376 error_report("Sector allocation test failed for %s",
1377 filename_over);
1378 goto out;
1379
1380 }
1381 nb_sectors = pnum;
1382 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001383 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384 filename_over, buf1, quiet);
1385 if (ret) {
1386 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001387 error_report("Error while reading offset %" PRId64
1388 " of %s: %s", sectors_to_bytes(sector_num),
1389 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001390 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001391 }
1392 goto out;
1393 }
1394 }
1395 sector_num += nb_sectors;
1396 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1397 }
1398 }
1399
1400 qprintf(quiet, "Images are identical.\n");
1401 ret = 0;
1402
1403out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001404 qemu_vfree(buf1);
1405 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001406 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001407out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001408 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409out3:
1410 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001411out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001412 return ret;
1413}
1414
Kevin Wolf690c7302015-03-19 13:33:32 +01001415enum ImgConvertBlockStatus {
1416 BLK_DATA,
1417 BLK_ZERO,
1418 BLK_BACKING_FILE,
1419};
1420
1421typedef struct ImgConvertState {
1422 BlockBackend **src;
1423 int64_t *src_sectors;
1424 int src_cur, src_num;
1425 int64_t src_cur_offset;
1426 int64_t total_sectors;
1427 int64_t allocated_sectors;
1428 enum ImgConvertBlockStatus status;
1429 int64_t sector_next_status;
1430 BlockBackend *target;
1431 bool has_zero_init;
1432 bool compressed;
1433 bool target_has_backing;
1434 int min_sparse;
1435 size_t cluster_sectors;
1436 size_t buf_sectors;
1437} ImgConvertState;
1438
1439static void convert_select_part(ImgConvertState *s, int64_t sector_num)
1440{
1441 assert(sector_num >= s->src_cur_offset);
1442 while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) {
1443 s->src_cur_offset += s->src_sectors[s->src_cur];
1444 s->src_cur++;
1445 assert(s->src_cur < s->src_num);
1446 }
1447}
1448
1449static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1450{
1451 int64_t ret;
1452 int n;
1453
1454 convert_select_part(s, sector_num);
1455
1456 assert(s->total_sectors > sector_num);
1457 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1458
1459 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001460 BlockDriverState *file;
Kevin Wolf690c7302015-03-19 13:33:32 +01001461 ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),
1462 sector_num - s->src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001463 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001464 if (ret < 0) {
1465 return ret;
1466 }
1467
1468 if (ret & BDRV_BLOCK_ZERO) {
1469 s->status = BLK_ZERO;
1470 } else if (ret & BDRV_BLOCK_DATA) {
1471 s->status = BLK_DATA;
1472 } else if (!s->target_has_backing) {
1473 /* Without a target backing file we must copy over the contents of
1474 * the backing file as well. */
1475 /* TODO Check block status of the backing file chain to avoid
1476 * needlessly reading zeroes and limiting the iteration to the
1477 * buffer size */
1478 s->status = BLK_DATA;
1479 } else {
1480 s->status = BLK_BACKING_FILE;
1481 }
1482
1483 s->sector_next_status = sector_num + n;
1484 }
1485
1486 n = MIN(n, s->sector_next_status - sector_num);
1487 if (s->status == BLK_DATA) {
1488 n = MIN(n, s->buf_sectors);
1489 }
1490
1491 /* We need to write complete clusters for compressed images, so if an
1492 * unallocated area is shorter than that, we must consider the whole
1493 * cluster allocated. */
1494 if (s->compressed) {
1495 if (n < s->cluster_sectors) {
1496 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1497 s->status = BLK_DATA;
1498 } else {
1499 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1500 }
1501 }
1502
1503 return n;
1504}
1505
1506static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1507 uint8_t *buf)
1508{
1509 int n;
1510 int ret;
1511
1512 if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) {
1513 return 0;
1514 }
1515
1516 assert(nb_sectors <= s->buf_sectors);
1517 while (nb_sectors > 0) {
1518 BlockBackend *blk;
1519 int64_t bs_sectors;
1520
1521 /* In the case of compression with multiple source files, we can get a
1522 * nb_sectors that spreads into the next part. So we must be able to
1523 * read across multiple BDSes for one convert_read() call. */
1524 convert_select_part(s, sector_num);
1525 blk = s->src[s->src_cur];
1526 bs_sectors = s->src_sectors[s->src_cur];
1527
1528 n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset));
1529 ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n);
1530 if (ret < 0) {
1531 return ret;
1532 }
1533
1534 sector_num += n;
1535 nb_sectors -= n;
1536 buf += n * BDRV_SECTOR_SIZE;
1537 }
1538
1539 return 0;
1540}
1541
1542static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors,
1543 const uint8_t *buf)
1544{
1545 int ret;
1546
1547 while (nb_sectors > 0) {
1548 int n = nb_sectors;
1549
1550 switch (s->status) {
1551 case BLK_BACKING_FILE:
1552 /* If we have a backing file, leave clusters unallocated that are
1553 * unallocated in the source image, so that the backing file is
1554 * visible at the respective offset. */
1555 assert(s->target_has_backing);
1556 break;
1557
1558 case BLK_DATA:
1559 /* We must always write compressed clusters as a whole, so don't
1560 * try to find zeroed parts in the buffer. We can only save the
1561 * write if the buffer is completely zeroed and we're allowed to
1562 * keep the target sparse. */
1563 if (s->compressed) {
1564 if (s->has_zero_init && s->min_sparse &&
1565 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1566 {
1567 assert(!s->target_has_backing);
1568 break;
1569 }
1570
1571 ret = blk_write_compressed(s->target, sector_num, buf, n);
1572 if (ret < 0) {
1573 return ret;
1574 }
1575 break;
1576 }
1577
1578 /* If there is real non-zero data or we're told to keep the target
1579 * fully allocated (-S 0), we must write it. Otherwise we can treat
1580 * it as zero sectors. */
1581 if (!s->min_sparse ||
1582 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1583 {
1584 ret = blk_write(s->target, sector_num, buf, n);
1585 if (ret < 0) {
1586 return ret;
1587 }
1588 break;
1589 }
1590 /* fall-through */
1591
1592 case BLK_ZERO:
1593 if (s->has_zero_init) {
1594 break;
1595 }
1596 ret = blk_write_zeroes(s->target, sector_num, n, 0);
1597 if (ret < 0) {
1598 return ret;
1599 }
1600 break;
1601 }
1602
1603 sector_num += n;
1604 nb_sectors -= n;
1605 buf += n * BDRV_SECTOR_SIZE;
1606 }
1607
1608 return 0;
1609}
1610
1611static int convert_do_copy(ImgConvertState *s)
1612{
1613 uint8_t *buf = NULL;
1614 int64_t sector_num, allocated_done;
1615 int ret;
1616 int n;
1617
1618 /* Check whether we have zero initialisation or can get it efficiently */
1619 s->has_zero_init = s->min_sparse && !s->target_has_backing
1620 ? bdrv_has_zero_init(blk_bs(s->target))
1621 : false;
1622
1623 if (!s->has_zero_init && !s->target_has_backing &&
1624 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1625 {
1626 ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);
1627 if (ret == 0) {
1628 s->has_zero_init = true;
1629 }
1630 }
1631
1632 /* Allocate buffer for copied data. For compressed images, only one cluster
1633 * can be copied at a time. */
1634 if (s->compressed) {
1635 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1636 error_report("invalid cluster size");
1637 ret = -EINVAL;
1638 goto fail;
1639 }
1640 s->buf_sectors = s->cluster_sectors;
1641 }
1642 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1643
1644 /* Calculate allocated sectors for progress */
1645 s->allocated_sectors = 0;
1646 sector_num = 0;
1647 while (sector_num < s->total_sectors) {
1648 n = convert_iteration_sectors(s, sector_num);
1649 if (n < 0) {
1650 ret = n;
1651 goto fail;
1652 }
1653 if (s->status == BLK_DATA) {
1654 s->allocated_sectors += n;
1655 }
1656 sector_num += n;
1657 }
1658
1659 /* Do the copy */
1660 s->src_cur = 0;
1661 s->src_cur_offset = 0;
1662 s->sector_next_status = 0;
1663
1664 sector_num = 0;
1665 allocated_done = 0;
1666
1667 while (sector_num < s->total_sectors) {
1668 n = convert_iteration_sectors(s, sector_num);
1669 if (n < 0) {
1670 ret = n;
1671 goto fail;
1672 }
1673 if (s->status == BLK_DATA) {
1674 allocated_done += n;
1675 qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,
1676 0);
1677 }
1678
1679 ret = convert_read(s, sector_num, n, buf);
1680 if (ret < 0) {
1681 error_report("error while reading sector %" PRId64
1682 ": %s", sector_num, strerror(-ret));
1683 goto fail;
1684 }
1685
1686 ret = convert_write(s, sector_num, n, buf);
1687 if (ret < 0) {
1688 error_report("error while writing sector %" PRId64
1689 ": %s", sector_num, strerror(-ret));
1690 goto fail;
1691 }
1692
1693 sector_num += n;
1694 }
1695
1696 if (s->compressed) {
1697 /* signal EOF to align */
1698 ret = blk_write_compressed(s->target, 0, NULL, 0);
1699 if (ret < 0) {
1700 goto fail;
1701 }
1702 }
1703
1704 ret = 0;
1705fail:
1706 qemu_vfree(buf);
1707 return ret;
1708}
1709
bellardea2384d2004-08-01 21:59:26 +00001710static int img_convert(int argc, char **argv)
1711{
Kevin Wolf690c7302015-03-19 13:33:32 +01001712 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001713 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001714 int progress = 0, flags, src_flags;
1715 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001716 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001717 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001718 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001719 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001720 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001721 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001722 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001723 QemuOpts *opts = NULL;
1724 QemuOptsList *create_opts = NULL;
1725 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001726 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001727 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001728 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001729 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001730 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001731 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001732 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001733 bool image_opts = false;
bellardea2384d2004-08-01 21:59:26 +00001734
1735 fmt = NULL;
1736 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001737 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001738 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001739 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001740 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001741 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001742 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001743 static const struct option long_options[] = {
1744 {"help", no_argument, 0, 'h'},
1745 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001746 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001747 {0, 0, 0, 0}
1748 };
1749 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn",
1750 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001751 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001752 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001753 }
bellardea2384d2004-08-01 21:59:26 +00001754 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001755 case '?':
bellardea2384d2004-08-01 21:59:26 +00001756 case 'h':
1757 help();
1758 break;
1759 case 'f':
1760 fmt = optarg;
1761 break;
1762 case 'O':
1763 out_fmt = optarg;
1764 break;
thsf58c7b32008-06-05 21:53:49 +00001765 case 'B':
1766 out_baseimg = optarg;
1767 break;
bellardea2384d2004-08-01 21:59:26 +00001768 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001769 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001770 break;
1771 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001772 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001773 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001774 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001775 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001776 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001777 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001778 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001779 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001780 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001781 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001782 if (!is_valid_option_list(optarg)) {
1783 error_report("Invalid option list: %s", optarg);
1784 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001785 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001786 }
1787 if (!options) {
1788 options = g_strdup(optarg);
1789 } else {
1790 char *old_options = options;
1791 options = g_strdup_printf("%s,%s", options, optarg);
1792 g_free(old_options);
1793 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001794 break;
edison51ef6722010-09-21 19:58:41 -07001795 case 's':
1796 snapshot_name = optarg;
1797 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001798 case 'l':
1799 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001800 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1801 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001802 if (!sn_opts) {
1803 error_report("Failed in parsing snapshot param '%s'",
1804 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001805 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001806 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001807 }
1808 } else {
1809 snapshot_name = optarg;
1810 }
1811 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001812 case 'S':
1813 {
1814 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001815 char *end;
Marc-André Lureau4677bb42015-09-16 18:02:56 +02001816 sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
Markus Armbrustere36b3692011-11-22 09:46:05 +01001817 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001818 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001819 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001820 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001821 }
1822
1823 min_sparse = sval / BDRV_SECTOR_SIZE;
1824 break;
1825 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001826 case 'p':
1827 progress = 1;
1828 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001829 case 't':
1830 cache = optarg;
1831 break;
Max Reitz40055952014-07-22 22:58:42 +02001832 case 'T':
1833 src_cache = optarg;
1834 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001835 case 'q':
1836 quiet = true;
1837 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001838 case 'n':
1839 skip_create = 1;
1840 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001841 case OPTION_OBJECT:
1842 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1843 optarg, true);
1844 if (!opts) {
1845 goto fail_getopt;
1846 }
1847 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001848 case OPTION_IMAGE_OPTS:
1849 image_opts = true;
1850 break;
bellardea2384d2004-08-01 21:59:26 +00001851 }
1852 }
ths3b46e622007-09-17 08:09:54 +00001853
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001854 if (qemu_opts_foreach(&qemu_object_opts,
1855 user_creatable_add_opts_foreach,
1856 NULL, &local_err)) {
1857 error_report_err(local_err);
1858 goto fail_getopt;
1859 }
1860
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001861 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001862 if (quiet) {
1863 progress = 0;
1864 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001865 qemu_progress_init(progress, 1.0);
1866
balrog926c2d22007-10-31 01:11:44 +00001867 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001868 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001869
Kevin Wolf2dc83282014-02-21 16:24:05 +01001870 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001871 ret = print_block_option_help(out_filename, out_fmt);
1872 goto out;
1873 }
1874
bellardea2384d2004-08-01 21:59:26 +00001875 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001876 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001877 }
bellardea2384d2004-08-01 21:59:26 +00001878
thsf58c7b32008-06-05 21:53:49 +00001879
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001880 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001881 error_report("-B makes no sense when concatenating multiple input "
1882 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001883 ret = -1;
1884 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001885 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001886
Max Reitz40055952014-07-22 22:58:42 +02001887 src_flags = BDRV_O_FLAGS;
1888 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
1889 if (ret < 0) {
1890 error_report("Invalid source cache option: %s", src_cache);
1891 goto out;
1892 }
1893
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001894 qemu_progress_print(0, 100);
1895
Markus Armbruster26f54e92014-10-07 13:59:04 +02001896 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001897 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001898 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001899
1900 total_sectors = 0;
1901 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01001902 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00001903 fmt, src_flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001904 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001905 ret = -1;
1906 goto out;
1907 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001908 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001909 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001910 if (bs_sectors[bs_i] < 0) {
1911 error_report("Could not get size of %s: %s",
1912 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1913 ret = -1;
1914 goto out;
1915 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001916 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001917 }
bellardea2384d2004-08-01 21:59:26 +00001918
Wenchao Xiaef806542013-12-04 17:10:57 +08001919 if (sn_opts) {
1920 ret = bdrv_snapshot_load_tmp(bs[0],
1921 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1922 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1923 &local_err);
1924 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001925 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001926 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001927 ret = -1;
1928 goto out;
1929 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001930
1931 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001932 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001933 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001934 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08001935 ret = -1;
1936 goto out;
edison51ef6722010-09-21 19:58:41 -07001937 }
1938
Kevin Wolfefa84d42009-05-18 16:42:12 +02001939 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001940 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001941 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001942 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001943 ret = -1;
1944 goto out;
1945 }
balrog926c2d22007-10-31 01:11:44 +00001946
Max Reitzb65a5e12015-02-05 13:58:12 -05001947 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001948 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01001949 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001950 ret = -1;
1951 goto out;
1952 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001953
Max Reitz2e024cd2015-02-11 09:58:46 -05001954 if (!skip_create) {
1955 if (!drv->create_opts) {
1956 error_report("Format driver '%s' does not support image creation",
1957 drv->format_name);
1958 ret = -1;
1959 goto out;
1960 }
Max Reitzf75613c2014-12-02 18:32:46 +01001961
Max Reitz2e024cd2015-02-11 09:58:46 -05001962 if (!proto_drv->create_opts) {
1963 error_report("Protocol driver '%s' does not support image creation",
1964 proto_drv->format_name);
1965 ret = -1;
1966 goto out;
1967 }
Max Reitzf75613c2014-12-02 18:32:46 +01001968
Max Reitz2e024cd2015-02-11 09:58:46 -05001969 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1970 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001971
Max Reitz2e024cd2015-02-11 09:58:46 -05001972 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001973 if (options) {
1974 qemu_opts_do_parse(opts, options, NULL, &local_err);
1975 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01001976 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01001977 ret = -1;
1978 goto out;
1979 }
Max Reitz2e024cd2015-02-11 09:58:46 -05001980 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001981
Markus Armbruster39101f22015-02-12 16:46:36 +01001982 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
1983 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05001984 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
1985 if (ret < 0) {
1986 goto out;
1987 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001988 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001989
Kevin Wolfa18953f2010-10-14 15:46:04 +02001990 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001991 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001992 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001993 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001994 }
1995
Kevin Wolfefa84d42009-05-18 16:42:12 +02001996 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001997 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001998 bool encryption =
1999 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2000 const char *preallocation =
2001 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002002
2003 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002004 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002005 ret = -1;
2006 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002007 }
2008
Chunyan Liu83d05212014-06-05 17:20:51 +08002009 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002010 error_report("Compression and encryption not supported at "
2011 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002012 ret = -1;
2013 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002014 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002015
Chunyan Liu83d05212014-06-05 17:20:51 +08002016 if (preallocation
2017 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002018 {
2019 error_report("Compression and preallocation not supported at "
2020 "the same time");
2021 ret = -1;
2022 goto out;
2023 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002024 }
2025
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002026 if (!skip_create) {
2027 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002028 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002029 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002030 error_reportf_err(local_err, "%s: error while converting %s: ",
2031 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002032 goto out;
bellardea2384d2004-08-01 21:59:26 +00002033 }
2034 }
ths3b46e622007-09-17 08:09:54 +00002035
Peter Lieven5a37b602013-10-24 12:07:06 +02002036 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002037 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002038 if (ret < 0) {
2039 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002040 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002041 }
2042
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002043 /* XXX we should allow --image-opts to trigger use of
2044 * img_open() here, but then we have trouble with
2045 * the bdrv_create() call which takes different params.
2046 * Not critical right now, so fix can wait...
2047 */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002048 out_blk = img_open_file(out_filename, out_fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002049 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002050 ret = -1;
2051 goto out;
2052 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002053 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002054
Peter Lievenf2521c92013-11-27 11:07:06 +01002055 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
2056 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2057 * as maximum. */
2058 bufsectors = MIN(32768,
2059 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
2060 out_bs->bl.discard_alignment))
2061 );
2062
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002063 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002064 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002065 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002066 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002067 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002068 ret = -1;
2069 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002070 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002071 error_report("output file is smaller than input file");
2072 ret = -1;
2073 goto out;
2074 }
2075 }
2076
Peter Lieven24f833c2013-11-27 11:07:07 +01002077 cluster_sectors = 0;
2078 ret = bdrv_get_info(out_bs, &bdi);
2079 if (ret < 0) {
2080 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002081 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002082 goto out;
2083 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002084 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002085 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002086 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2087 }
2088
Kevin Wolf690c7302015-03-19 13:33:32 +01002089 state = (ImgConvertState) {
2090 .src = blk,
2091 .src_sectors = bs_sectors,
2092 .src_num = bs_n,
2093 .total_sectors = total_sectors,
2094 .target = out_blk,
2095 .compressed = compress,
2096 .target_has_backing = (bool) out_baseimg,
2097 .min_sparse = min_sparse,
2098 .cluster_sectors = cluster_sectors,
2099 .buf_sectors = bufsectors,
2100 };
2101 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002102
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002103out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002104 if (!ret) {
2105 qemu_progress_print(100, 0);
2106 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002107 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002108 qemu_opts_del(opts);
2109 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002110 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002111 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002112 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002113 if (blk) {
2114 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2115 blk_unref(blk[bs_i]);
2116 }
2117 g_free(blk);
2118 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002119 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002120fail_getopt:
2121 g_free(options);
2122
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002123 if (ret) {
2124 return 1;
2125 }
bellardea2384d2004-08-01 21:59:26 +00002126 return 0;
2127}
2128
bellard57d1a2b2004-08-03 21:15:11 +00002129
bellardfaea38e2006-08-05 21:31:00 +00002130static void dump_snapshots(BlockDriverState *bs)
2131{
2132 QEMUSnapshotInfo *sn_tab, *sn;
2133 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002134
2135 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2136 if (nb_sns <= 0)
2137 return;
2138 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002139 bdrv_snapshot_dump(fprintf, stdout, NULL);
2140 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002141 for(i = 0; i < nb_sns; i++) {
2142 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002143 bdrv_snapshot_dump(fprintf, stdout, sn);
2144 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002145 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002146 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002147}
2148
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002149static void dump_json_image_info_list(ImageInfoList *list)
2150{
Markus Armbruster4399c432014-04-25 16:50:32 +02002151 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002152 QString *str;
2153 QmpOutputVisitor *ov = qmp_output_visitor_new();
2154 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002155 visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
2156 &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002157 obj = qmp_output_get_qobject(ov);
2158 str = qobject_to_json_pretty(obj);
2159 assert(str != NULL);
2160 printf("%s\n", qstring_get_str(str));
2161 qobject_decref(obj);
2162 qmp_output_visitor_cleanup(ov);
2163 QDECREF(str);
2164}
2165
Benoît Canetc054b3f2012-09-05 13:09:02 +02002166static void dump_json_image_info(ImageInfo *info)
2167{
Markus Armbruster4399c432014-04-25 16:50:32 +02002168 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002169 QString *str;
2170 QmpOutputVisitor *ov = qmp_output_visitor_new();
2171 QObject *obj;
Eric Blake51e72bc2016-01-29 06:48:54 -07002172 visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002173 obj = qmp_output_get_qobject(ov);
2174 str = qobject_to_json_pretty(obj);
2175 assert(str != NULL);
2176 printf("%s\n", qstring_get_str(str));
2177 qobject_decref(obj);
2178 qmp_output_visitor_cleanup(ov);
2179 QDECREF(str);
2180}
2181
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002182static void dump_human_image_info_list(ImageInfoList *list)
2183{
2184 ImageInfoList *elem;
2185 bool delim = false;
2186
2187 for (elem = list; elem; elem = elem->next) {
2188 if (delim) {
2189 printf("\n");
2190 }
2191 delim = true;
2192
Wenchao Xia5b917042013-05-25 11:09:45 +08002193 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002194 }
2195}
2196
2197static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2198{
2199 return strcmp(a, b) == 0;
2200}
2201
2202/**
2203 * Open an image file chain and return an ImageInfoList
2204 *
2205 * @filename: topmost image filename
2206 * @fmt: topmost image format (may be NULL to autodetect)
2207 * @chain: true - enumerate entire backing file chain
2208 * false - only topmost image file
2209 *
2210 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2211 * image file. If there was an error a message will have been printed to
2212 * stderr.
2213 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002214static ImageInfoList *collect_image_info_list(bool image_opts,
2215 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002216 const char *fmt,
2217 bool chain)
2218{
2219 ImageInfoList *head = NULL;
2220 ImageInfoList **last = &head;
2221 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002222 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002223
2224 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2225
2226 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002227 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002228 BlockDriverState *bs;
2229 ImageInfo *info;
2230 ImageInfoList *elem;
2231
2232 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2233 error_report("Backing file '%s' creates an infinite loop.",
2234 filename);
2235 goto err;
2236 }
2237 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2238
Max Reitzefaa7c42016-03-16 19:54:38 +01002239 blk = img_open(image_opts, filename, fmt,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002240 BDRV_O_FLAGS | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
2241 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002242 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002243 goto err;
2244 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002245 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002246
Wenchao Xia43526ec2013-06-06 12:27:58 +08002247 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002248 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002249 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002250 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002251 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002252 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002253
2254 elem = g_new0(ImageInfoList, 1);
2255 elem->value = info;
2256 *last = elem;
2257 last = &elem->next;
2258
Markus Armbruster26f54e92014-10-07 13:59:04 +02002259 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002260
2261 filename = fmt = NULL;
2262 if (chain) {
2263 if (info->has_full_backing_filename) {
2264 filename = info->full_backing_filename;
2265 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002266 error_report("Could not determine absolute backing filename,"
2267 " but backing filename '%s' present",
2268 info->backing_filename);
2269 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002270 }
2271 if (info->has_backing_filename_format) {
2272 fmt = info->backing_filename_format;
2273 }
2274 }
2275 }
2276 g_hash_table_destroy(filenames);
2277 return head;
2278
2279err:
2280 qapi_free_ImageInfoList(head);
2281 g_hash_table_destroy(filenames);
2282 return NULL;
2283}
2284
Benoît Canetc054b3f2012-09-05 13:09:02 +02002285static int img_info(int argc, char **argv)
2286{
2287 int c;
2288 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002289 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002290 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002291 ImageInfoList *list;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002292 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002293 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002294
bellardea2384d2004-08-01 21:59:26 +00002295 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002296 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002297 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002298 int option_index = 0;
2299 static const struct option long_options[] = {
2300 {"help", no_argument, 0, 'h'},
2301 {"format", required_argument, 0, 'f'},
2302 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002303 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002304 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002305 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002306 {0, 0, 0, 0}
2307 };
2308 c = getopt_long(argc, argv, "f:h",
2309 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002310 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002311 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002312 }
bellardea2384d2004-08-01 21:59:26 +00002313 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002314 case '?':
bellardea2384d2004-08-01 21:59:26 +00002315 case 'h':
2316 help();
2317 break;
2318 case 'f':
2319 fmt = optarg;
2320 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002321 case OPTION_OUTPUT:
2322 output = optarg;
2323 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002324 case OPTION_BACKING_CHAIN:
2325 chain = true;
2326 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002327 case OPTION_OBJECT: {
2328 QemuOpts *opts;
2329 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2330 optarg, true);
2331 if (!opts) {
2332 return 1;
2333 }
2334 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002335 case OPTION_IMAGE_OPTS:
2336 image_opts = true;
2337 break;
bellardea2384d2004-08-01 21:59:26 +00002338 }
2339 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002340 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002341 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002342 }
bellardea2384d2004-08-01 21:59:26 +00002343 filename = argv[optind++];
2344
Benoît Canetc054b3f2012-09-05 13:09:02 +02002345 if (output && !strcmp(output, "json")) {
2346 output_format = OFORMAT_JSON;
2347 } else if (output && !strcmp(output, "human")) {
2348 output_format = OFORMAT_HUMAN;
2349 } else if (output) {
2350 error_report("--output must be used with human or json as argument.");
2351 return 1;
2352 }
2353
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002354 if (qemu_opts_foreach(&qemu_object_opts,
2355 user_creatable_add_opts_foreach,
2356 NULL, &local_err)) {
2357 error_report_err(local_err);
2358 return 1;
2359 }
2360
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002361 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002362 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002363 return 1;
2364 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002365
Benoît Canetc054b3f2012-09-05 13:09:02 +02002366 switch (output_format) {
2367 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002368 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002369 break;
2370 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002371 if (chain) {
2372 dump_json_image_info_list(list);
2373 } else {
2374 dump_json_image_info(list->value);
2375 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002376 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002377 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002378
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002379 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002380 return 0;
2381}
2382
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002383static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2384 MapEntry *next)
2385{
2386 switch (output_format) {
2387 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002388 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002389 error_report("File contains external, encrypted or compressed clusters.");
2390 exit(1);
2391 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002392 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002393 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002394 e->start, e->length,
2395 e->has_offset ? e->offset : 0,
2396 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002397 }
2398 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2399 * Modify the flags here to allow more coalescing.
2400 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002401 if (next && (!next->data || next->zero)) {
2402 next->data = false;
2403 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002404 }
2405 break;
2406 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002407 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2408 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002409 (e->start == 0 ? "[" : ",\n"),
2410 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002411 e->zero ? "true" : "false",
2412 e->data ? "true" : "false");
2413 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002414 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002415 }
2416 putchar('}');
2417
2418 if (!next) {
2419 printf("]\n");
2420 }
2421 break;
2422 }
2423}
2424
2425static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2426 int nb_sectors, MapEntry *e)
2427{
2428 int64_t ret;
2429 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002430 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002431 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002432
2433 /* As an optimization, we could cache the current range of unallocated
2434 * clusters in each file of the chain, and avoid querying the same
2435 * range repeatedly.
2436 */
2437
2438 depth = 0;
2439 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002440 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2441 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002442 if (ret < 0) {
2443 return ret;
2444 }
2445 assert(nb_sectors);
2446 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2447 break;
2448 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002449 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002450 if (bs == NULL) {
2451 ret = 0;
2452 break;
2453 }
2454
2455 depth++;
2456 }
2457
John Snow28756452016-02-05 13:12:33 -05002458 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2459
2460 *e = (MapEntry) {
2461 .start = sector_num * BDRV_SECTOR_SIZE,
2462 .length = nb_sectors * BDRV_SECTOR_SIZE,
2463 .data = !!(ret & BDRV_BLOCK_DATA),
2464 .zero = !!(ret & BDRV_BLOCK_ZERO),
2465 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2466 .has_offset = has_offset,
2467 .depth = depth,
2468 .has_filename = file && has_offset,
2469 .filename = file && has_offset ? file->filename : NULL,
2470 };
2471
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002472 return 0;
2473}
2474
Fam Zheng16b0d552016-01-26 11:59:02 +08002475static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2476{
2477 if (curr->length == 0) {
2478 return false;
2479 }
2480 if (curr->zero != next->zero ||
2481 curr->data != next->data ||
2482 curr->depth != next->depth ||
2483 curr->has_filename != next->has_filename ||
2484 curr->has_offset != next->has_offset) {
2485 return false;
2486 }
2487 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2488 return false;
2489 }
2490 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2491 return false;
2492 }
2493 return true;
2494}
2495
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002496static int img_map(int argc, char **argv)
2497{
2498 int c;
2499 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002500 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002501 BlockDriverState *bs;
2502 const char *filename, *fmt, *output;
2503 int64_t length;
2504 MapEntry curr = { .length = 0 }, next;
2505 int ret = 0;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002506 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002507 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002508
2509 fmt = NULL;
2510 output = NULL;
2511 for (;;) {
2512 int option_index = 0;
2513 static const struct option long_options[] = {
2514 {"help", no_argument, 0, 'h'},
2515 {"format", required_argument, 0, 'f'},
2516 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002517 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002518 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002519 {0, 0, 0, 0}
2520 };
2521 c = getopt_long(argc, argv, "f:h",
2522 long_options, &option_index);
2523 if (c == -1) {
2524 break;
2525 }
2526 switch (c) {
2527 case '?':
2528 case 'h':
2529 help();
2530 break;
2531 case 'f':
2532 fmt = optarg;
2533 break;
2534 case OPTION_OUTPUT:
2535 output = optarg;
2536 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002537 case OPTION_OBJECT: {
2538 QemuOpts *opts;
2539 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2540 optarg, true);
2541 if (!opts) {
2542 return 1;
2543 }
2544 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002545 case OPTION_IMAGE_OPTS:
2546 image_opts = true;
2547 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002548 }
2549 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002550 if (optind != argc - 1) {
2551 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002552 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002553 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002554
2555 if (output && !strcmp(output, "json")) {
2556 output_format = OFORMAT_JSON;
2557 } else if (output && !strcmp(output, "human")) {
2558 output_format = OFORMAT_HUMAN;
2559 } else if (output) {
2560 error_report("--output must be used with human or json as argument.");
2561 return 1;
2562 }
2563
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002564 if (qemu_opts_foreach(&qemu_object_opts,
2565 user_creatable_add_opts_foreach,
2566 NULL, &local_err)) {
2567 error_report_err(local_err);
2568 return 1;
2569 }
2570
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002571 blk = img_open(image_opts, filename, fmt, BDRV_O_FLAGS, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002572 if (!blk) {
2573 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002574 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002575 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002576
2577 if (output_format == OFORMAT_HUMAN) {
2578 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2579 }
2580
Max Reitzf1d3cd72015-02-05 13:58:18 -05002581 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002582 while (curr.start + curr.length < length) {
2583 int64_t nsectors_left;
2584 int64_t sector_num;
2585 int n;
2586
2587 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2588
2589 /* Probe up to 1 GiB at a time. */
2590 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2591 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2592 ret = get_block_status(bs, sector_num, n, &next);
2593
2594 if (ret < 0) {
2595 error_report("Could not read file metadata: %s", strerror(-ret));
2596 goto out;
2597 }
2598
Fam Zheng16b0d552016-01-26 11:59:02 +08002599 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002600 curr.length += next.length;
2601 continue;
2602 }
2603
2604 if (curr.length > 0) {
2605 dump_map_entry(output_format, &curr, &next);
2606 }
2607 curr = next;
2608 }
2609
2610 dump_map_entry(output_format, &curr, NULL);
2611
2612out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002613 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002614 return ret < 0;
2615}
2616
aliguorif7b4a942009-01-07 17:40:15 +00002617#define SNAPSHOT_LIST 1
2618#define SNAPSHOT_CREATE 2
2619#define SNAPSHOT_APPLY 3
2620#define SNAPSHOT_DELETE 4
2621
Stuart Brady153859b2009-06-07 00:42:17 +01002622static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002623{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002624 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002625 BlockDriverState *bs;
2626 QEMUSnapshotInfo sn;
2627 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002628 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002629 int action = 0;
2630 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002631 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002632 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002633 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002634
Kevin Wolf710da702011-01-10 12:33:02 +01002635 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002636 /* Parse commandline parameters */
2637 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002638 static const struct option long_options[] = {
2639 {"help", no_argument, 0, 'h'},
2640 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002641 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002642 {0, 0, 0, 0}
2643 };
2644 c = getopt_long(argc, argv, "la:c:d:hq",
2645 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002646 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002647 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002648 }
aliguorif7b4a942009-01-07 17:40:15 +00002649 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002650 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002651 case 'h':
2652 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002653 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002654 case 'l':
2655 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002656 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002657 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002658 }
2659 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002660 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002661 break;
2662 case 'a':
2663 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002664 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002665 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002666 }
2667 action = SNAPSHOT_APPLY;
2668 snapshot_name = optarg;
2669 break;
2670 case 'c':
2671 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002672 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002673 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002674 }
2675 action = SNAPSHOT_CREATE;
2676 snapshot_name = optarg;
2677 break;
2678 case 'd':
2679 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002680 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002681 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002682 }
2683 action = SNAPSHOT_DELETE;
2684 snapshot_name = optarg;
2685 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002686 case 'q':
2687 quiet = true;
2688 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002689 case OPTION_OBJECT: {
2690 QemuOpts *opts;
2691 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2692 optarg, true);
2693 if (!opts) {
2694 return 1;
2695 }
2696 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002697 case OPTION_IMAGE_OPTS:
2698 image_opts = true;
2699 break;
aliguorif7b4a942009-01-07 17:40:15 +00002700 }
2701 }
2702
Kevin Wolffc11eb22013-08-05 10:53:04 +02002703 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002704 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002705 }
aliguorif7b4a942009-01-07 17:40:15 +00002706 filename = argv[optind++];
2707
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002708 if (qemu_opts_foreach(&qemu_object_opts,
2709 user_creatable_add_opts_foreach,
2710 NULL, &err)) {
2711 error_report_err(err);
2712 return 1;
2713 }
2714
aliguorif7b4a942009-01-07 17:40:15 +00002715 /* Open the image */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002716 blk = img_open(image_opts, filename, NULL, bdrv_oflags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002717 if (!blk) {
2718 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002719 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002720 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002721
2722 /* Perform the requested action */
2723 switch(action) {
2724 case SNAPSHOT_LIST:
2725 dump_snapshots(bs);
2726 break;
2727
2728 case SNAPSHOT_CREATE:
2729 memset(&sn, 0, sizeof(sn));
2730 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2731
2732 qemu_gettimeofday(&tv);
2733 sn.date_sec = tv.tv_sec;
2734 sn.date_nsec = tv.tv_usec * 1000;
2735
2736 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002737 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002738 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002739 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002740 }
aliguorif7b4a942009-01-07 17:40:15 +00002741 break;
2742
2743 case SNAPSHOT_APPLY:
2744 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002745 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002746 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002747 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002748 }
aliguorif7b4a942009-01-07 17:40:15 +00002749 break;
2750
2751 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002752 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002753 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002754 error_reportf_err(err, "Could not delete snapshot '%s': ",
2755 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002756 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002757 }
aliguorif7b4a942009-01-07 17:40:15 +00002758 break;
2759 }
2760
2761 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002762 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002763 if (ret) {
2764 return 1;
2765 }
Stuart Brady153859b2009-06-07 00:42:17 +01002766 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002767}
2768
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002769static int img_rebase(int argc, char **argv)
2770{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002771 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002772 uint8_t *buf_old = NULL;
2773 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002774 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002775 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002776 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2777 int c, flags, src_flags, ret;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002778 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002779 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002780 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002781 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002782 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002783
2784 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002785 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002786 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002787 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002788 out_baseimg = NULL;
2789 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002790 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002791 static const struct option long_options[] = {
2792 {"help", no_argument, 0, 'h'},
2793 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002794 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002795 {0, 0, 0, 0}
2796 };
2797 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2798 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002799 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002800 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002801 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002802 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002803 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002804 case 'h':
2805 help();
2806 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002807 case 'f':
2808 fmt = optarg;
2809 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002810 case 'F':
2811 out_basefmt = optarg;
2812 break;
2813 case 'b':
2814 out_baseimg = optarg;
2815 break;
2816 case 'u':
2817 unsafe = 1;
2818 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002819 case 'p':
2820 progress = 1;
2821 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002822 case 't':
2823 cache = optarg;
2824 break;
Max Reitz40055952014-07-22 22:58:42 +02002825 case 'T':
2826 src_cache = optarg;
2827 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002828 case 'q':
2829 quiet = true;
2830 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002831 case OPTION_OBJECT: {
2832 QemuOpts *opts;
2833 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2834 optarg, true);
2835 if (!opts) {
2836 return 1;
2837 }
2838 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002839 case OPTION_IMAGE_OPTS:
2840 image_opts = true;
2841 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002842 }
2843 }
2844
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002845 if (quiet) {
2846 progress = 0;
2847 }
2848
Fam Zhengac1307a2014-04-22 13:36:11 +08002849 if (optind != argc - 1) {
2850 error_exit("Expecting one image file name");
2851 }
2852 if (!unsafe && !out_baseimg) {
2853 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002854 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002855 filename = argv[optind++];
2856
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002857 if (qemu_opts_foreach(&qemu_object_opts,
2858 user_creatable_add_opts_foreach,
2859 NULL, &local_err)) {
2860 error_report_err(local_err);
2861 return 1;
2862 }
2863
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002864 qemu_progress_init(progress, 2.0);
2865 qemu_progress_print(0, 100);
2866
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002867 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002868 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002869 if (ret < 0) {
2870 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002871 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002872 }
2873
Max Reitz40055952014-07-22 22:58:42 +02002874 src_flags = BDRV_O_FLAGS;
2875 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
2876 if (ret < 0) {
2877 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002878 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002879 }
2880
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002881 /*
2882 * Open the images.
2883 *
2884 * Ignore the old backing file for unsafe rebase in case we want to correct
2885 * the reference to a renamed or moved backing file.
2886 */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00002887 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002888 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002889 ret = -1;
2890 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002891 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002892 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002893
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002894 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05002895 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002896 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002897 ret = -1;
2898 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002899 }
2900 }
2901
2902 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002903 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002904 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05002905 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002906
Max Reitz644483d2015-02-05 13:58:17 -05002907 if (bs->backing_format[0] != '\0') {
2908 options = qdict_new();
2909 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
2910 }
2911
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002912 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01002913 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002914 options, src_flags, &local_err);
2915 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002916 error_reportf_err(local_err,
2917 "Could not open old backing file '%s': ",
2918 backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002919 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002920 }
Max Reitz644483d2015-02-05 13:58:17 -05002921
Alex Bligha6166732012-10-16 13:46:18 +01002922 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05002923 if (out_basefmt) {
2924 options = qdict_new();
2925 qdict_put(options, "driver", qstring_from_str(out_basefmt));
2926 } else {
2927 options = NULL;
2928 }
2929
Max Reitzefaa7c42016-03-16 19:54:38 +01002930 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05002931 options, src_flags, &local_err);
2932 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002933 error_reportf_err(local_err,
2934 "Could not open new backing file '%s': ",
2935 out_baseimg);
Alex Bligha6166732012-10-16 13:46:18 +01002936 goto out;
2937 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002938 }
2939 }
2940
2941 /*
2942 * Check each unallocated cluster in the COW file. If it is unallocated,
2943 * accesses go to the backing file. We must therefore compare this cluster
2944 * in the old and new backing file, and if they differ we need to copy it
2945 * from the old backing file into the COW file.
2946 *
2947 * If qemu-img crashes during this step, no harm is done. The content of
2948 * the image is the same as the original one at any time.
2949 */
2950 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002951 int64_t num_sectors;
2952 int64_t old_backing_num_sectors;
2953 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002954 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002955 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02002956 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002957
Max Reitzf1d3cd72015-02-05 13:58:18 -05002958 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
2959 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002960
Max Reitzf1d3cd72015-02-05 13:58:18 -05002961 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002962 if (num_sectors < 0) {
2963 error_report("Could not get size of '%s': %s",
2964 filename, strerror(-num_sectors));
2965 ret = -1;
2966 goto out;
2967 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002968 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002969 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05002970 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002971
2972 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2973 error_report("Could not get size of '%s': %s",
2974 backing_name, strerror(-old_backing_num_sectors));
2975 ret = -1;
2976 goto out;
2977 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05002978 if (blk_new_backing) {
2979 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002980 if (new_backing_num_sectors < 0) {
2981 error_report("Could not get size of '%s': %s",
2982 out_baseimg, strerror(-new_backing_num_sectors));
2983 ret = -1;
2984 goto out;
2985 }
Alex Bligha6166732012-10-16 13:46:18 +01002986 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002987
Kevin Wolf1f710492012-10-12 14:29:18 +02002988 if (num_sectors != 0) {
2989 local_progress = (float)100 /
2990 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2991 }
2992
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002993 for (sector = 0; sector < num_sectors; sector += n) {
2994
2995 /* How many sectors can we handle with the next read? */
2996 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2997 n = (IO_BUF_SIZE / 512);
2998 } else {
2999 n = num_sectors - sector;
3000 }
3001
3002 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003003 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003004 if (ret < 0) {
3005 error_report("error while reading image metadata: %s",
3006 strerror(-ret));
3007 goto out;
3008 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003009 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003010 continue;
3011 }
3012
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003013 /*
3014 * Read old and new backing file and take into consideration that
3015 * backing files may be smaller than the COW image.
3016 */
3017 if (sector >= old_backing_num_sectors) {
3018 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3019 } else {
3020 if (sector + n > old_backing_num_sectors) {
3021 n = old_backing_num_sectors - sector;
3022 }
3023
Max Reitzf1d3cd72015-02-05 13:58:18 -05003024 ret = blk_read(blk_old_backing, sector, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003025 if (ret < 0) {
3026 error_report("error while reading from old backing file");
3027 goto out;
3028 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003029 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003030
Max Reitzf1d3cd72015-02-05 13:58:18 -05003031 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003032 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3033 } else {
3034 if (sector + n > new_backing_num_sectors) {
3035 n = new_backing_num_sectors - sector;
3036 }
3037
Max Reitzf1d3cd72015-02-05 13:58:18 -05003038 ret = blk_read(blk_new_backing, sector, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003039 if (ret < 0) {
3040 error_report("error while reading from new backing file");
3041 goto out;
3042 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003043 }
3044
3045 /* If they differ, we need to write to the COW file */
3046 uint64_t written = 0;
3047
3048 while (written < n) {
3049 int pnum;
3050
3051 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003052 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003053 {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003054 ret = blk_write(blk, sector + written,
3055 buf_old + written * 512, pnum);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003056 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003057 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003058 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003059 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003060 }
3061 }
3062
3063 written += pnum;
3064 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003065 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003066 }
3067 }
3068
3069 /*
3070 * Change the backing file. All clusters that are different from the old
3071 * backing file are overwritten in the COW file now, so the visible content
3072 * doesn't change when we switch the backing file.
3073 */
Alex Bligha6166732012-10-16 13:46:18 +01003074 if (out_baseimg && *out_baseimg) {
3075 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3076 } else {
3077 ret = bdrv_change_backing_file(bs, NULL, NULL);
3078 }
3079
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003080 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003081 error_report("Could not change the backing file to '%s': No "
3082 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003083 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003084 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003085 out_baseimg, strerror(-ret));
3086 }
3087
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003088 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003089 /*
3090 * TODO At this point it is possible to check if any clusters that are
3091 * allocated in the COW file are the same in the backing file. If so, they
3092 * could be dropped from the COW file. Don't do this before switching the
3093 * backing file, in case of a crash this would lead to corruption.
3094 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003095out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003096 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003097 /* Cleanup */
3098 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003099 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003100 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003101 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003102 qemu_vfree(buf_old);
3103 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003104
Markus Armbruster26f54e92014-10-07 13:59:04 +02003105 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003106 if (ret) {
3107 return 1;
3108 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003109 return 0;
3110}
3111
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003112static int img_resize(int argc, char **argv)
3113{
Markus Armbruster6750e792015-02-12 17:43:08 +01003114 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003115 int c, ret, relative;
3116 const char *filename, *fmt, *size;
3117 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003118 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003119 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003120 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003121 Error *local_err = NULL;
3122
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003123 static QemuOptsList resize_options = {
3124 .name = "resize_options",
3125 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3126 .desc = {
3127 {
3128 .name = BLOCK_OPT_SIZE,
3129 .type = QEMU_OPT_SIZE,
3130 .help = "Virtual disk size"
3131 }, {
3132 /* end of list */
3133 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003134 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003135 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003136 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003137
Kevin Wolfe80fec72011-04-29 10:58:12 +02003138 /* Remove size from argv manually so that negative numbers are not treated
3139 * as options by getopt. */
3140 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003141 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003142 return 1;
3143 }
3144
3145 size = argv[--argc];
3146
3147 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003148 fmt = NULL;
3149 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003150 static const struct option long_options[] = {
3151 {"help", no_argument, 0, 'h'},
3152 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003153 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003154 {0, 0, 0, 0}
3155 };
3156 c = getopt_long(argc, argv, "f:hq",
3157 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003158 if (c == -1) {
3159 break;
3160 }
3161 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003162 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003163 case 'h':
3164 help();
3165 break;
3166 case 'f':
3167 fmt = optarg;
3168 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003169 case 'q':
3170 quiet = true;
3171 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003172 case OPTION_OBJECT: {
3173 QemuOpts *opts;
3174 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3175 optarg, true);
3176 if (!opts) {
3177 return 1;
3178 }
3179 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003180 case OPTION_IMAGE_OPTS:
3181 image_opts = true;
3182 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003183 }
3184 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003185 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003186 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003187 }
3188 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003189
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003190 if (qemu_opts_foreach(&qemu_object_opts,
3191 user_creatable_add_opts_foreach,
3192 NULL, &local_err)) {
3193 error_report_err(local_err);
3194 return 1;
3195 }
3196
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003197 /* Choose grow, shrink, or absolute resize mode */
3198 switch (size[0]) {
3199 case '+':
3200 relative = 1;
3201 size++;
3202 break;
3203 case '-':
3204 relative = -1;
3205 size++;
3206 break;
3207 default:
3208 relative = 0;
3209 break;
3210 }
3211
3212 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003213 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003214 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003215 if (err) {
3216 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003217 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003218 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003219 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003220 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003221 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3222 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003223
Max Reitzefaa7c42016-03-16 19:54:38 +01003224 blk = img_open(image_opts, filename, fmt,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00003225 BDRV_O_FLAGS | BDRV_O_RDWR, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003226 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003227 ret = -1;
3228 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003229 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003230
3231 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003232 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003233 } else {
3234 total_size = n;
3235 }
3236 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003237 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003238 ret = -1;
3239 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003240 }
3241
Max Reitzf1d3cd72015-02-05 13:58:18 -05003242 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003243 switch (ret) {
3244 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003245 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003246 break;
3247 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003248 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003249 break;
3250 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003251 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003252 break;
3253 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01003254 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003255 break;
3256 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003257out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003258 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003259 if (ret) {
3260 return 1;
3261 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003262 return 0;
3263}
3264
Max Reitz76a3a342014-10-27 11:12:51 +01003265static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003266 int64_t offset, int64_t total_work_size,
3267 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003268{
3269 qemu_progress_print(100.f * offset / total_work_size, 0);
3270}
3271
Max Reitz6f176b42013-09-03 10:09:50 +02003272static int img_amend(int argc, char **argv)
3273{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003274 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003275 int c, ret = 0;
3276 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003277 QemuOptsList *create_opts = NULL;
3278 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003279 const char *fmt = NULL, *filename, *cache;
3280 int flags;
Max Reitz76a3a342014-10-27 11:12:51 +01003281 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003282 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003283 BlockDriverState *bs = NULL;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003284 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003285 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003286
Max Reitzbd39e6e2014-07-22 22:58:43 +02003287 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003288 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003289 static const struct option long_options[] = {
3290 {"help", no_argument, 0, 'h'},
3291 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003292 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003293 {0, 0, 0, 0}
3294 };
3295 c = getopt_long(argc, argv, "ho:f:t:pq",
3296 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003297 if (c == -1) {
3298 break;
3299 }
3300
3301 switch (c) {
3302 case 'h':
3303 case '?':
3304 help();
3305 break;
3306 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003307 if (!is_valid_option_list(optarg)) {
3308 error_report("Invalid option list: %s", optarg);
3309 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003310 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003311 }
3312 if (!options) {
3313 options = g_strdup(optarg);
3314 } else {
3315 char *old_options = options;
3316 options = g_strdup_printf("%s,%s", options, optarg);
3317 g_free(old_options);
3318 }
Max Reitz6f176b42013-09-03 10:09:50 +02003319 break;
3320 case 'f':
3321 fmt = optarg;
3322 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003323 case 't':
3324 cache = optarg;
3325 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003326 case 'p':
3327 progress = true;
3328 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003329 case 'q':
3330 quiet = true;
3331 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003332 case OPTION_OBJECT:
3333 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3334 optarg, true);
3335 if (!opts) {
3336 ret = -1;
3337 goto out_no_progress;
3338 }
3339 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003340 case OPTION_IMAGE_OPTS:
3341 image_opts = true;
3342 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003343 }
3344 }
3345
Max Reitz6f176b42013-09-03 10:09:50 +02003346 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003347 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003348 }
3349
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003350 if (qemu_opts_foreach(&qemu_object_opts,
3351 user_creatable_add_opts_foreach,
3352 NULL, &local_err)) {
3353 error_report_err(local_err);
3354 ret = -1;
3355 goto out_no_progress;
3356 }
3357
Max Reitz76a3a342014-10-27 11:12:51 +01003358 if (quiet) {
3359 progress = false;
3360 }
3361 qemu_progress_init(progress, 1.0);
3362
Kevin Wolfa283cb62014-02-21 16:24:07 +01003363 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3364 if (fmt && has_help_option(options)) {
3365 /* If a format is explicitly specified (and possibly no filename is
3366 * given), print option help here */
3367 ret = print_block_option_help(filename, fmt);
3368 goto out;
3369 }
3370
3371 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003372 error_report("Expecting one image file name");
3373 ret = -1;
3374 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003375 }
Max Reitz6f176b42013-09-03 10:09:50 +02003376
Max Reitzbd39e6e2014-07-22 22:58:43 +02003377 flags = BDRV_O_FLAGS | BDRV_O_RDWR;
3378 ret = bdrv_parse_cache_flags(cache, &flags);
3379 if (ret < 0) {
3380 error_report("Invalid cache option: %s", cache);
3381 goto out;
3382 }
3383
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +00003384 blk = img_open(image_opts, filename, fmt, flags, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003385 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003386 ret = -1;
3387 goto out;
3388 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003389 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003390
3391 fmt = bs->drv->format_name;
3392
Kevin Wolf626f84f2014-02-21 16:24:06 +01003393 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003394 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003395 ret = print_block_option_help(filename, fmt);
3396 goto out;
3397 }
3398
Max Reitzb2439d22014-12-02 18:32:47 +01003399 if (!bs->drv->create_opts) {
3400 error_report("Format driver '%s' does not support any options to amend",
3401 fmt);
3402 ret = -1;
3403 goto out;
3404 }
3405
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003406 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003407 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003408 if (options) {
3409 qemu_opts_do_parse(opts, options, NULL, &err);
3410 if (err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01003411 error_report_err(err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003412 ret = -1;
3413 goto out;
3414 }
Max Reitz6f176b42013-09-03 10:09:50 +02003415 }
3416
Max Reitz76a3a342014-10-27 11:12:51 +01003417 /* In case the driver does not call amend_status_cb() */
3418 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003419 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003420 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003421 if (ret < 0) {
3422 error_report("Error while amending options: %s", strerror(-ret));
3423 goto out;
3424 }
3425
3426out:
Max Reitz76a3a342014-10-27 11:12:51 +01003427 qemu_progress_end();
3428
Max Reitze814dff2015-08-20 16:00:38 -07003429out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003430 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003431 qemu_opts_del(opts);
3432 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003433 g_free(options);
3434
Max Reitz6f176b42013-09-03 10:09:50 +02003435 if (ret) {
3436 return 1;
3437 }
3438 return 0;
3439}
3440
Anthony Liguoric227f092009-10-01 16:12:16 -05003441static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01003442#define DEF(option, callback, arg_string) \
3443 { option, callback },
3444#include "qemu-img-cmds.h"
3445#undef DEF
3446#undef GEN_DOCS
3447 { NULL, NULL, },
3448};
3449
bellardea2384d2004-08-01 21:59:26 +00003450int main(int argc, char **argv)
3451{
Anthony Liguoric227f092009-10-01 16:12:16 -05003452 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01003453 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003454 Error *local_error = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04003455 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04003456 static const struct option long_options[] = {
3457 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04003458 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04003459 {0, 0, 0, 0}
3460 };
bellardea2384d2004-08-01 21:59:26 +00003461
MORITA Kazutaka526eda12013-07-23 17:30:11 +09003462#ifdef CONFIG_POSIX
3463 signal(SIGPIPE, SIG_IGN);
3464#endif
3465
Kevin Wolf53f76e52010-12-16 15:10:32 +01003466 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08003467 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01003468
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003469 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01003470 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03003471 exit(EXIT_FAILURE);
3472 }
3473
Daniel P. Berrange064097d2016-02-10 18:41:01 +00003474 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00003475 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08003476 if (argc < 2) {
3477 error_exit("Not enough arguments");
3478 }
Stuart Brady153859b2009-06-07 00:42:17 +01003479 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01003480
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003481 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003482 qemu_add_opts(&qemu_source_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003483
Stuart Brady153859b2009-06-07 00:42:17 +01003484 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04003485 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01003486 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04003487 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01003488 }
bellardea2384d2004-08-01 21:59:26 +00003489 }
Stuart Brady153859b2009-06-07 00:42:17 +01003490
Jeff Cody5f6979c2014-04-28 14:37:18 -04003491 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04003492
3493 if (c == 'h') {
3494 help();
3495 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04003496 if (c == 'v') {
3497 printf(QEMU_IMG_VERSION);
3498 return 0;
3499 }
Jeff Cody7db16892014-04-25 17:02:32 -04003500
Stuart Brady153859b2009-06-07 00:42:17 +01003501 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08003502 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00003503}