blob: e0e3d31455f285476177ea12fa4130b35db63c90 [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"
Fam Zheng67a1de02016-06-01 17:44:21 +080025#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020027#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010028#include "qapi/qobject-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010030#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080031#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000033#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/option.h"
35#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030036#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000037#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020039#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010040#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020041#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080042#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010043#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030044#include "trace/control.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020045#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000046
Don Slutz61979a62015-01-09 10:17:35 -050047#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020048 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040049
Anthony Liguoric227f092009-10-01 16:12:16 -050050typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010051 const char *name;
52 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050053} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010054
Federico Simoncelli8599ea42013-01-28 06:59:47 -050055enum {
56 OPTION_OUTPUT = 256,
57 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000058 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000059 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020060 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020061 OPTION_FLUSH_INTERVAL = 261,
62 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010063 OPTION_TARGET_IMAGE_OPTS = 263,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050064};
65
66typedef enum OutputFormat {
67 OFORMAT_JSON,
68 OFORMAT_HUMAN,
69} OutputFormat;
70
Kevin Wolfe6996142016-03-15 13:03:11 +010071/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040072#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000073
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010074static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000075{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010076 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000077}
78
Fam Zhengac1307a2014-04-22 13:36:11 +080079static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
80{
81 va_list ap;
82
83 error_printf("qemu-img: ");
84
85 va_start(ap, fmt);
86 error_vprintf(fmt, ap);
87 va_end(ap);
88
89 error_printf("\nTry 'qemu-img --help' for more information\n");
90 exit(EXIT_FAILURE);
91}
92
Stefan Hajnoczic9192972017-03-17 18:45:41 +080093static void QEMU_NORETURN missing_argument(const char *option)
94{
95 error_exit("missing argument for option '%s'", option);
96}
97
98static void QEMU_NORETURN unrecognized_option(const char *option)
99{
100 error_exit("unrecognized option '%s'", option);
101}
102
blueswir1d2c639d2009-01-24 18:19:25 +0000103/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800104static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000105{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100106 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400107 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300108 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300109 "QEMU disk image utility\n"
110 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300111 " '-h', '--help' display this help and exit\n"
112 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300113 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300115 "\n"
malc3f020d72010-02-08 12:04:56 +0300116 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100117#define DEF(option, callback, arg_string) \
118 " " arg_string "\n"
119#include "qemu-img-cmds.h"
120#undef DEF
121#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300122 "\n"
123 "Command parameters:\n"
124 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000125 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
126 " manual page for a description of the object properties. The most common\n"
127 " object type is a 'secret', which is used to supply passwords and/or\n"
128 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300129 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400130 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800131 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
132 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100133 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
134 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300135 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200136 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
137 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
138 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300139 " 'output_filename' is the destination disk image filename\n"
140 " 'output_fmt' is the destination format\n"
141 " 'options' is a comma separated list of format specific options in a\n"
142 " name=value format. Use -o ? for an overview of the options supported by the\n"
143 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800144 " 'snapshot_param' is param used for internal snapshot, format\n"
145 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
146 " '[ID_OR_NAME]'\n"
147 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
148 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
151 " match exactly. The image doesn't need a working backing file before\n"
152 " rebasing in this case (useful for renaming the backing file)\n"
153 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200154 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100155 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200156 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
157 " contain only zeros for qemu-img to create a sparse image during\n"
158 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
159 " unallocated or zero sectors, and the destination image will always be\n"
160 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200161 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100162 " '-n' skips the target volume creation (useful if the volume is created\n"
163 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300164 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200165 "Parameters to check subcommand:\n"
166 " '-r' tries to repair any inconsistencies that are found during the check.\n"
167 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
168 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200169 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200170 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100171 "Parameters to convert subcommand:\n"
172 " '-m' specifies how many coroutines work in parallel during the convert\n"
173 " process (defaults to 8)\n"
174 " '-W' allow to write to the target out of order rather than sequential\n"
175 "\n"
malc3f020d72010-02-08 12:04:56 +0300176 "Parameters to snapshot subcommand:\n"
177 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
178 " '-a' applies a snapshot (revert disk to saved state)\n"
179 " '-c' creates a snapshot\n"
180 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100181 " '-l' lists all snapshots in the given image\n"
182 "\n"
183 "Parameters to compare subcommand:\n"
184 " '-f' first image format\n"
185 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200186 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
187 "\n"
188 "Parameters to dd subcommand:\n"
189 " 'bs=BYTES' read and write up to BYTES bytes at a time "
190 "(default: 512)\n"
191 " 'count=N' copy only N input blocks\n"
192 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200193 " 'of=FILE' write to FILE\n"
194 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100195
196 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100197 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000198 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800199 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000200}
201
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000202static QemuOptsList qemu_object_opts = {
203 .name = "object",
204 .implied_opt_name = "qom-type",
205 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
206 .desc = {
207 { }
208 },
209};
210
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000211static QemuOptsList qemu_source_opts = {
212 .name = "source",
213 .implied_opt_name = "file",
214 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
215 .desc = {
216 { }
217 },
218};
219
Stefan Weil7c30f652013-06-16 17:01:05 +0200220static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100221{
222 int ret = 0;
223 if (!quiet) {
224 va_list args;
225 va_start(args, fmt);
226 ret = vprintf(fmt, args);
227 va_end(args);
228 }
229 return ret;
230}
231
bellardea2384d2004-08-01 21:59:26 +0000232
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100233static int print_block_option_help(const char *filename, const char *fmt)
234{
235 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800236 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500237 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100238
239 /* Find driver and parse its options */
240 drv = bdrv_find_format(fmt);
241 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100242 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100243 return 1;
244 }
245
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800246 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100247 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500248 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100249 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100250 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800251 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100252 return 1;
253 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800254 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100255 }
256
Chunyan Liu83d05212014-06-05 17:20:51 +0800257 qemu_opts_print_help(create_opts);
258 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100259 return 0;
260}
261
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000262
263static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000264 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000265{
266 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000267 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000268
269 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000270 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
271 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000272 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
273 if (qemu_read_password(password, sizeof(password)) < 0) {
274 error_report("No password given");
275 return -1;
276 }
277 if (bdrv_set_key(bs, password) < 0) {
278 error_report("invalid password");
279 return -1;
280 }
281 }
282 return 0;
283}
284
285
Max Reitzefaa7c42016-03-16 19:54:38 +0100286static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100287 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800288 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289{
290 QDict *options;
291 Error *local_err = NULL;
292 BlockBackend *blk;
293 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800294 if (force_share) {
295 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
296 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
297 error_report("--force-share/-U conflicts with image options");
298 return NULL;
299 }
300 qdict_put(options, BDRV_OPT_FORCE_SHARE, qbool_from_bool(true));
301 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100302 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000303 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100304 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000305 return NULL;
306 }
Kevin Wolfce099542016-03-15 13:01:04 +0100307 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000308
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000309 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000310 blk_unref(blk);
311 return NULL;
312 }
313 return blk;
314}
315
Max Reitzefaa7c42016-03-16 19:54:38 +0100316static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000317 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800318 bool writethrough, bool quiet,
319 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000320{
321 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200322 Error *local_err = NULL;
Fam Zheng335e9932017-05-03 00:35:39 +0800323 QDict *options = qdict_new();
Kevin Wolfad717132010-12-16 15:37:41 +0100324
bellard75c23802004-08-27 21:28:58 +0000325 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500326 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000327 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100328
Fam Zheng335e9932017-05-03 00:35:39 +0800329 if (force_share) {
330 qdict_put(options, BDRV_OPT_FORCE_SHARE, qbool_from_bool(true));
331 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100332 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500333 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100334 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000335 return NULL;
bellard75c23802004-08-27 21:28:58 +0000336 }
Kevin Wolfce099542016-03-15 13:01:04 +0100337 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100338
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000339 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000340 blk_unref(blk);
341 return NULL;
bellard75c23802004-08-27 21:28:58 +0000342 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200343 return blk;
bellard75c23802004-08-27 21:28:58 +0000344}
345
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000346
Max Reitzefaa7c42016-03-16 19:54:38 +0100347static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000348 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100349 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800350 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000351{
352 BlockBackend *blk;
353 if (image_opts) {
354 QemuOpts *opts;
355 if (fmt) {
356 error_report("--image-opts and --format are mutually exclusive");
357 return NULL;
358 }
359 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
360 filename, true);
361 if (!opts) {
362 return NULL;
363 }
Fam Zheng335e9932017-05-03 00:35:39 +0800364 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
365 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000366 } else {
Fam Zheng335e9932017-05-03 00:35:39 +0800367 blk = img_open_file(filename, fmt, flags, writethrough, quiet,
368 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000369 }
370 return blk;
371}
372
373
Chunyan Liu83d05212014-06-05 17:20:51 +0800374static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100375 const char *base_filename,
376 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200377{
Markus Armbruster6750e792015-02-12 17:43:08 +0100378 Error *err = NULL;
379
Kevin Wolfefa84d42009-05-18 16:42:12 +0200380 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100381 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100382 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100383 error_report("Backing file not supported for file format '%s'",
384 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100385 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900386 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200387 }
388 }
389 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100390 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100391 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100392 error_report("Backing file format not supported for file "
393 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100394 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900395 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200396 }
397 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900398 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200399}
400
Markus Armbruster606caa02017-02-21 21:14:04 +0100401static int64_t cvtnum(const char *s)
402{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100403 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100404 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100405
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100406 err = qemu_strtosz(s, NULL, &value);
407 if (err < 0) {
408 return err;
409 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100410 if (value > INT64_MAX) {
411 return -ERANGE;
412 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100413 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100414}
415
bellardea2384d2004-08-01 21:59:26 +0000416static int img_create(int argc, char **argv)
417{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200418 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100419 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000420 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000421 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000422 const char *filename;
423 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200424 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200425 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100426 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000427
bellardea2384d2004-08-01 21:59:26 +0000428 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000429 static const struct option long_options[] = {
430 {"help", no_argument, 0, 'h'},
431 {"object", required_argument, 0, OPTION_OBJECT},
432 {0, 0, 0, 0}
433 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800434 c = getopt_long(argc, argv, ":F:b:f:he6o:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000435 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100436 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000437 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100438 }
bellardea2384d2004-08-01 21:59:26 +0000439 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800440 case ':':
441 missing_argument(argv[optind - 1]);
442 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100443 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800444 unrecognized_option(argv[optind - 1]);
445 break;
bellardea2384d2004-08-01 21:59:26 +0000446 case 'h':
447 help();
448 break;
aliguori9230eaf2009-03-28 17:55:19 +0000449 case 'F':
450 base_fmt = optarg;
451 break;
bellardea2384d2004-08-01 21:59:26 +0000452 case 'b':
453 base_filename = optarg;
454 break;
455 case 'f':
456 fmt = optarg;
457 break;
458 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200459 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100460 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100461 goto fail;
thsd8871c52007-10-24 16:11:42 +0000462 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200463 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100464 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100465 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200466 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100467 if (!is_valid_option_list(optarg)) {
468 error_report("Invalid option list: %s", optarg);
469 goto fail;
470 }
471 if (!options) {
472 options = g_strdup(optarg);
473 } else {
474 char *old_options = options;
475 options = g_strdup_printf("%s,%s", options, optarg);
476 g_free(old_options);
477 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200478 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100479 case 'q':
480 quiet = true;
481 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000482 case OPTION_OBJECT: {
483 QemuOpts *opts;
484 opts = qemu_opts_parse_noisily(&qemu_object_opts,
485 optarg, true);
486 if (!opts) {
487 goto fail;
488 }
489 } break;
bellardea2384d2004-08-01 21:59:26 +0000490 }
491 }
aliguori9230eaf2009-03-28 17:55:19 +0000492
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900493 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100494 filename = (optind < argc) ? argv[optind] : NULL;
495 if (options && has_help_option(options)) {
496 g_free(options);
497 return print_block_option_help(filename, fmt);
498 }
499
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100500 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800501 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100502 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100503 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900504
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000505 if (qemu_opts_foreach(&qemu_object_opts,
506 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200507 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000508 goto fail;
509 }
510
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100511 /* Get image size, if specified */
512 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100513 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100514
515 sval = cvtnum(argv[optind++]);
516 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800517 if (sval == -ERANGE) {
518 error_report("Image size must be less than 8 EiB!");
519 } else {
520 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200521 "G, T, P or E suffixes for ");
522 error_report("kilobytes, megabytes, gigabytes, terabytes, "
523 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800524 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100525 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100526 }
527 img_size = (uint64_t)sval;
528 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200529 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800530 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200531 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100532
Luiz Capitulino9b375252012-11-30 10:52:05 -0200533 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800534 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100535 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100536 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100537 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900538 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200539
Kevin Wolf77386bf2014-02-21 16:24:04 +0100540 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000541 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100542
543fail:
544 g_free(options);
545 return 1;
bellardea2384d2004-08-01 21:59:26 +0000546}
547
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100548static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500549{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500550 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500551 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100552 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600553
554 visit_type_ImageCheck(v, NULL, &check, &error_abort);
555 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500556 str = qobject_to_json_pretty(obj);
557 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100558 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500559 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600560 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500561 QDECREF(str);
562}
563
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100564static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500565{
566 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100567 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500568 } else {
569 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100570 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
571 "Data may be corrupted, or further writes to the image "
572 "may corrupt it.\n",
573 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500574 }
575
576 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100577 qprintf(quiet,
578 "\n%" PRId64 " leaked clusters were found on the image.\n"
579 "This means waste of disk space, but no harm to data.\n",
580 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500581 }
582
583 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100584 qprintf(quiet,
585 "\n%" PRId64
586 " internal errors have occurred during the check.\n",
587 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500588 }
589 }
590
591 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100592 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
593 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
594 check->allocated_clusters, check->total_clusters,
595 check->allocated_clusters * 100.0 / check->total_clusters,
596 check->fragmented_clusters * 100.0 / check->allocated_clusters,
597 check->compressed_clusters * 100.0 /
598 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500599 }
600
601 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100602 qprintf(quiet,
603 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500604 }
605}
606
607static int collect_image_check(BlockDriverState *bs,
608 ImageCheck *check,
609 const char *filename,
610 const char *fmt,
611 int fix)
612{
613 int ret;
614 BdrvCheckResult result;
615
616 ret = bdrv_check(bs, &result, fix);
617 if (ret < 0) {
618 return ret;
619 }
620
621 check->filename = g_strdup(filename);
622 check->format = g_strdup(bdrv_get_format_name(bs));
623 check->check_errors = result.check_errors;
624 check->corruptions = result.corruptions;
625 check->has_corruptions = result.corruptions != 0;
626 check->leaks = result.leaks;
627 check->has_leaks = result.leaks != 0;
628 check->corruptions_fixed = result.corruptions_fixed;
629 check->has_corruptions_fixed = result.corruptions != 0;
630 check->leaks_fixed = result.leaks_fixed;
631 check->has_leaks_fixed = result.leaks != 0;
632 check->image_end_offset = result.image_end_offset;
633 check->has_image_end_offset = result.image_end_offset != 0;
634 check->total_clusters = result.bfi.total_clusters;
635 check->has_total_clusters = result.bfi.total_clusters != 0;
636 check->allocated_clusters = result.bfi.allocated_clusters;
637 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
638 check->fragmented_clusters = result.bfi.fragmented_clusters;
639 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100640 check->compressed_clusters = result.bfi.compressed_clusters;
641 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500642
643 return 0;
644}
645
Kevin Wolfe076f332010-06-29 11:43:13 +0200646/*
647 * Checks an image for consistency. Exit codes:
648 *
Max Reitzd6635c42014-06-02 22:15:21 +0200649 * 0 - Check completed, image is good
650 * 1 - Check not completed because of internal errors
651 * 2 - Check completed, image is corrupted
652 * 3 - Check completed, image has leaked clusters, but is good otherwise
653 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200654 */
aliguori15859692009-04-21 23:11:53 +0000655static int img_check(int argc, char **argv)
656{
657 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500658 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200659 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200660 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000661 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200662 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100663 int flags = BDRV_O_CHECK;
664 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200665 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100666 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000667 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800668 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000669
670 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500671 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200672 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100673
aliguori15859692009-04-21 23:11:53 +0000674 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500675 int option_index = 0;
676 static const struct option long_options[] = {
677 {"help", no_argument, 0, 'h'},
678 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530679 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500680 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000681 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000682 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800683 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500684 {0, 0, 0, 0}
685 };
Fam Zheng335e9932017-05-03 00:35:39 +0800686 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500687 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100688 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000689 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100690 }
aliguori15859692009-04-21 23:11:53 +0000691 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800692 case ':':
693 missing_argument(argv[optind - 1]);
694 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100695 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800696 unrecognized_option(argv[optind - 1]);
697 break;
aliguori15859692009-04-21 23:11:53 +0000698 case 'h':
699 help();
700 break;
701 case 'f':
702 fmt = optarg;
703 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200704 case 'r':
705 flags |= BDRV_O_RDWR;
706
707 if (!strcmp(optarg, "leaks")) {
708 fix = BDRV_FIX_LEAKS;
709 } else if (!strcmp(optarg, "all")) {
710 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
711 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800712 error_exit("Unknown option value for -r "
713 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200714 }
715 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500716 case OPTION_OUTPUT:
717 output = optarg;
718 break;
Max Reitz40055952014-07-22 22:58:42 +0200719 case 'T':
720 cache = optarg;
721 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100722 case 'q':
723 quiet = true;
724 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800725 case 'U':
726 force_share = true;
727 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000728 case OPTION_OBJECT: {
729 QemuOpts *opts;
730 opts = qemu_opts_parse_noisily(&qemu_object_opts,
731 optarg, true);
732 if (!opts) {
733 return 1;
734 }
735 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000736 case OPTION_IMAGE_OPTS:
737 image_opts = true;
738 break;
aliguori15859692009-04-21 23:11:53 +0000739 }
740 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200741 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800742 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100743 }
aliguori15859692009-04-21 23:11:53 +0000744 filename = argv[optind++];
745
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500746 if (output && !strcmp(output, "json")) {
747 output_format = OFORMAT_JSON;
748 } else if (output && !strcmp(output, "human")) {
749 output_format = OFORMAT_HUMAN;
750 } else if (output) {
751 error_report("--output must be used with human or json as argument.");
752 return 1;
753 }
754
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000755 if (qemu_opts_foreach(&qemu_object_opts,
756 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200757 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000758 return 1;
759 }
760
Kevin Wolfce099542016-03-15 13:01:04 +0100761 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200762 if (ret < 0) {
763 error_report("Invalid source cache option: %s", cache);
764 return 1;
765 }
766
Fam Zheng335e9932017-05-03 00:35:39 +0800767 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
768 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200769 if (!blk) {
770 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900771 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200772 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500773
774 check = g_new0(ImageCheck, 1);
775 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200776
777 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200778 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200779 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500780 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200781 }
782
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500783 if (check->corruptions_fixed || check->leaks_fixed) {
784 int corruptions_fixed, leaks_fixed;
785
786 leaks_fixed = check->leaks_fixed;
787 corruptions_fixed = check->corruptions_fixed;
788
789 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100790 qprintf(quiet,
791 "The following inconsistencies were found and repaired:\n\n"
792 " %" PRId64 " leaked clusters\n"
793 " %" PRId64 " corruptions\n\n"
794 "Double checking the fixed image now...\n",
795 check->leaks_fixed,
796 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500797 }
798
799 ret = collect_image_check(bs, check, filename, fmt, 0);
800
801 check->leaks_fixed = leaks_fixed;
802 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200803 }
804
Max Reitz832390a2014-10-23 15:29:12 +0200805 if (!ret) {
806 switch (output_format) {
807 case OFORMAT_HUMAN:
808 dump_human_image_check(check, quiet);
809 break;
810 case OFORMAT_JSON:
811 dump_json_image_check(check, quiet);
812 break;
813 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500814 }
815
816 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200817 if (ret) {
818 error_report("Check failed: %s", strerror(-ret));
819 } else {
820 error_report("Check failed");
821 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500822 ret = 1;
823 goto fail;
824 }
825
826 if (check->corruptions) {
827 ret = 2;
828 } else if (check->leaks) {
829 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200830 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500831 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000832 }
833
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500834fail:
835 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200836 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500837 return ret;
aliguori15859692009-04-21 23:11:53 +0000838}
839
Max Reitzd4a32382014-10-24 15:57:37 +0200840typedef struct CommonBlockJobCBInfo {
841 BlockDriverState *bs;
842 Error **errp;
843} CommonBlockJobCBInfo;
844
845static void common_block_job_cb(void *opaque, int ret)
846{
847 CommonBlockJobCBInfo *cbi = opaque;
848
849 if (ret < 0) {
850 error_setg_errno(cbi->errp, -ret, "Block job failed");
851 }
Max Reitzd4a32382014-10-24 15:57:37 +0200852}
853
854static void run_block_job(BlockJob *job, Error **errp)
855{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200856 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200857
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100858 /* FIXME In error cases, the job simply goes away and we access a dangling
859 * pointer below. */
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200860 aio_context_acquire(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200861 do {
862 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500863 qemu_progress_print(job->len ?
864 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200865 } while (!job->ready);
866
867 block_job_complete_sync(job, errp);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200868 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200869
870 /* A block job may finish instantaneously without publishing any progress,
871 * so just signal completion here */
872 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200873}
874
bellardea2384d2004-08-01 21:59:26 +0000875static int img_commit(int argc, char **argv)
876{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400877 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200878 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200879 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200880 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100881 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200882 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100883 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200884 Error *local_err = NULL;
885 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000886 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200887 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000888
889 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400890 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200891 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000892 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000893 static const struct option long_options[] = {
894 {"help", no_argument, 0, 'h'},
895 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000896 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000897 {0, 0, 0, 0}
898 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800899 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000900 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100901 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000902 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100903 }
bellardea2384d2004-08-01 21:59:26 +0000904 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800905 case ':':
906 missing_argument(argv[optind - 1]);
907 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100908 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800909 unrecognized_option(argv[optind - 1]);
910 break;
bellardea2384d2004-08-01 21:59:26 +0000911 case 'h':
912 help();
913 break;
914 case 'f':
915 fmt = optarg;
916 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400917 case 't':
918 cache = optarg;
919 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200920 case 'b':
921 base = optarg;
922 /* -b implies -d */
923 drop = true;
924 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200925 case 'd':
926 drop = true;
927 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200928 case 'p':
929 progress = true;
930 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100931 case 'q':
932 quiet = true;
933 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000934 case OPTION_OBJECT: {
935 QemuOpts *opts;
936 opts = qemu_opts_parse_noisily(&qemu_object_opts,
937 optarg, true);
938 if (!opts) {
939 return 1;
940 }
941 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000942 case OPTION_IMAGE_OPTS:
943 image_opts = true;
944 break;
bellardea2384d2004-08-01 21:59:26 +0000945 }
946 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200947
948 /* Progress is not shown in Quiet mode */
949 if (quiet) {
950 progress = false;
951 }
952
Kevin Wolffc11eb22013-08-05 10:53:04 +0200953 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800954 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100955 }
bellardea2384d2004-08-01 21:59:26 +0000956 filename = argv[optind++];
957
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000958 if (qemu_opts_foreach(&qemu_object_opts,
959 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200960 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000961 return 1;
962 }
963
Max Reitz9a86fe42014-10-24 15:57:38 +0200964 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100965 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400966 if (ret < 0) {
967 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100968 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400969 }
970
Fam Zheng335e9932017-05-03 00:35:39 +0800971 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
972 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200973 if (!blk) {
974 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900975 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200976 bs = blk_bs(blk);
977
Max Reitz687fa1d2014-10-24 15:57:39 +0200978 qemu_progress_init(progress, 1.f);
979 qemu_progress_print(0.f, 100);
980
Max Reitz1b22bff2014-10-24 15:57:40 +0200981 if (base) {
982 base_bs = bdrv_find_backing_image(bs, base);
983 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100984 error_setg(&local_err,
985 "Did not find '%s' in the backing chain of '%s'",
986 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200987 goto done;
988 }
989 } else {
990 /* This is different from QMP, which by default uses the deepest file in
991 * the backing chain (i.e., the very base); however, the traditional
992 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200993 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200994 if (!base_bs) {
995 error_setg(&local_err, "Image does not have a backing file");
996 goto done;
997 }
bellardea2384d2004-08-01 21:59:26 +0000998 }
999
Max Reitzd4a32382014-10-24 15:57:37 +02001000 cbi = (CommonBlockJobCBInfo){
1001 .errp = &local_err,
1002 .bs = bs,
1003 };
1004
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001005 aio_context = bdrv_get_aio_context(bs);
1006 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001007 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001008 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001009 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001010 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001011 if (local_err) {
1012 goto done;
1013 }
1014
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001015 /* When the block job completes, the BlockBackend reference will point to
1016 * the old backing file. In order to avoid that the top image is already
1017 * deleted, so we can still empty it afterwards, increment the reference
1018 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001019 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001020 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001021 }
1022
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001023 job = block_job_get("commit");
1024 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001025 if (local_err) {
1026 goto unref_backing;
1027 }
1028
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001029 if (!drop && bs->drv->bdrv_make_empty) {
1030 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001031 if (ret) {
1032 error_setg_errno(&local_err, -ret, "Could not empty %s",
1033 filename);
1034 goto unref_backing;
1035 }
1036 }
1037
1038unref_backing:
1039 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001040 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001041 }
Max Reitzd4a32382014-10-24 15:57:37 +02001042
1043done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001044 qemu_progress_end();
1045
Markus Armbruster26f54e92014-10-07 13:59:04 +02001046 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001047
1048 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001049 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001050 return 1;
1051 }
Max Reitzd4a32382014-10-24 15:57:37 +02001052
1053 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001054 return 0;
1055}
1056
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001057/*
thsf58c7b32008-06-05 21:53:49 +00001058 * Returns true iff the first sector pointed to by 'buf' contains at least
1059 * a non-NUL byte.
1060 *
1061 * 'pnum' is set to the number of sectors (including and immediately following
1062 * the first one) that are known to be in the same allocated/unallocated state.
1063 */
bellardea2384d2004-08-01 21:59:26 +00001064static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1065{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001066 bool is_zero;
1067 int i;
bellardea2384d2004-08-01 21:59:26 +00001068
1069 if (n <= 0) {
1070 *pnum = 0;
1071 return 0;
1072 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001073 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001074 for(i = 1; i < n; i++) {
1075 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001076 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001077 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001078 }
bellardea2384d2004-08-01 21:59:26 +00001079 }
1080 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001081 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001082}
1083
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001084/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001085 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1086 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1087 * breaking up write requests for only small sparse areas.
1088 */
1089static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1090 int min)
1091{
1092 int ret;
1093 int num_checked, num_used;
1094
1095 if (n < min) {
1096 min = n;
1097 }
1098
1099 ret = is_allocated_sectors(buf, n, pnum);
1100 if (!ret) {
1101 return ret;
1102 }
1103
1104 num_used = *pnum;
1105 buf += BDRV_SECTOR_SIZE * *pnum;
1106 n -= *pnum;
1107 num_checked = num_used;
1108
1109 while (n > 0) {
1110 ret = is_allocated_sectors(buf, n, pnum);
1111
1112 buf += BDRV_SECTOR_SIZE * *pnum;
1113 n -= *pnum;
1114 num_checked += *pnum;
1115 if (ret) {
1116 num_used = num_checked;
1117 } else if (*pnum >= min) {
1118 break;
1119 }
1120 }
1121
1122 *pnum = num_used;
1123 return 1;
1124}
1125
1126/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001127 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1128 * buffers matches, non-zero otherwise.
1129 *
1130 * pnum is set to the number of sectors (including and immediately following
1131 * the first one) that are known to have the same comparison result
1132 */
1133static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1134 int *pnum)
1135{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001136 bool res;
1137 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001138
1139 if (n <= 0) {
1140 *pnum = 0;
1141 return 0;
1142 }
1143
1144 res = !!memcmp(buf1, buf2, 512);
1145 for(i = 1; i < n; i++) {
1146 buf1 += 512;
1147 buf2 += 512;
1148
1149 if (!!memcmp(buf1, buf2, 512) != res) {
1150 break;
1151 }
1152 }
1153
1154 *pnum = i;
1155 return res;
1156}
1157
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001158#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001159
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001160static int64_t sectors_to_bytes(int64_t sectors)
1161{
1162 return sectors << BDRV_SECTOR_BITS;
1163}
1164
1165static int64_t sectors_to_process(int64_t total, int64_t from)
1166{
1167 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1168}
1169
1170/*
1171 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1172 *
1173 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1174 * data and negative value on error.
1175 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001176 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001177 * @param sect_num: Number of first sector to check
1178 * @param sect_count: Number of sectors to check
1179 * @param filename: Name of disk file we are checking (logging purpose)
1180 * @param buffer: Allocated buffer for storing read data
1181 * @param quiet: Flag for quiet mode
1182 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001183static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001184 int sect_count, const char *filename,
1185 uint8_t *buffer, bool quiet)
1186{
1187 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001188 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1189 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001190 if (ret < 0) {
1191 error_report("Error while reading offset %" PRId64 " of %s: %s",
1192 sectors_to_bytes(sect_num), filename, strerror(-ret));
1193 return ret;
1194 }
1195 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1196 if (ret || pnum != sect_count) {
1197 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1198 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1199 return 1;
1200 }
1201
1202 return 0;
1203}
1204
1205/*
1206 * Compares two images. Exit codes:
1207 *
1208 * 0 - Images are identical
1209 * 1 - Images differ
1210 * >1 - Error occurred
1211 */
1212static int img_compare(int argc, char **argv)
1213{
Max Reitz40055952014-07-22 22:58:42 +02001214 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001215 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001216 BlockDriverState *bs1, *bs2;
1217 int64_t total_sectors1, total_sectors2;
1218 uint8_t *buf1 = NULL, *buf2 = NULL;
1219 int pnum1, pnum2;
1220 int allocated1, allocated2;
1221 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1222 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001223 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001224 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 int64_t total_sectors;
1226 int64_t sector_num = 0;
1227 int64_t nb_sectors;
1228 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001229 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001230 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001231 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001232
Max Reitz40055952014-07-22 22:58:42 +02001233 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001234 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001235 static const struct option long_options[] = {
1236 {"help", no_argument, 0, 'h'},
1237 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001238 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001239 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001240 {0, 0, 0, 0}
1241 };
Fam Zheng335e9932017-05-03 00:35:39 +08001242 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001243 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001244 if (c == -1) {
1245 break;
1246 }
1247 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001248 case ':':
1249 missing_argument(argv[optind - 1]);
1250 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001251 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001252 unrecognized_option(argv[optind - 1]);
1253 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001254 case 'h':
1255 help();
1256 break;
1257 case 'f':
1258 fmt1 = optarg;
1259 break;
1260 case 'F':
1261 fmt2 = optarg;
1262 break;
Max Reitz40055952014-07-22 22:58:42 +02001263 case 'T':
1264 cache = optarg;
1265 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001266 case 'p':
1267 progress = true;
1268 break;
1269 case 'q':
1270 quiet = true;
1271 break;
1272 case 's':
1273 strict = true;
1274 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001275 case 'U':
1276 force_share = true;
1277 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001278 case OPTION_OBJECT: {
1279 QemuOpts *opts;
1280 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1281 optarg, true);
1282 if (!opts) {
1283 ret = 2;
1284 goto out4;
1285 }
1286 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001287 case OPTION_IMAGE_OPTS:
1288 image_opts = true;
1289 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001290 }
1291 }
1292
1293 /* Progress is not shown in Quiet mode */
1294 if (quiet) {
1295 progress = false;
1296 }
1297
1298
Kevin Wolffc11eb22013-08-05 10:53:04 +02001299 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001300 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001301 }
1302 filename1 = argv[optind++];
1303 filename2 = argv[optind++];
1304
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001305 if (qemu_opts_foreach(&qemu_object_opts,
1306 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001307 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001308 ret = 2;
1309 goto out4;
1310 }
1311
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001312 /* Initialize before goto out */
1313 qemu_progress_init(progress, 2.0);
1314
Kevin Wolfce099542016-03-15 13:01:04 +01001315 flags = 0;
1316 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001317 if (ret < 0) {
1318 error_report("Invalid source cache option: %s", cache);
1319 ret = 2;
1320 goto out3;
1321 }
1322
Fam Zheng335e9932017-05-03 00:35:39 +08001323 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1324 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001325 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001326 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001327 goto out3;
1328 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001329
Fam Zheng335e9932017-05-03 00:35:39 +08001330 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1331 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001332 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001333 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001334 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001335 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001336 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001337 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001338
Max Reitzf1d3cd72015-02-05 13:58:18 -05001339 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1340 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1341 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001342 if (total_sectors1 < 0) {
1343 error_report("Can't get size of %s: %s",
1344 filename1, strerror(-total_sectors1));
1345 ret = 4;
1346 goto out;
1347 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001348 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001349 if (total_sectors2 < 0) {
1350 error_report("Can't get size of %s: %s",
1351 filename2, strerror(-total_sectors2));
1352 ret = 4;
1353 goto out;
1354 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001355 total_sectors = MIN(total_sectors1, total_sectors2);
1356 progress_base = MAX(total_sectors1, total_sectors2);
1357
1358 qemu_progress_print(0, 100);
1359
1360 if (strict && total_sectors1 != total_sectors2) {
1361 ret = 1;
1362 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1363 goto out;
1364 }
1365
1366 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001367 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001368 BlockDriverState *file;
1369
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001370 nb_sectors = sectors_to_process(total_sectors, sector_num);
1371 if (nb_sectors <= 0) {
1372 break;
1373 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001374 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1375 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001376 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001377 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001378 ret = 3;
1379 error_report("Sector allocation test failed for %s", filename1);
1380 goto out;
1381 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001382 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001383
Fam Zheng25ad8e62016-01-13 16:37:41 +08001384 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1385 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001386 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001387 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001388 ret = 3;
1389 error_report("Sector allocation test failed for %s", filename2);
1390 goto out;
1391 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001392 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1393 if (pnum1) {
1394 nb_sectors = MIN(nb_sectors, pnum1);
1395 }
1396 if (pnum2) {
1397 nb_sectors = MIN(nb_sectors, pnum2);
1398 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001399
Fam Zheng25ad8e62016-01-13 16:37:41 +08001400 if (strict) {
1401 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1402 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1403 ret = 1;
1404 qprintf(quiet, "Strict mode: Offset %" PRId64
1405 " block status mismatch!\n",
1406 sectors_to_bytes(sector_num));
1407 goto out;
1408 }
1409 }
1410 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1411 nb_sectors = MIN(pnum1, pnum2);
1412 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001414 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1415 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001416 if (ret < 0) {
1417 error_report("Error while reading offset %" PRId64 " of %s:"
1418 " %s", sectors_to_bytes(sector_num), filename1,
1419 strerror(-ret));
1420 ret = 4;
1421 goto out;
1422 }
Eric Blake91669202016-05-06 10:26:43 -06001423 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1424 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001425 if (ret < 0) {
1426 error_report("Error while reading offset %" PRId64
1427 " of %s: %s", sectors_to_bytes(sector_num),
1428 filename2, strerror(-ret));
1429 ret = 4;
1430 goto out;
1431 }
1432 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1433 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001434 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1435 sectors_to_bytes(
1436 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001437 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001438 goto out;
1439 }
1440 }
1441 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001442
1443 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001444 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001445 filename1, buf1, quiet);
1446 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001447 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001448 filename2, buf1, quiet);
1449 }
1450 if (ret) {
1451 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001452 error_report("Error while reading offset %" PRId64 ": %s",
1453 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001454 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001455 }
1456 goto out;
1457 }
1458 }
1459 sector_num += nb_sectors;
1460 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1461 }
1462
1463 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001464 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465 int64_t total_sectors_over;
1466 const char *filename_over;
1467
1468 qprintf(quiet, "Warning: Image size mismatch!\n");
1469 if (total_sectors1 > total_sectors2) {
1470 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001471 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001472 filename_over = filename1;
1473 } else {
1474 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001475 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 filename_over = filename2;
1477 }
1478
1479 for (;;) {
1480 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1481 if (nb_sectors <= 0) {
1482 break;
1483 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001484 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001485 nb_sectors, &pnum);
1486 if (ret < 0) {
1487 ret = 3;
1488 error_report("Sector allocation test failed for %s",
1489 filename_over);
1490 goto out;
1491
1492 }
1493 nb_sectors = pnum;
1494 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001495 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001496 filename_over, buf1, quiet);
1497 if (ret) {
1498 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001499 error_report("Error while reading offset %" PRId64
1500 " of %s: %s", sectors_to_bytes(sector_num),
1501 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001502 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001503 }
1504 goto out;
1505 }
1506 }
1507 sector_num += nb_sectors;
1508 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1509 }
1510 }
1511
1512 qprintf(quiet, "Images are identical.\n");
1513 ret = 0;
1514
1515out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001516 qemu_vfree(buf1);
1517 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001518 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001519out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001520 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001521out3:
1522 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001523out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001524 return ret;
1525}
1526
Kevin Wolf690c7302015-03-19 13:33:32 +01001527enum ImgConvertBlockStatus {
1528 BLK_DATA,
1529 BLK_ZERO,
1530 BLK_BACKING_FILE,
1531};
1532
Peter Lieven2d9187b2017-02-28 13:40:07 +01001533#define MAX_COROUTINES 16
1534
Kevin Wolf690c7302015-03-19 13:33:32 +01001535typedef struct ImgConvertState {
1536 BlockBackend **src;
1537 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001538 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001539 int64_t total_sectors;
1540 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001541 int64_t allocated_done;
1542 int64_t sector_num;
1543 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001544 enum ImgConvertBlockStatus status;
1545 int64_t sector_next_status;
1546 BlockBackend *target;
1547 bool has_zero_init;
1548 bool compressed;
1549 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001550 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001551 int min_sparse;
1552 size_t cluster_sectors;
1553 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001554 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001555 int running_coroutines;
1556 Coroutine *co[MAX_COROUTINES];
1557 int64_t wait_sector_num[MAX_COROUTINES];
1558 CoMutex lock;
1559 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001560} ImgConvertState;
1561
Peter Lieven2d9187b2017-02-28 13:40:07 +01001562static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1563 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001564{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001565 *src_cur = 0;
1566 *src_cur_offset = 0;
1567 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1568 *src_cur_offset += s->src_sectors[*src_cur];
1569 (*src_cur)++;
1570 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001571 }
1572}
1573
1574static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1575{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001576 int64_t ret, src_cur_offset;
1577 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001578
Peter Lieven2d9187b2017-02-28 13:40:07 +01001579 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001580
1581 assert(s->total_sectors > sector_num);
1582 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1583
1584 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001585 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001586 if (s->target_has_backing) {
1587 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1588 sector_num - src_cur_offset,
1589 n, &n, &file);
1590 } else {
1591 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1592 sector_num - src_cur_offset,
1593 n, &n, &file);
1594 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001595 if (ret < 0) {
1596 return ret;
1597 }
1598
1599 if (ret & BDRV_BLOCK_ZERO) {
1600 s->status = BLK_ZERO;
1601 } else if (ret & BDRV_BLOCK_DATA) {
1602 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001603 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001604 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001605 }
1606
1607 s->sector_next_status = sector_num + n;
1608 }
1609
1610 n = MIN(n, s->sector_next_status - sector_num);
1611 if (s->status == BLK_DATA) {
1612 n = MIN(n, s->buf_sectors);
1613 }
1614
1615 /* We need to write complete clusters for compressed images, so if an
1616 * unallocated area is shorter than that, we must consider the whole
1617 * cluster allocated. */
1618 if (s->compressed) {
1619 if (n < s->cluster_sectors) {
1620 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1621 s->status = BLK_DATA;
1622 } else {
1623 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1624 }
1625 }
1626
1627 return n;
1628}
1629
Peter Lieven2d9187b2017-02-28 13:40:07 +01001630static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1631 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001632{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001633 int n, ret;
1634 QEMUIOVector qiov;
1635 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001636
Kevin Wolf690c7302015-03-19 13:33:32 +01001637 assert(nb_sectors <= s->buf_sectors);
1638 while (nb_sectors > 0) {
1639 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001640 int src_cur;
1641 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001642
1643 /* In the case of compression with multiple source files, we can get a
1644 * nb_sectors that spreads into the next part. So we must be able to
1645 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001646 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1647 blk = s->src[src_cur];
1648 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001649
Peter Lieven2d9187b2017-02-28 13:40:07 +01001650 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1651 iov.iov_base = buf;
1652 iov.iov_len = n << BDRV_SECTOR_BITS;
1653 qemu_iovec_init_external(&qiov, &iov, 1);
1654
1655 ret = blk_co_preadv(
1656 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1657 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001658 if (ret < 0) {
1659 return ret;
1660 }
1661
1662 sector_num += n;
1663 nb_sectors -= n;
1664 buf += n * BDRV_SECTOR_SIZE;
1665 }
1666
1667 return 0;
1668}
1669
Peter Lieven2d9187b2017-02-28 13:40:07 +01001670
1671static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1672 int nb_sectors, uint8_t *buf,
1673 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001674{
1675 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001676 QEMUIOVector qiov;
1677 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001678
1679 while (nb_sectors > 0) {
1680 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001681 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1682
Peter Lieven2d9187b2017-02-28 13:40:07 +01001683 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001684 case BLK_BACKING_FILE:
1685 /* If we have a backing file, leave clusters unallocated that are
1686 * unallocated in the source image, so that the backing file is
1687 * visible at the respective offset. */
1688 assert(s->target_has_backing);
1689 break;
1690
1691 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001692 /* If we're told to keep the target fully allocated (-S 0) or there
1693 * is real non-zero data, we must write it. Otherwise we can treat
1694 * it as zero sectors.
1695 * Compressed clusters need to be written as a whole, so in that
1696 * case we can only save the write if the buffer is completely
1697 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001698 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001699 (!s->compressed &&
1700 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1701 (s->compressed &&
1702 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001703 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001704 iov.iov_base = buf;
1705 iov.iov_len = n << BDRV_SECTOR_BITS;
1706 qemu_iovec_init_external(&qiov, &iov, 1);
1707
1708 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001709 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001710 if (ret < 0) {
1711 return ret;
1712 }
1713 break;
1714 }
1715 /* fall-through */
1716
1717 case BLK_ZERO:
1718 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001719 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001720 break;
1721 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001722 ret = blk_co_pwrite_zeroes(s->target,
1723 sector_num << BDRV_SECTOR_BITS,
1724 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001725 if (ret < 0) {
1726 return ret;
1727 }
1728 break;
1729 }
1730
1731 sector_num += n;
1732 nb_sectors -= n;
1733 buf += n * BDRV_SECTOR_SIZE;
1734 }
1735
1736 return 0;
1737}
1738
Peter Lieven2d9187b2017-02-28 13:40:07 +01001739static void coroutine_fn convert_co_do_copy(void *opaque)
1740{
1741 ImgConvertState *s = opaque;
1742 uint8_t *buf = NULL;
1743 int ret, i;
1744 int index = -1;
1745
1746 for (i = 0; i < s->num_coroutines; i++) {
1747 if (s->co[i] == qemu_coroutine_self()) {
1748 index = i;
1749 break;
1750 }
1751 }
1752 assert(index >= 0);
1753
1754 s->running_coroutines++;
1755 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1756
1757 while (1) {
1758 int n;
1759 int64_t sector_num;
1760 enum ImgConvertBlockStatus status;
1761
1762 qemu_co_mutex_lock(&s->lock);
1763 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1764 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001765 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001766 }
1767 n = convert_iteration_sectors(s, s->sector_num);
1768 if (n < 0) {
1769 qemu_co_mutex_unlock(&s->lock);
1770 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001771 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001772 }
1773 /* save current sector and allocation status to local variables */
1774 sector_num = s->sector_num;
1775 status = s->status;
1776 if (!s->min_sparse && s->status == BLK_ZERO) {
1777 n = MIN(n, s->buf_sectors);
1778 }
1779 /* increment global sector counter so that other coroutines can
1780 * already continue reading beyond this request */
1781 s->sector_num += n;
1782 qemu_co_mutex_unlock(&s->lock);
1783
1784 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1785 s->allocated_done += n;
1786 qemu_progress_print(100.0 * s->allocated_done /
1787 s->allocated_sectors, 0);
1788 }
1789
1790 if (status == BLK_DATA) {
1791 ret = convert_co_read(s, sector_num, n, buf);
1792 if (ret < 0) {
1793 error_report("error while reading sector %" PRId64
1794 ": %s", sector_num, strerror(-ret));
1795 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001796 }
1797 } else if (!s->min_sparse && status == BLK_ZERO) {
1798 status = BLK_DATA;
1799 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1800 }
1801
1802 if (s->wr_in_order) {
1803 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001804 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001805 s->wait_sector_num[index] = sector_num;
1806 qemu_coroutine_yield();
1807 }
1808 s->wait_sector_num[index] = -1;
1809 }
1810
Anton Nefedovb91127e2017-04-26 11:33:15 +03001811 if (s->ret == -EINPROGRESS) {
1812 ret = convert_co_write(s, sector_num, n, buf, status);
1813 if (ret < 0) {
1814 error_report("error while writing sector %" PRId64
1815 ": %s", sector_num, strerror(-ret));
1816 s->ret = ret;
1817 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001818 }
1819
1820 if (s->wr_in_order) {
1821 /* reenter the coroutine that might have waited
1822 * for this write to complete */
1823 s->wr_offs = sector_num + n;
1824 for (i = 0; i < s->num_coroutines; i++) {
1825 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1826 /*
1827 * A -> B -> A cannot occur because A has
1828 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1829 * B will never enter A during this time window.
1830 */
1831 qemu_coroutine_enter(s->co[i]);
1832 break;
1833 }
1834 }
1835 }
1836 }
1837
Peter Lieven2d9187b2017-02-28 13:40:07 +01001838 qemu_vfree(buf);
1839 s->co[index] = NULL;
1840 s->running_coroutines--;
1841 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1842 /* the convert job finished successfully */
1843 s->ret = 0;
1844 }
1845}
1846
Kevin Wolf690c7302015-03-19 13:33:32 +01001847static int convert_do_copy(ImgConvertState *s)
1848{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001849 int ret, i, n;
1850 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001851
1852 /* Check whether we have zero initialisation or can get it efficiently */
1853 s->has_zero_init = s->min_sparse && !s->target_has_backing
1854 ? bdrv_has_zero_init(blk_bs(s->target))
1855 : false;
1856
1857 if (!s->has_zero_init && !s->target_has_backing &&
1858 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1859 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001860 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001861 if (ret == 0) {
1862 s->has_zero_init = true;
1863 }
1864 }
1865
1866 /* Allocate buffer for copied data. For compressed images, only one cluster
1867 * can be copied at a time. */
1868 if (s->compressed) {
1869 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1870 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001871 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001872 }
1873 s->buf_sectors = s->cluster_sectors;
1874 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001875
Kevin Wolf690c7302015-03-19 13:33:32 +01001876 while (sector_num < s->total_sectors) {
1877 n = convert_iteration_sectors(s, sector_num);
1878 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001879 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001880 }
Max Reitzaad15de2016-03-24 23:33:57 +01001881 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1882 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001883 s->allocated_sectors += n;
1884 }
1885 sector_num += n;
1886 }
1887
1888 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001889 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001890 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001891
Peter Lieven2d9187b2017-02-28 13:40:07 +01001892 qemu_co_mutex_init(&s->lock);
1893 for (i = 0; i < s->num_coroutines; i++) {
1894 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1895 s->wait_sector_num[i] = -1;
1896 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001897 }
1898
Anton Nefedovb91127e2017-04-26 11:33:15 +03001899 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001900 main_loop_wait(false);
1901 }
1902
1903 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001904 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001905 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001906 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001907 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001908 }
1909 }
1910
Peter Lieven2d9187b2017-02-28 13:40:07 +01001911 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001912}
1913
bellardea2384d2004-08-01 21:59:26 +00001914static int img_convert(int argc, char **argv)
1915{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001916 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001917 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001918 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1919 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001920 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001921 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001922 BlockDriverState *out_bs;
1923 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001924 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001925 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001926 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001927 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001928 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001929 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001930 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001931
Peter Lieven9fd77f92017-04-21 11:11:55 +02001932 ImgConvertState s = (ImgConvertState) {
1933 /* Need at least 4k of zeros for sparse detection */
1934 .min_sparse = 8,
1935 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1936 .wr_in_order = true,
1937 .num_coroutines = 8,
1938 };
1939
bellardea2384d2004-08-01 21:59:26 +00001940 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001941 static const struct option long_options[] = {
1942 {"help", no_argument, 0, 'h'},
1943 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001944 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001945 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001946 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001947 {0, 0, 0, 0}
1948 };
Fam Zheng335e9932017-05-03 00:35:39 +08001949 c = getopt_long(argc, argv, ":hf:O:B:ce6o:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001950 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001951 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001952 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001953 }
bellardea2384d2004-08-01 21:59:26 +00001954 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001955 case ':':
1956 missing_argument(argv[optind - 1]);
1957 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001958 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001959 unrecognized_option(argv[optind - 1]);
1960 break;
bellardea2384d2004-08-01 21:59:26 +00001961 case 'h':
1962 help();
1963 break;
1964 case 'f':
1965 fmt = optarg;
1966 break;
1967 case 'O':
1968 out_fmt = optarg;
1969 break;
thsf58c7b32008-06-05 21:53:49 +00001970 case 'B':
1971 out_baseimg = optarg;
1972 break;
bellardea2384d2004-08-01 21:59:26 +00001973 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001974 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001975 break;
1976 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001977 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001978 "encryption\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001979 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001980 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001981 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001982 "compat6\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001983 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001984 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001985 if (!is_valid_option_list(optarg)) {
1986 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001987 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001988 }
1989 if (!options) {
1990 options = g_strdup(optarg);
1991 } else {
1992 char *old_options = options;
1993 options = g_strdup_printf("%s,%s", options, optarg);
1994 g_free(old_options);
1995 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001996 break;
edison51ef6722010-09-21 19:58:41 -07001997 case 's':
1998 snapshot_name = optarg;
1999 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002000 case 'l':
2001 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002002 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2003 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002004 if (!sn_opts) {
2005 error_report("Failed in parsing snapshot param '%s'",
2006 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002007 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002008 }
2009 } else {
2010 snapshot_name = optarg;
2011 }
2012 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002013 case 'S':
2014 {
2015 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002016
2017 sval = cvtnum(optarg);
2018 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002019 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002020 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002021 }
2022
Peter Lieven9fd77f92017-04-21 11:11:55 +02002023 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002024 break;
2025 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002026 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002027 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002028 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002029 case 't':
2030 cache = optarg;
2031 break;
Max Reitz40055952014-07-22 22:58:42 +02002032 case 'T':
2033 src_cache = optarg;
2034 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002035 case 'q':
2036 quiet = true;
2037 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002038 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002039 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002040 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002041 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002042 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2043 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002044 error_report("Invalid number of coroutines. Allowed number of"
2045 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002046 goto fail_getopt;
2047 }
2048 break;
2049 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002050 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002051 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002052 case 'U':
2053 force_share = true;
2054 break;
Max Reitz3258b912017-04-26 15:46:47 +02002055 case OPTION_OBJECT: {
2056 QemuOpts *object_opts;
2057 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2058 optarg, true);
2059 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002060 goto fail_getopt;
2061 }
2062 break;
Max Reitz3258b912017-04-26 15:46:47 +02002063 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002064 case OPTION_IMAGE_OPTS:
2065 image_opts = true;
2066 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002067 case OPTION_TARGET_IMAGE_OPTS:
2068 tgt_image_opts = true;
2069 break;
bellardea2384d2004-08-01 21:59:26 +00002070 }
2071 }
ths3b46e622007-09-17 08:09:54 +00002072
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002073 if (!out_fmt && !tgt_image_opts) {
2074 out_fmt = "raw";
2075 }
2076
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002077 if (qemu_opts_foreach(&qemu_object_opts,
2078 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002079 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002080 goto fail_getopt;
2081 }
2082
Peter Lieven9fd77f92017-04-21 11:11:55 +02002083 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002084 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002085 goto fail_getopt;
2086 }
2087
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002088 if (tgt_image_opts && !skip_create) {
2089 error_report("--target-image-opts requires use of -n flag");
2090 goto fail_getopt;
2091 }
2092
Peter Lieven9fd77f92017-04-21 11:11:55 +02002093 s.src_num = argc - optind - 1;
2094 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2095
2096 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002097 if (out_fmt) {
2098 ret = print_block_option_help(out_filename, out_fmt);
2099 goto fail_getopt;
2100 } else {
2101 error_report("Option help requires a format be specified");
2102 goto fail_getopt;
2103 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002104 }
2105
2106 if (s.src_num < 1) {
2107 error_report("Must specify image file name");
2108 goto fail_getopt;
2109 }
2110
2111
Peter Lieven9fd77f92017-04-21 11:11:55 +02002112 /* ret is still -EINVAL until here */
2113 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2114 if (ret < 0) {
2115 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002116 goto fail_getopt;
2117 }
2118
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002119 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002120 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002121 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002122 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002123 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002124 qemu_progress_print(0, 100);
2125
Peter Lieven9fd77f92017-04-21 11:11:55 +02002126 s.src = g_new0(BlockBackend *, s.src_num);
2127 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002128
Peter Lieven9fd77f92017-04-21 11:11:55 +02002129 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2130 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002131 fmt, src_flags, src_writethrough, quiet,
2132 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002133 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002134 ret = -1;
2135 goto out;
2136 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002137 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2138 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002139 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002140 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002141 ret = -1;
2142 goto out;
2143 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002144 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002145 }
bellardea2384d2004-08-01 21:59:26 +00002146
Wenchao Xiaef806542013-12-04 17:10:57 +08002147 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002148 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002149 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2150 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2151 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002152 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002153 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002154 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002155 ret = -1;
2156 goto out;
2157 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002158
Peter Lieven9fd77f92017-04-21 11:11:55 +02002159 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2160 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002161 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002162 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002163 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002164 ret = -1;
2165 goto out;
edison51ef6722010-09-21 19:58:41 -07002166 }
2167
Max Reitz2e024cd2015-02-11 09:58:46 -05002168 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002169 /* Find driver and parse its options */
2170 drv = bdrv_find_format(out_fmt);
2171 if (!drv) {
2172 error_report("Unknown file format '%s'", out_fmt);
2173 ret = -1;
2174 goto out;
2175 }
2176
2177 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2178 if (!proto_drv) {
2179 error_report_err(local_err);
2180 ret = -1;
2181 goto out;
2182 }
2183
Max Reitz2e024cd2015-02-11 09:58:46 -05002184 if (!drv->create_opts) {
2185 error_report("Format driver '%s' does not support image creation",
2186 drv->format_name);
2187 ret = -1;
2188 goto out;
2189 }
Max Reitzf75613c2014-12-02 18:32:46 +01002190
Max Reitz2e024cd2015-02-11 09:58:46 -05002191 if (!proto_drv->create_opts) {
2192 error_report("Protocol driver '%s' does not support image creation",
2193 proto_drv->format_name);
2194 ret = -1;
2195 goto out;
2196 }
Max Reitzf75613c2014-12-02 18:32:46 +01002197
Max Reitz2e024cd2015-02-11 09:58:46 -05002198 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2199 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002200
Max Reitz2e024cd2015-02-11 09:58:46 -05002201 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002202 if (options) {
2203 qemu_opts_do_parse(opts, options, NULL, &local_err);
2204 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002205 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002206 ret = -1;
2207 goto out;
2208 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002209 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002210
Peter Lieven9fd77f92017-04-21 11:11:55 +02002211 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002212 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002213 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2214 if (ret < 0) {
2215 goto out;
2216 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002217 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002218
Kevin Wolfa18953f2010-10-14 15:46:04 +02002219 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002220 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002221 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002222 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002223 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002224 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002225
Max Reitz48758a82017-04-26 15:46:48 +02002226 if (s.src_num > 1 && out_baseimg) {
2227 error_report("Having a backing file for the target makes no sense when "
2228 "concatenating multiple input images");
2229 ret = -1;
2230 goto out;
2231 }
2232
Kevin Wolfefa84d42009-05-18 16:42:12 +02002233 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002234 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002235 bool encryption =
2236 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2237 const char *preallocation =
2238 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002239
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002240 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002241 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002242 ret = -1;
2243 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002244 }
2245
Chunyan Liu83d05212014-06-05 17:20:51 +08002246 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002247 error_report("Compression and encryption not supported at "
2248 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002249 ret = -1;
2250 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002251 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002252
Chunyan Liu83d05212014-06-05 17:20:51 +08002253 if (preallocation
2254 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002255 {
2256 error_report("Compression and preallocation not supported at "
2257 "the same time");
2258 ret = -1;
2259 goto out;
2260 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002261 }
2262
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002263 if (!skip_create) {
2264 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002265 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002266 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002267 error_reportf_err(local_err, "%s: error while converting %s: ",
2268 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002269 goto out;
bellardea2384d2004-08-01 21:59:26 +00002270 }
2271 }
ths3b46e622007-09-17 08:09:54 +00002272
Peter Lieven9fd77f92017-04-21 11:11:55 +02002273 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002274 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002275 if (ret < 0) {
2276 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002277 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002278 }
2279
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002280 if (skip_create) {
2281 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2282 flags, writethrough, quiet, false);
2283 } else {
2284 /* TODO ultimately we should allow --target-image-opts
2285 * to be used even when -n is not given.
2286 * That has to wait for bdrv_create to be improved
2287 * to allow filenames in option syntax
2288 */
2289 s.target = img_open_file(out_filename, out_fmt, flags,
2290 writethrough, quiet, false);
2291 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002292 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002293 ret = -1;
2294 goto out;
2295 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002296 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002297
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002298 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2299 error_report("Compression not supported for this file format");
2300 ret = -1;
2301 goto out;
2302 }
2303
Eric Blake5def6b82016-06-23 16:37:19 -06002304 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002305 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2306 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002307 s.buf_sectors = MIN(32768,
2308 MAX(s.buf_sectors,
2309 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2310 out_bs->bl.pdiscard_alignment >>
2311 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002312
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002313 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002314 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002315 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002316 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002317 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002318 ret = -1;
2319 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002320 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002321 error_report("output file is smaller than input file");
2322 ret = -1;
2323 goto out;
2324 }
2325 }
2326
Peter Lieven24f833c2013-11-27 11:07:07 +01002327 ret = bdrv_get_info(out_bs, &bdi);
2328 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002329 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002330 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002331 goto out;
2332 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002333 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002334 s.compressed = s.compressed || bdi.needs_compressed_writes;
2335 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002336 }
2337
Peter Lieven9fd77f92017-04-21 11:11:55 +02002338 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002339out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002340 if (!ret) {
2341 qemu_progress_print(100, 0);
2342 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002343 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002344 qemu_opts_del(opts);
2345 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002346 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002347 blk_unref(s.target);
2348 if (s.src) {
2349 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2350 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002351 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002352 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002353 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002354 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002355fail_getopt:
2356 g_free(options);
2357
Peter Lieven9fd77f92017-04-21 11:11:55 +02002358 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002359}
2360
bellard57d1a2b2004-08-03 21:15:11 +00002361
bellardfaea38e2006-08-05 21:31:00 +00002362static void dump_snapshots(BlockDriverState *bs)
2363{
2364 QEMUSnapshotInfo *sn_tab, *sn;
2365 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002366
2367 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2368 if (nb_sns <= 0)
2369 return;
2370 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002371 bdrv_snapshot_dump(fprintf, stdout, NULL);
2372 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002373 for(i = 0; i < nb_sns; i++) {
2374 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002375 bdrv_snapshot_dump(fprintf, stdout, sn);
2376 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002377 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002378 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002379}
2380
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002381static void dump_json_image_info_list(ImageInfoList *list)
2382{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002383 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002384 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002385 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002386
2387 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2388 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002389 str = qobject_to_json_pretty(obj);
2390 assert(str != NULL);
2391 printf("%s\n", qstring_get_str(str));
2392 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002393 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002394 QDECREF(str);
2395}
2396
Benoît Canetc054b3f2012-09-05 13:09:02 +02002397static void dump_json_image_info(ImageInfo *info)
2398{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002399 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002400 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002401 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002402
2403 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2404 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002405 str = qobject_to_json_pretty(obj);
2406 assert(str != NULL);
2407 printf("%s\n", qstring_get_str(str));
2408 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002409 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002410 QDECREF(str);
2411}
2412
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002413static void dump_human_image_info_list(ImageInfoList *list)
2414{
2415 ImageInfoList *elem;
2416 bool delim = false;
2417
2418 for (elem = list; elem; elem = elem->next) {
2419 if (delim) {
2420 printf("\n");
2421 }
2422 delim = true;
2423
Wenchao Xia5b917042013-05-25 11:09:45 +08002424 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002425 }
2426}
2427
2428static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2429{
2430 return strcmp(a, b) == 0;
2431}
2432
2433/**
2434 * Open an image file chain and return an ImageInfoList
2435 *
2436 * @filename: topmost image filename
2437 * @fmt: topmost image format (may be NULL to autodetect)
2438 * @chain: true - enumerate entire backing file chain
2439 * false - only topmost image file
2440 *
2441 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2442 * image file. If there was an error a message will have been printed to
2443 * stderr.
2444 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002445static ImageInfoList *collect_image_info_list(bool image_opts,
2446 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002447 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002448 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002449{
2450 ImageInfoList *head = NULL;
2451 ImageInfoList **last = &head;
2452 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002453 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002454
2455 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2456
2457 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002458 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002459 BlockDriverState *bs;
2460 ImageInfo *info;
2461 ImageInfoList *elem;
2462
2463 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2464 error_report("Backing file '%s' creates an infinite loop.",
2465 filename);
2466 goto err;
2467 }
2468 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2469
Max Reitzefaa7c42016-03-16 19:54:38 +01002470 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002471 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2472 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002473 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002474 goto err;
2475 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002476 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002477
Wenchao Xia43526ec2013-06-06 12:27:58 +08002478 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002479 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002480 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002481 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002482 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002483 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002484
2485 elem = g_new0(ImageInfoList, 1);
2486 elem->value = info;
2487 *last = elem;
2488 last = &elem->next;
2489
Markus Armbruster26f54e92014-10-07 13:59:04 +02002490 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002491
2492 filename = fmt = NULL;
2493 if (chain) {
2494 if (info->has_full_backing_filename) {
2495 filename = info->full_backing_filename;
2496 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002497 error_report("Could not determine absolute backing filename,"
2498 " but backing filename '%s' present",
2499 info->backing_filename);
2500 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002501 }
2502 if (info->has_backing_filename_format) {
2503 fmt = info->backing_filename_format;
2504 }
2505 }
2506 }
2507 g_hash_table_destroy(filenames);
2508 return head;
2509
2510err:
2511 qapi_free_ImageInfoList(head);
2512 g_hash_table_destroy(filenames);
2513 return NULL;
2514}
2515
Benoît Canetc054b3f2012-09-05 13:09:02 +02002516static int img_info(int argc, char **argv)
2517{
2518 int c;
2519 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002520 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002521 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002522 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002523 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002524 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002525
bellardea2384d2004-08-01 21:59:26 +00002526 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002527 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002528 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002529 int option_index = 0;
2530 static const struct option long_options[] = {
2531 {"help", no_argument, 0, 'h'},
2532 {"format", required_argument, 0, 'f'},
2533 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002534 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002535 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002536 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002537 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002538 {0, 0, 0, 0}
2539 };
Fam Zheng335e9932017-05-03 00:35:39 +08002540 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002541 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002542 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002543 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002544 }
bellardea2384d2004-08-01 21:59:26 +00002545 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002546 case ':':
2547 missing_argument(argv[optind - 1]);
2548 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002549 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002550 unrecognized_option(argv[optind - 1]);
2551 break;
bellardea2384d2004-08-01 21:59:26 +00002552 case 'h':
2553 help();
2554 break;
2555 case 'f':
2556 fmt = optarg;
2557 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002558 case 'U':
2559 force_share = true;
2560 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002561 case OPTION_OUTPUT:
2562 output = optarg;
2563 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002564 case OPTION_BACKING_CHAIN:
2565 chain = true;
2566 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002567 case OPTION_OBJECT: {
2568 QemuOpts *opts;
2569 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2570 optarg, true);
2571 if (!opts) {
2572 return 1;
2573 }
2574 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002575 case OPTION_IMAGE_OPTS:
2576 image_opts = true;
2577 break;
bellardea2384d2004-08-01 21:59:26 +00002578 }
2579 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002580 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002581 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002582 }
bellardea2384d2004-08-01 21:59:26 +00002583 filename = argv[optind++];
2584
Benoît Canetc054b3f2012-09-05 13:09:02 +02002585 if (output && !strcmp(output, "json")) {
2586 output_format = OFORMAT_JSON;
2587 } else if (output && !strcmp(output, "human")) {
2588 output_format = OFORMAT_HUMAN;
2589 } else if (output) {
2590 error_report("--output must be used with human or json as argument.");
2591 return 1;
2592 }
2593
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002594 if (qemu_opts_foreach(&qemu_object_opts,
2595 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002596 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002597 return 1;
2598 }
2599
Fam Zheng335e9932017-05-03 00:35:39 +08002600 list = collect_image_info_list(image_opts, filename, fmt, chain,
2601 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002602 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002603 return 1;
2604 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002605
Benoît Canetc054b3f2012-09-05 13:09:02 +02002606 switch (output_format) {
2607 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002608 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002609 break;
2610 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002611 if (chain) {
2612 dump_json_image_info_list(list);
2613 } else {
2614 dump_json_image_info(list->value);
2615 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002616 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002617 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002618
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002619 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002620 return 0;
2621}
2622
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002623static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2624 MapEntry *next)
2625{
2626 switch (output_format) {
2627 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002628 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002629 error_report("File contains external, encrypted or compressed clusters.");
2630 exit(1);
2631 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002632 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002633 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002634 e->start, e->length,
2635 e->has_offset ? e->offset : 0,
2636 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002637 }
2638 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2639 * Modify the flags here to allow more coalescing.
2640 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002641 if (next && (!next->data || next->zero)) {
2642 next->data = false;
2643 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002644 }
2645 break;
2646 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002647 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2648 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002649 (e->start == 0 ? "[" : ",\n"),
2650 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002651 e->zero ? "true" : "false",
2652 e->data ? "true" : "false");
2653 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002654 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002655 }
2656 putchar('}');
2657
2658 if (!next) {
2659 printf("]\n");
2660 }
2661 break;
2662 }
2663}
2664
2665static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2666 int nb_sectors, MapEntry *e)
2667{
2668 int64_t ret;
2669 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002670 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002671 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002672
2673 /* As an optimization, we could cache the current range of unallocated
2674 * clusters in each file of the chain, and avoid querying the same
2675 * range repeatedly.
2676 */
2677
2678 depth = 0;
2679 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002680 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2681 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002682 if (ret < 0) {
2683 return ret;
2684 }
2685 assert(nb_sectors);
2686 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2687 break;
2688 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002689 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002690 if (bs == NULL) {
2691 ret = 0;
2692 break;
2693 }
2694
2695 depth++;
2696 }
2697
John Snow28756452016-02-05 13:12:33 -05002698 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2699
2700 *e = (MapEntry) {
2701 .start = sector_num * BDRV_SECTOR_SIZE,
2702 .length = nb_sectors * BDRV_SECTOR_SIZE,
2703 .data = !!(ret & BDRV_BLOCK_DATA),
2704 .zero = !!(ret & BDRV_BLOCK_ZERO),
2705 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2706 .has_offset = has_offset,
2707 .depth = depth,
2708 .has_filename = file && has_offset,
2709 .filename = file && has_offset ? file->filename : NULL,
2710 };
2711
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002712 return 0;
2713}
2714
Fam Zheng16b0d552016-01-26 11:59:02 +08002715static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2716{
2717 if (curr->length == 0) {
2718 return false;
2719 }
2720 if (curr->zero != next->zero ||
2721 curr->data != next->data ||
2722 curr->depth != next->depth ||
2723 curr->has_filename != next->has_filename ||
2724 curr->has_offset != next->has_offset) {
2725 return false;
2726 }
2727 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2728 return false;
2729 }
2730 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2731 return false;
2732 }
2733 return true;
2734}
2735
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002736static int img_map(int argc, char **argv)
2737{
2738 int c;
2739 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002740 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002741 BlockDriverState *bs;
2742 const char *filename, *fmt, *output;
2743 int64_t length;
2744 MapEntry curr = { .length = 0 }, next;
2745 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002746 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002747 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002748
2749 fmt = NULL;
2750 output = NULL;
2751 for (;;) {
2752 int option_index = 0;
2753 static const struct option long_options[] = {
2754 {"help", no_argument, 0, 'h'},
2755 {"format", required_argument, 0, 'f'},
2756 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002757 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002758 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002759 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002760 {0, 0, 0, 0}
2761 };
Fam Zheng335e9932017-05-03 00:35:39 +08002762 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002763 long_options, &option_index);
2764 if (c == -1) {
2765 break;
2766 }
2767 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002768 case ':':
2769 missing_argument(argv[optind - 1]);
2770 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002771 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002772 unrecognized_option(argv[optind - 1]);
2773 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002774 case 'h':
2775 help();
2776 break;
2777 case 'f':
2778 fmt = optarg;
2779 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002780 case 'U':
2781 force_share = true;
2782 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002783 case OPTION_OUTPUT:
2784 output = optarg;
2785 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002786 case OPTION_OBJECT: {
2787 QemuOpts *opts;
2788 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2789 optarg, true);
2790 if (!opts) {
2791 return 1;
2792 }
2793 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002794 case OPTION_IMAGE_OPTS:
2795 image_opts = true;
2796 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002797 }
2798 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002799 if (optind != argc - 1) {
2800 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002801 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002802 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002803
2804 if (output && !strcmp(output, "json")) {
2805 output_format = OFORMAT_JSON;
2806 } else if (output && !strcmp(output, "human")) {
2807 output_format = OFORMAT_HUMAN;
2808 } else if (output) {
2809 error_report("--output must be used with human or json as argument.");
2810 return 1;
2811 }
2812
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002813 if (qemu_opts_foreach(&qemu_object_opts,
2814 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002815 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002816 return 1;
2817 }
2818
Fam Zheng335e9932017-05-03 00:35:39 +08002819 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002820 if (!blk) {
2821 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002822 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002823 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002824
2825 if (output_format == OFORMAT_HUMAN) {
2826 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2827 }
2828
Max Reitzf1d3cd72015-02-05 13:58:18 -05002829 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002830 while (curr.start + curr.length < length) {
2831 int64_t nsectors_left;
2832 int64_t sector_num;
2833 int n;
2834
2835 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2836
2837 /* Probe up to 1 GiB at a time. */
2838 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2839 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2840 ret = get_block_status(bs, sector_num, n, &next);
2841
2842 if (ret < 0) {
2843 error_report("Could not read file metadata: %s", strerror(-ret));
2844 goto out;
2845 }
2846
Fam Zheng16b0d552016-01-26 11:59:02 +08002847 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002848 curr.length += next.length;
2849 continue;
2850 }
2851
2852 if (curr.length > 0) {
2853 dump_map_entry(output_format, &curr, &next);
2854 }
2855 curr = next;
2856 }
2857
2858 dump_map_entry(output_format, &curr, NULL);
2859
2860out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002861 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002862 return ret < 0;
2863}
2864
aliguorif7b4a942009-01-07 17:40:15 +00002865#define SNAPSHOT_LIST 1
2866#define SNAPSHOT_CREATE 2
2867#define SNAPSHOT_APPLY 3
2868#define SNAPSHOT_DELETE 4
2869
Stuart Brady153859b2009-06-07 00:42:17 +01002870static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002871{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002872 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002873 BlockDriverState *bs;
2874 QEMUSnapshotInfo sn;
2875 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002876 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002877 int action = 0;
2878 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002879 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002880 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002881 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002882 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002883
Kevin Wolfce099542016-03-15 13:01:04 +01002884 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002885 /* Parse commandline parameters */
2886 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002887 static const struct option long_options[] = {
2888 {"help", no_argument, 0, 'h'},
2889 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002890 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002891 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002892 {0, 0, 0, 0}
2893 };
Fam Zheng335e9932017-05-03 00:35:39 +08002894 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002895 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002896 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002897 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002898 }
aliguorif7b4a942009-01-07 17:40:15 +00002899 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002900 case ':':
2901 missing_argument(argv[optind - 1]);
2902 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002903 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002904 unrecognized_option(argv[optind - 1]);
2905 break;
aliguorif7b4a942009-01-07 17:40:15 +00002906 case 'h':
2907 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002908 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002909 case 'l':
2910 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002911 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002912 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002913 }
2914 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002915 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002916 break;
2917 case 'a':
2918 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002919 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002920 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002921 }
2922 action = SNAPSHOT_APPLY;
2923 snapshot_name = optarg;
2924 break;
2925 case 'c':
2926 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002927 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002928 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002929 }
2930 action = SNAPSHOT_CREATE;
2931 snapshot_name = optarg;
2932 break;
2933 case 'd':
2934 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002935 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002936 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002937 }
2938 action = SNAPSHOT_DELETE;
2939 snapshot_name = optarg;
2940 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002941 case 'q':
2942 quiet = true;
2943 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002944 case 'U':
2945 force_share = true;
2946 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002947 case OPTION_OBJECT: {
2948 QemuOpts *opts;
2949 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2950 optarg, true);
2951 if (!opts) {
2952 return 1;
2953 }
2954 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002955 case OPTION_IMAGE_OPTS:
2956 image_opts = true;
2957 break;
aliguorif7b4a942009-01-07 17:40:15 +00002958 }
2959 }
2960
Kevin Wolffc11eb22013-08-05 10:53:04 +02002961 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002962 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002963 }
aliguorif7b4a942009-01-07 17:40:15 +00002964 filename = argv[optind++];
2965
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002966 if (qemu_opts_foreach(&qemu_object_opts,
2967 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002968 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002969 return 1;
2970 }
2971
aliguorif7b4a942009-01-07 17:40:15 +00002972 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002973 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2974 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002975 if (!blk) {
2976 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002977 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002978 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002979
2980 /* Perform the requested action */
2981 switch(action) {
2982 case SNAPSHOT_LIST:
2983 dump_snapshots(bs);
2984 break;
2985
2986 case SNAPSHOT_CREATE:
2987 memset(&sn, 0, sizeof(sn));
2988 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2989
2990 qemu_gettimeofday(&tv);
2991 sn.date_sec = tv.tv_sec;
2992 sn.date_nsec = tv.tv_usec * 1000;
2993
2994 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002995 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002996 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002997 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002998 }
aliguorif7b4a942009-01-07 17:40:15 +00002999 break;
3000
3001 case SNAPSHOT_APPLY:
3002 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003003 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003004 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003005 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003006 }
aliguorif7b4a942009-01-07 17:40:15 +00003007 break;
3008
3009 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003010 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003011 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003012 error_reportf_err(err, "Could not delete snapshot '%s': ",
3013 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003014 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003015 }
aliguorif7b4a942009-01-07 17:40:15 +00003016 break;
3017 }
3018
3019 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003020 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003021 if (ret) {
3022 return 1;
3023 }
Stuart Brady153859b2009-06-07 00:42:17 +01003024 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003025}
3026
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003027static int img_rebase(int argc, char **argv)
3028{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003029 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003030 uint8_t *buf_old = NULL;
3031 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003032 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003033 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003034 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3035 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003036 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003037 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003038 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003039 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003040 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003041 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003042 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003043
3044 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003045 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003046 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003047 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003048 out_baseimg = NULL;
3049 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003050 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003051 static const struct option long_options[] = {
3052 {"help", no_argument, 0, 'h'},
3053 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003054 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003055 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003056 {0, 0, 0, 0}
3057 };
Fam Zheng335e9932017-05-03 00:35:39 +08003058 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003059 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003060 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003061 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003062 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003063 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003064 case ':':
3065 missing_argument(argv[optind - 1]);
3066 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003067 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003068 unrecognized_option(argv[optind - 1]);
3069 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003070 case 'h':
3071 help();
3072 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003073 case 'f':
3074 fmt = optarg;
3075 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003076 case 'F':
3077 out_basefmt = optarg;
3078 break;
3079 case 'b':
3080 out_baseimg = optarg;
3081 break;
3082 case 'u':
3083 unsafe = 1;
3084 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003085 case 'p':
3086 progress = 1;
3087 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003088 case 't':
3089 cache = optarg;
3090 break;
Max Reitz40055952014-07-22 22:58:42 +02003091 case 'T':
3092 src_cache = optarg;
3093 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003094 case 'q':
3095 quiet = true;
3096 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003097 case OPTION_OBJECT: {
3098 QemuOpts *opts;
3099 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3100 optarg, true);
3101 if (!opts) {
3102 return 1;
3103 }
3104 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003105 case OPTION_IMAGE_OPTS:
3106 image_opts = true;
3107 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003108 case 'U':
3109 force_share = true;
3110 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003111 }
3112 }
3113
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003114 if (quiet) {
3115 progress = 0;
3116 }
3117
Fam Zhengac1307a2014-04-22 13:36:11 +08003118 if (optind != argc - 1) {
3119 error_exit("Expecting one image file name");
3120 }
3121 if (!unsafe && !out_baseimg) {
3122 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003123 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003124 filename = argv[optind++];
3125
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003126 if (qemu_opts_foreach(&qemu_object_opts,
3127 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003128 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003129 return 1;
3130 }
3131
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003132 qemu_progress_init(progress, 2.0);
3133 qemu_progress_print(0, 100);
3134
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003135 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003136 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003137 if (ret < 0) {
3138 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003139 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003140 }
3141
Kevin Wolfce099542016-03-15 13:01:04 +01003142 src_flags = 0;
3143 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003144 if (ret < 0) {
3145 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003146 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003147 }
3148
Kevin Wolfce099542016-03-15 13:01:04 +01003149 /* The source files are opened read-only, don't care about WCE */
3150 assert((src_flags & BDRV_O_RDWR) == 0);
3151 (void) src_writethrough;
3152
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003153 /*
3154 * Open the images.
3155 *
3156 * Ignore the old backing file for unsafe rebase in case we want to correct
3157 * the reference to a renamed or moved backing file.
3158 */
Fam Zheng335e9932017-05-03 00:35:39 +08003159 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3160 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003161 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003162 ret = -1;
3163 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003164 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003165 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003166
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003167 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003168 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003169 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003170 ret = -1;
3171 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003172 }
3173 }
3174
3175 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003176 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003177 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003178 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003179
Max Reitz644483d2015-02-05 13:58:17 -05003180 if (bs->backing_format[0] != '\0') {
3181 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003182 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003183 }
3184
Fam Zheng335e9932017-05-03 00:35:39 +08003185 if (force_share) {
3186 if (!options) {
3187 options = qdict_new();
3188 }
3189 qdict_put(options, BDRV_OPT_FORCE_SHARE,
3190 qbool_from_bool(true));
3191 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003192 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003193 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003194 options, src_flags, &local_err);
3195 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003196 error_reportf_err(local_err,
3197 "Could not open old backing file '%s': ",
3198 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003199 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003200 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003201 }
Max Reitz644483d2015-02-05 13:58:17 -05003202
Alex Bligha6166732012-10-16 13:46:18 +01003203 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003204 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003205 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003206 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003207 }
3208 if (force_share) {
3209 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003210 }
3211
Max Reitzefaa7c42016-03-16 19:54:38 +01003212 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003213 options, src_flags, &local_err);
3214 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003215 error_reportf_err(local_err,
3216 "Could not open new backing file '%s': ",
3217 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003218 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003219 goto out;
3220 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003221 }
3222 }
3223
3224 /*
3225 * Check each unallocated cluster in the COW file. If it is unallocated,
3226 * accesses go to the backing file. We must therefore compare this cluster
3227 * in the old and new backing file, and if they differ we need to copy it
3228 * from the old backing file into the COW file.
3229 *
3230 * If qemu-img crashes during this step, no harm is done. The content of
3231 * the image is the same as the original one at any time.
3232 */
3233 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003234 int64_t num_sectors;
3235 int64_t old_backing_num_sectors;
3236 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003237 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003238 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003239 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003240
Max Reitzf1d3cd72015-02-05 13:58:18 -05003241 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3242 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003243
Max Reitzf1d3cd72015-02-05 13:58:18 -05003244 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003245 if (num_sectors < 0) {
3246 error_report("Could not get size of '%s': %s",
3247 filename, strerror(-num_sectors));
3248 ret = -1;
3249 goto out;
3250 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003251 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003252 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003253 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003254
3255 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3256 error_report("Could not get size of '%s': %s",
3257 backing_name, strerror(-old_backing_num_sectors));
3258 ret = -1;
3259 goto out;
3260 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003261 if (blk_new_backing) {
3262 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003263 if (new_backing_num_sectors < 0) {
3264 error_report("Could not get size of '%s': %s",
3265 out_baseimg, strerror(-new_backing_num_sectors));
3266 ret = -1;
3267 goto out;
3268 }
Alex Bligha6166732012-10-16 13:46:18 +01003269 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003270
Kevin Wolf1f710492012-10-12 14:29:18 +02003271 if (num_sectors != 0) {
3272 local_progress = (float)100 /
3273 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3274 }
3275
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003276 for (sector = 0; sector < num_sectors; sector += n) {
3277
3278 /* How many sectors can we handle with the next read? */
3279 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3280 n = (IO_BUF_SIZE / 512);
3281 } else {
3282 n = num_sectors - sector;
3283 }
3284
3285 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003286 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003287 if (ret < 0) {
3288 error_report("error while reading image metadata: %s",
3289 strerror(-ret));
3290 goto out;
3291 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003292 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003293 continue;
3294 }
3295
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003296 /*
3297 * Read old and new backing file and take into consideration that
3298 * backing files may be smaller than the COW image.
3299 */
3300 if (sector >= old_backing_num_sectors) {
3301 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3302 } else {
3303 if (sector + n > old_backing_num_sectors) {
3304 n = old_backing_num_sectors - sector;
3305 }
3306
Eric Blake91669202016-05-06 10:26:43 -06003307 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3308 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003309 if (ret < 0) {
3310 error_report("error while reading from old backing file");
3311 goto out;
3312 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003313 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003314
Max Reitzf1d3cd72015-02-05 13:58:18 -05003315 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003316 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3317 } else {
3318 if (sector + n > new_backing_num_sectors) {
3319 n = new_backing_num_sectors - sector;
3320 }
3321
Eric Blake91669202016-05-06 10:26:43 -06003322 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3323 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003324 if (ret < 0) {
3325 error_report("error while reading from new backing file");
3326 goto out;
3327 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003328 }
3329
3330 /* If they differ, we need to write to the COW file */
3331 uint64_t written = 0;
3332
3333 while (written < n) {
3334 int pnum;
3335
3336 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003337 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003338 {
Eric Blake91669202016-05-06 10:26:43 -06003339 ret = blk_pwrite(blk,
3340 (sector + written) << BDRV_SECTOR_BITS,
3341 buf_old + written * 512,
3342 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003343 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003344 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003345 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003346 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003347 }
3348 }
3349
3350 written += pnum;
3351 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003352 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003353 }
3354 }
3355
3356 /*
3357 * Change the backing file. All clusters that are different from the old
3358 * backing file are overwritten in the COW file now, so the visible content
3359 * doesn't change when we switch the backing file.
3360 */
Alex Bligha6166732012-10-16 13:46:18 +01003361 if (out_baseimg && *out_baseimg) {
3362 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3363 } else {
3364 ret = bdrv_change_backing_file(bs, NULL, NULL);
3365 }
3366
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003367 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003368 error_report("Could not change the backing file to '%s': No "
3369 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003370 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003371 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003372 out_baseimg, strerror(-ret));
3373 }
3374
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003375 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003376 /*
3377 * TODO At this point it is possible to check if any clusters that are
3378 * allocated in the COW file are the same in the backing file. If so, they
3379 * could be dropped from the COW file. Don't do this before switching the
3380 * backing file, in case of a crash this would lead to corruption.
3381 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003382out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003383 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003384 /* Cleanup */
3385 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003386 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003387 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003388 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003389 qemu_vfree(buf_old);
3390 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003391
Markus Armbruster26f54e92014-10-07 13:59:04 +02003392 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003393 if (ret) {
3394 return 1;
3395 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003396 return 0;
3397}
3398
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003399static int img_resize(int argc, char **argv)
3400{
Markus Armbruster6750e792015-02-12 17:43:08 +01003401 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003402 int c, ret, relative;
3403 const char *filename, *fmt, *size;
3404 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003405 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003406 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003407 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003408
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003409 static QemuOptsList resize_options = {
3410 .name = "resize_options",
3411 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3412 .desc = {
3413 {
3414 .name = BLOCK_OPT_SIZE,
3415 .type = QEMU_OPT_SIZE,
3416 .help = "Virtual disk size"
3417 }, {
3418 /* end of list */
3419 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003420 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003421 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003422 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003423
Kevin Wolfe80fec72011-04-29 10:58:12 +02003424 /* Remove size from argv manually so that negative numbers are not treated
3425 * as options by getopt. */
3426 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003427 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003428 return 1;
3429 }
3430
3431 size = argv[--argc];
3432
3433 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003434 fmt = NULL;
3435 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003436 static const struct option long_options[] = {
3437 {"help", no_argument, 0, 'h'},
3438 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003439 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003440 {0, 0, 0, 0}
3441 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003442 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003443 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003444 if (c == -1) {
3445 break;
3446 }
3447 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003448 case ':':
3449 missing_argument(argv[optind - 1]);
3450 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003451 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003452 unrecognized_option(argv[optind - 1]);
3453 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003454 case 'h':
3455 help();
3456 break;
3457 case 'f':
3458 fmt = optarg;
3459 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003460 case 'q':
3461 quiet = true;
3462 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003463 case OPTION_OBJECT: {
3464 QemuOpts *opts;
3465 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3466 optarg, true);
3467 if (!opts) {
3468 return 1;
3469 }
3470 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003471 case OPTION_IMAGE_OPTS:
3472 image_opts = true;
3473 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003474 }
3475 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003476 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003477 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003478 }
3479 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003480
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003481 if (qemu_opts_foreach(&qemu_object_opts,
3482 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003483 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003484 return 1;
3485 }
3486
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003487 /* Choose grow, shrink, or absolute resize mode */
3488 switch (size[0]) {
3489 case '+':
3490 relative = 1;
3491 size++;
3492 break;
3493 case '-':
3494 relative = -1;
3495 size++;
3496 break;
3497 default:
3498 relative = 0;
3499 break;
3500 }
3501
3502 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003503 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003504 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003505 if (err) {
3506 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003507 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003508 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003509 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003510 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003511 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3512 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003513
Max Reitzefaa7c42016-03-16 19:54:38 +01003514 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003515 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3516 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003517 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003518 ret = -1;
3519 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003520 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003521
3522 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003523 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003524 } else {
3525 total_size = n;
3526 }
3527 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003528 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003529 ret = -1;
3530 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003531 }
3532
Max Reitzed3d2ec2017-03-28 22:51:27 +02003533 ret = blk_truncate(blk, total_size, &err);
3534 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003535 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003536 } else {
3537 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003538 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003539out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003540 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003541 if (ret) {
3542 return 1;
3543 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003544 return 0;
3545}
3546
Max Reitz76a3a342014-10-27 11:12:51 +01003547static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003548 int64_t offset, int64_t total_work_size,
3549 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003550{
3551 qemu_progress_print(100.f * offset / total_work_size, 0);
3552}
3553
Max Reitz6f176b42013-09-03 10:09:50 +02003554static int img_amend(int argc, char **argv)
3555{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003556 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003557 int c, ret = 0;
3558 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003559 QemuOptsList *create_opts = NULL;
3560 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003561 const char *fmt = NULL, *filename, *cache;
3562 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003563 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003564 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003565 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003566 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003567 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003568
Max Reitzbd39e6e2014-07-22 22:58:43 +02003569 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003570 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003571 static const struct option long_options[] = {
3572 {"help", no_argument, 0, 'h'},
3573 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003574 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003575 {0, 0, 0, 0}
3576 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003577 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003578 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003579 if (c == -1) {
3580 break;
3581 }
3582
3583 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003584 case ':':
3585 missing_argument(argv[optind - 1]);
3586 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003587 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003588 unrecognized_option(argv[optind - 1]);
3589 break;
3590 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003591 help();
3592 break;
3593 case 'o':
3594 if (!is_valid_option_list(optarg)) {
3595 error_report("Invalid option list: %s", optarg);
3596 ret = -1;
3597 goto out_no_progress;
3598 }
3599 if (!options) {
3600 options = g_strdup(optarg);
3601 } else {
3602 char *old_options = options;
3603 options = g_strdup_printf("%s,%s", options, optarg);
3604 g_free(old_options);
3605 }
3606 break;
3607 case 'f':
3608 fmt = optarg;
3609 break;
3610 case 't':
3611 cache = optarg;
3612 break;
3613 case 'p':
3614 progress = true;
3615 break;
3616 case 'q':
3617 quiet = true;
3618 break;
3619 case OPTION_OBJECT:
3620 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3621 optarg, true);
3622 if (!opts) {
3623 ret = -1;
3624 goto out_no_progress;
3625 }
3626 break;
3627 case OPTION_IMAGE_OPTS:
3628 image_opts = true;
3629 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003630 }
3631 }
3632
Max Reitz6f176b42013-09-03 10:09:50 +02003633 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003634 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003635 }
3636
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003637 if (qemu_opts_foreach(&qemu_object_opts,
3638 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003639 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003640 ret = -1;
3641 goto out_no_progress;
3642 }
3643
Max Reitz76a3a342014-10-27 11:12:51 +01003644 if (quiet) {
3645 progress = false;
3646 }
3647 qemu_progress_init(progress, 1.0);
3648
Kevin Wolfa283cb62014-02-21 16:24:07 +01003649 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3650 if (fmt && has_help_option(options)) {
3651 /* If a format is explicitly specified (and possibly no filename is
3652 * given), print option help here */
3653 ret = print_block_option_help(filename, fmt);
3654 goto out;
3655 }
3656
3657 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003658 error_report("Expecting one image file name");
3659 ret = -1;
3660 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003661 }
Max Reitz6f176b42013-09-03 10:09:50 +02003662
Kevin Wolfce099542016-03-15 13:01:04 +01003663 flags = BDRV_O_RDWR;
3664 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003665 if (ret < 0) {
3666 error_report("Invalid cache option: %s", cache);
3667 goto out;
3668 }
3669
Fam Zheng335e9932017-05-03 00:35:39 +08003670 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3671 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003672 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003673 ret = -1;
3674 goto out;
3675 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003676 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003677
3678 fmt = bs->drv->format_name;
3679
Kevin Wolf626f84f2014-02-21 16:24:06 +01003680 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003681 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003682 ret = print_block_option_help(filename, fmt);
3683 goto out;
3684 }
3685
Max Reitzb2439d22014-12-02 18:32:47 +01003686 if (!bs->drv->create_opts) {
3687 error_report("Format driver '%s' does not support any options to amend",
3688 fmt);
3689 ret = -1;
3690 goto out;
3691 }
3692
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003693 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003694 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003695 qemu_opts_do_parse(opts, options, NULL, &err);
3696 if (err) {
3697 error_report_err(err);
3698 ret = -1;
3699 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003700 }
3701
Max Reitz76a3a342014-10-27 11:12:51 +01003702 /* In case the driver does not call amend_status_cb() */
3703 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003704 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003705 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003706 if (ret < 0) {
3707 error_report("Error while amending options: %s", strerror(-ret));
3708 goto out;
3709 }
3710
3711out:
Max Reitz76a3a342014-10-27 11:12:51 +01003712 qemu_progress_end();
3713
Max Reitze814dff2015-08-20 16:00:38 -07003714out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003715 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003716 qemu_opts_del(opts);
3717 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003718 g_free(options);
3719
Max Reitz6f176b42013-09-03 10:09:50 +02003720 if (ret) {
3721 return 1;
3722 }
3723 return 0;
3724}
3725
Kevin Wolfb6133b82014-08-05 14:17:13 +02003726typedef struct BenchData {
3727 BlockBackend *blk;
3728 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003729 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003730 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003731 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003732 int nrreq;
3733 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003734 int flush_interval;
3735 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003736 uint8_t *buf;
3737 QEMUIOVector *qiov;
3738
3739 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003740 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003741 uint64_t offset;
3742} BenchData;
3743
Kevin Wolf55d539c2016-06-03 13:59:41 +02003744static void bench_undrained_flush_cb(void *opaque, int ret)
3745{
3746 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003747 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003748 exit(EXIT_FAILURE);
3749 }
3750}
3751
Kevin Wolfb6133b82014-08-05 14:17:13 +02003752static void bench_cb(void *opaque, int ret)
3753{
3754 BenchData *b = opaque;
3755 BlockAIOCB *acb;
3756
3757 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003758 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003759 exit(EXIT_FAILURE);
3760 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003761
3762 if (b->in_flush) {
3763 /* Just finished a flush with drained queue: Start next requests */
3764 assert(b->in_flight == 0);
3765 b->in_flush = false;
3766 } else if (b->in_flight > 0) {
3767 int remaining = b->n - b->in_flight;
3768
Kevin Wolfb6133b82014-08-05 14:17:13 +02003769 b->n--;
3770 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003771
3772 /* Time for flush? Drain queue if requested, then flush */
3773 if (b->flush_interval && remaining % b->flush_interval == 0) {
3774 if (!b->in_flight || !b->drain_on_flush) {
3775 BlockCompletionFunc *cb;
3776
3777 if (b->drain_on_flush) {
3778 b->in_flush = true;
3779 cb = bench_cb;
3780 } else {
3781 cb = bench_undrained_flush_cb;
3782 }
3783
3784 acb = blk_aio_flush(b->blk, cb, b);
3785 if (!acb) {
3786 error_report("Failed to issue flush request");
3787 exit(EXIT_FAILURE);
3788 }
3789 }
3790 if (b->drain_on_flush) {
3791 return;
3792 }
3793 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003794 }
3795
3796 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003797 int64_t offset = b->offset;
3798 /* blk_aio_* might look for completed I/Os and kick bench_cb
3799 * again, so make sure this operation is counted by in_flight
3800 * and b->offset is ready for the next submission.
3801 */
3802 b->in_flight++;
3803 b->offset += b->step;
3804 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003805 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003806 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003807 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003808 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003809 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003810 if (!acb) {
3811 error_report("Failed to issue request");
3812 exit(EXIT_FAILURE);
3813 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003814 }
3815}
3816
3817static int img_bench(int argc, char **argv)
3818{
3819 int c, ret = 0;
3820 const char *fmt = NULL, *filename;
3821 bool quiet = false;
3822 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003823 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003824 int count = 75000;
3825 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003826 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003827 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003828 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003829 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003830 int flush_interval = 0;
3831 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003832 int64_t image_size;
3833 BlockBackend *blk = NULL;
3834 BenchData data = {};
3835 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003836 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003837 struct timeval t1, t2;
3838 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003839 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003840
3841 for (;;) {
3842 static const struct option long_options[] = {
3843 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003844 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003845 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003846 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003847 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003848 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003849 {0, 0, 0, 0}
3850 };
Fam Zheng335e9932017-05-03 00:35:39 +08003851 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003852 if (c == -1) {
3853 break;
3854 }
3855
3856 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003857 case ':':
3858 missing_argument(argv[optind - 1]);
3859 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003860 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003861 unrecognized_option(argv[optind - 1]);
3862 break;
3863 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003864 help();
3865 break;
3866 case 'c':
3867 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003868 unsigned long res;
3869
3870 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003871 error_report("Invalid request count specified");
3872 return 1;
3873 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003874 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003875 break;
3876 }
3877 case 'd':
3878 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003879 unsigned long res;
3880
3881 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003882 error_report("Invalid queue depth specified");
3883 return 1;
3884 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003885 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003886 break;
3887 }
3888 case 'f':
3889 fmt = optarg;
3890 break;
3891 case 'n':
3892 flags |= BDRV_O_NATIVE_AIO;
3893 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003894 case 'o':
3895 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003896 offset = cvtnum(optarg);
3897 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003898 error_report("Invalid offset specified");
3899 return 1;
3900 }
3901 break;
3902 }
3903 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003904 case 'q':
3905 quiet = true;
3906 break;
3907 case 's':
3908 {
3909 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003910
Markus Armbruster606caa02017-02-21 21:14:04 +01003911 sval = cvtnum(optarg);
3912 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003913 error_report("Invalid buffer size specified");
3914 return 1;
3915 }
3916
3917 bufsize = sval;
3918 break;
3919 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003920 case 'S':
3921 {
3922 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003923
Markus Armbruster606caa02017-02-21 21:14:04 +01003924 sval = cvtnum(optarg);
3925 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003926 error_report("Invalid step size specified");
3927 return 1;
3928 }
3929
3930 step = sval;
3931 break;
3932 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003933 case 't':
3934 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3935 if (ret < 0) {
3936 error_report("Invalid cache mode");
3937 ret = -1;
3938 goto out;
3939 }
3940 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003941 case 'w':
3942 flags |= BDRV_O_RDWR;
3943 is_write = true;
3944 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003945 case 'U':
3946 force_share = true;
3947 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003948 case OPTION_PATTERN:
3949 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003950 unsigned long res;
3951
3952 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003953 error_report("Invalid pattern byte specified");
3954 return 1;
3955 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003956 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003957 break;
3958 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003959 case OPTION_FLUSH_INTERVAL:
3960 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003961 unsigned long res;
3962
3963 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003964 error_report("Invalid flush interval specified");
3965 return 1;
3966 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003967 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003968 break;
3969 }
3970 case OPTION_NO_DRAIN:
3971 drain_on_flush = false;
3972 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003973 case OPTION_IMAGE_OPTS:
3974 image_opts = true;
3975 break;
3976 }
3977 }
3978
3979 if (optind != argc - 1) {
3980 error_exit("Expecting one image file name");
3981 }
3982 filename = argv[argc - 1];
3983
Kevin Wolf55d539c2016-06-03 13:59:41 +02003984 if (!is_write && flush_interval) {
3985 error_report("--flush-interval is only available in write tests");
3986 ret = -1;
3987 goto out;
3988 }
3989 if (flush_interval && flush_interval < depth) {
3990 error_report("Flush interval can't be smaller than depth");
3991 ret = -1;
3992 goto out;
3993 }
3994
Fam Zheng335e9932017-05-03 00:35:39 +08003995 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3996 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003997 if (!blk) {
3998 ret = -1;
3999 goto out;
4000 }
4001
4002 image_size = blk_getlength(blk);
4003 if (image_size < 0) {
4004 ret = image_size;
4005 goto out;
4006 }
4007
4008 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004009 .blk = blk,
4010 .image_size = image_size,
4011 .bufsize = bufsize,
4012 .step = step ?: bufsize,
4013 .nrreq = depth,
4014 .n = count,
4015 .offset = offset,
4016 .write = is_write,
4017 .flush_interval = flush_interval,
4018 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004019 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004020 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004021 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004022 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004023 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004024 if (flush_interval) {
4025 printf("Sending flush every %d requests\n", flush_interval);
4026 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004027
4028 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004029 memset(data.buf, pattern, data.nrreq * data.bufsize);
4030
Kevin Wolfb6133b82014-08-05 14:17:13 +02004031 data.qiov = g_new(QEMUIOVector, data.nrreq);
4032 for (i = 0; i < data.nrreq; i++) {
4033 qemu_iovec_init(&data.qiov[i], 1);
4034 qemu_iovec_add(&data.qiov[i],
4035 data.buf + i * data.bufsize, data.bufsize);
4036 }
4037
4038 gettimeofday(&t1, NULL);
4039 bench_cb(&data, 0);
4040
4041 while (data.n > 0) {
4042 main_loop_wait(false);
4043 }
4044 gettimeofday(&t2, NULL);
4045
4046 printf("Run completed in %3.3f seconds.\n",
4047 (t2.tv_sec - t1.tv_sec)
4048 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4049
4050out:
4051 qemu_vfree(data.buf);
4052 blk_unref(blk);
4053
4054 if (ret) {
4055 return 1;
4056 }
4057 return 0;
4058}
4059
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004060#define C_BS 01
4061#define C_COUNT 02
4062#define C_IF 04
4063#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004064#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004065
4066struct DdInfo {
4067 unsigned int flags;
4068 int64_t count;
4069};
4070
4071struct DdIo {
4072 int bsz; /* Block size */
4073 char *filename;
4074 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004075 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004076};
4077
4078struct DdOpts {
4079 const char *name;
4080 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4081 unsigned int flag;
4082};
4083
4084static int img_dd_bs(const char *arg,
4085 struct DdIo *in, struct DdIo *out,
4086 struct DdInfo *dd)
4087{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004088 int64_t res;
4089
Markus Armbruster606caa02017-02-21 21:14:04 +01004090 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004091
Markus Armbruster606caa02017-02-21 21:14:04 +01004092 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004093 error_report("invalid number: '%s'", arg);
4094 return 1;
4095 }
4096 in->bsz = out->bsz = res;
4097
4098 return 0;
4099}
4100
4101static int img_dd_count(const char *arg,
4102 struct DdIo *in, struct DdIo *out,
4103 struct DdInfo *dd)
4104{
Markus Armbruster606caa02017-02-21 21:14:04 +01004105 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004106
Markus Armbruster606caa02017-02-21 21:14:04 +01004107 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004108 error_report("invalid number: '%s'", arg);
4109 return 1;
4110 }
4111
4112 return 0;
4113}
4114
4115static int img_dd_if(const char *arg,
4116 struct DdIo *in, struct DdIo *out,
4117 struct DdInfo *dd)
4118{
4119 in->filename = g_strdup(arg);
4120
4121 return 0;
4122}
4123
4124static int img_dd_of(const char *arg,
4125 struct DdIo *in, struct DdIo *out,
4126 struct DdInfo *dd)
4127{
4128 out->filename = g_strdup(arg);
4129
4130 return 0;
4131}
4132
Reda Sallahif7c15532016-08-10 16:16:09 +02004133static int img_dd_skip(const char *arg,
4134 struct DdIo *in, struct DdIo *out,
4135 struct DdInfo *dd)
4136{
Markus Armbruster606caa02017-02-21 21:14:04 +01004137 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004138
Markus Armbruster606caa02017-02-21 21:14:04 +01004139 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004140 error_report("invalid number: '%s'", arg);
4141 return 1;
4142 }
4143
4144 return 0;
4145}
4146
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004147static int img_dd(int argc, char **argv)
4148{
4149 int ret = 0;
4150 char *arg = NULL;
4151 char *tmp;
4152 BlockDriver *drv = NULL, *proto_drv = NULL;
4153 BlockBackend *blk1 = NULL, *blk2 = NULL;
4154 QemuOpts *opts = NULL;
4155 QemuOptsList *create_opts = NULL;
4156 Error *local_err = NULL;
4157 bool image_opts = false;
4158 int c, i;
4159 const char *out_fmt = "raw";
4160 const char *fmt = NULL;
4161 int64_t size = 0;
4162 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004163 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004164 struct DdInfo dd = {
4165 .flags = 0,
4166 .count = 0,
4167 };
4168 struct DdIo in = {
4169 .bsz = 512, /* Block size is by default 512 bytes */
4170 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004171 .buf = NULL,
4172 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004173 };
4174 struct DdIo out = {
4175 .bsz = 512,
4176 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004177 .buf = NULL,
4178 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004179 };
4180
4181 const struct DdOpts options[] = {
4182 { "bs", img_dd_bs, C_BS },
4183 { "count", img_dd_count, C_COUNT },
4184 { "if", img_dd_if, C_IF },
4185 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004186 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004187 { NULL, NULL, 0 }
4188 };
4189 const struct option long_options[] = {
4190 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004191 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004192 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004193 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004194 { 0, 0, 0, 0 }
4195 };
4196
Fam Zheng335e9932017-05-03 00:35:39 +08004197 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004198 if (c == EOF) {
4199 break;
4200 }
4201 switch (c) {
4202 case 'O':
4203 out_fmt = optarg;
4204 break;
4205 case 'f':
4206 fmt = optarg;
4207 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004208 case ':':
4209 missing_argument(argv[optind - 1]);
4210 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004211 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004212 unrecognized_option(argv[optind - 1]);
4213 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004214 case 'h':
4215 help();
4216 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004217 case 'U':
4218 force_share = true;
4219 break;
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004220 case OPTION_OBJECT: {
4221 QemuOpts *opts;
4222 opts = qemu_opts_parse_noisily(&qemu_object_opts,
4223 optarg, true);
4224 if (!opts) {
4225 ret = -1;
4226 goto out;
4227 }
4228 } break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004229 case OPTION_IMAGE_OPTS:
4230 image_opts = true;
4231 break;
4232 }
4233 }
4234
4235 for (i = optind; i < argc; i++) {
4236 int j;
4237 arg = g_strdup(argv[i]);
4238
4239 tmp = strchr(arg, '=');
4240 if (tmp == NULL) {
4241 error_report("unrecognized operand %s", arg);
4242 ret = -1;
4243 goto out;
4244 }
4245
4246 *tmp++ = '\0';
4247
4248 for (j = 0; options[j].name != NULL; j++) {
4249 if (!strcmp(arg, options[j].name)) {
4250 break;
4251 }
4252 }
4253 if (options[j].name == NULL) {
4254 error_report("unrecognized operand %s", arg);
4255 ret = -1;
4256 goto out;
4257 }
4258
4259 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4260 ret = -1;
4261 goto out;
4262 }
4263 dd.flags |= options[j].flag;
4264 g_free(arg);
4265 arg = NULL;
4266 }
4267
4268 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4269 error_report("Must specify both input and output files");
4270 ret = -1;
4271 goto out;
4272 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004273
4274 if (qemu_opts_foreach(&qemu_object_opts,
4275 user_creatable_add_opts_foreach,
4276 NULL, NULL)) {
4277 ret = -1;
4278 goto out;
4279 }
4280
Fam Zheng335e9932017-05-03 00:35:39 +08004281 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4282 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004283
4284 if (!blk1) {
4285 ret = -1;
4286 goto out;
4287 }
4288
4289 drv = bdrv_find_format(out_fmt);
4290 if (!drv) {
4291 error_report("Unknown file format");
4292 ret = -1;
4293 goto out;
4294 }
4295 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4296
4297 if (!proto_drv) {
4298 error_report_err(local_err);
4299 ret = -1;
4300 goto out;
4301 }
4302 if (!drv->create_opts) {
4303 error_report("Format driver '%s' does not support image creation",
4304 drv->format_name);
4305 ret = -1;
4306 goto out;
4307 }
4308 if (!proto_drv->create_opts) {
4309 error_report("Protocol driver '%s' does not support image creation",
4310 proto_drv->format_name);
4311 ret = -1;
4312 goto out;
4313 }
4314 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4315 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4316
4317 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4318
4319 size = blk_getlength(blk1);
4320 if (size < 0) {
4321 error_report("Failed to get size for '%s'", in.filename);
4322 ret = -1;
4323 goto out;
4324 }
4325
4326 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4327 dd.count * in.bsz < size) {
4328 size = dd.count * in.bsz;
4329 }
4330
Reda Sallahif7c15532016-08-10 16:16:09 +02004331 /* Overflow means the specified offset is beyond input image's size */
4332 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4333 size < in.bsz * in.offset)) {
4334 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4335 } else {
4336 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4337 size - in.bsz * in.offset, &error_abort);
4338 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004339
4340 ret = bdrv_create(drv, out.filename, opts, &local_err);
4341 if (ret < 0) {
4342 error_reportf_err(local_err,
4343 "%s: error while creating output image: ",
4344 out.filename);
4345 ret = -1;
4346 goto out;
4347 }
4348
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004349 /* TODO, we can't honour --image-opts for the target,
4350 * since it needs to be given in a format compatible
4351 * with the bdrv_create() call above which does not
4352 * support image-opts style.
4353 */
4354 blk2 = img_open_file(out.filename, out_fmt, BDRV_O_RDWR,
4355 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004356
4357 if (!blk2) {
4358 ret = -1;
4359 goto out;
4360 }
4361
Reda Sallahif7c15532016-08-10 16:16:09 +02004362 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4363 size < in.offset * in.bsz)) {
4364 /* We give a warning if the skip option is bigger than the input
4365 * size and create an empty output disk image (i.e. like dd(1)).
4366 */
4367 error_report("%s: cannot skip to specified offset", in.filename);
4368 in_pos = size;
4369 } else {
4370 in_pos = in.offset * in.bsz;
4371 }
4372
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004373 in.buf = g_new(uint8_t, in.bsz);
4374
Reda Sallahif7c15532016-08-10 16:16:09 +02004375 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004376 int in_ret, out_ret;
4377
4378 if (in_pos + in.bsz > size) {
4379 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4380 } else {
4381 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4382 }
4383 if (in_ret < 0) {
4384 error_report("error while reading from input image file: %s",
4385 strerror(-in_ret));
4386 ret = -1;
4387 goto out;
4388 }
4389 in_pos += in_ret;
4390
4391 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4392
4393 if (out_ret < 0) {
4394 error_report("error while writing to output image file: %s",
4395 strerror(-out_ret));
4396 ret = -1;
4397 goto out;
4398 }
4399 out_pos += out_ret;
4400 }
4401
4402out:
4403 g_free(arg);
4404 qemu_opts_del(opts);
4405 qemu_opts_free(create_opts);
4406 blk_unref(blk1);
4407 blk_unref(blk2);
4408 g_free(in.filename);
4409 g_free(out.filename);
4410 g_free(in.buf);
4411 g_free(out.buf);
4412
4413 if (ret) {
4414 return 1;
4415 }
4416 return 0;
4417}
4418
Kevin Wolfb6133b82014-08-05 14:17:13 +02004419
Anthony Liguoric227f092009-10-01 16:12:16 -05004420static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004421#define DEF(option, callback, arg_string) \
4422 { option, callback },
4423#include "qemu-img-cmds.h"
4424#undef DEF
4425#undef GEN_DOCS
4426 { NULL, NULL, },
4427};
4428
bellardea2384d2004-08-01 21:59:26 +00004429int main(int argc, char **argv)
4430{
Anthony Liguoric227f092009-10-01 16:12:16 -05004431 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004432 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004433 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004434 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004435 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004436 static const struct option long_options[] = {
4437 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004438 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004439 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004440 {0, 0, 0, 0}
4441 };
bellardea2384d2004-08-01 21:59:26 +00004442
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004443#ifdef CONFIG_POSIX
4444 signal(SIGPIPE, SIG_IGN);
4445#endif
4446
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004447 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004448 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004449 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004450
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004451 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004452 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004453 exit(EXIT_FAILURE);
4454 }
4455
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004456 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004457
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004458 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004459 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004460 if (argc < 2) {
4461 error_exit("Not enough arguments");
4462 }
Stuart Brady153859b2009-06-07 00:42:17 +01004463
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004464 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004465 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004466 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004467
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004468 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004469 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004470 case ':':
4471 missing_argument(argv[optind - 1]);
4472 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004473 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004474 unrecognized_option(argv[optind - 1]);
4475 return 0;
4476 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004477 help();
4478 return 0;
4479 case 'V':
4480 printf(QEMU_IMG_VERSION);
4481 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004482 case 'T':
4483 g_free(trace_file);
4484 trace_file = trace_opt_parse(optarg);
4485 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004486 }
bellardea2384d2004-08-01 21:59:26 +00004487 }
Stuart Brady153859b2009-06-07 00:42:17 +01004488
Denis V. Lunev10985132016-06-17 17:44:13 +03004489 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004490
Denis V. Lunev10985132016-06-17 17:44:13 +03004491 /* reset getopt_long scanning */
4492 argc -= optind;
4493 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004494 return 0;
4495 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004496 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004497 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004498
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004499 if (!trace_init_backends()) {
4500 exit(1);
4501 }
4502 trace_init_file(trace_file);
4503 qemu_set_log(LOG_TRACE);
4504
Denis V. Lunev10985132016-06-17 17:44:13 +03004505 /* find the command */
4506 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4507 if (!strcmp(cmdname, cmd->name)) {
4508 return cmd->handler(argc, argv);
4509 }
4510 }
Jeff Cody7db16892014-04-25 17:02:32 -04004511
Stuart Brady153859b2009-06-07 00:42:17 +01004512 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004513 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004514}