blob: 4dae84a1824e7bb0ffa3af8d779e021285c2645c [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 */
Benoît Canetc054b3f2012-09-05 13:09:02 +020024#include "qapi-visit.h"
25#include "qapi/qmp-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010026#include "qapi/qmp/qjson.h"
pbrookfaf07962007-11-11 02:51:17 +000027#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/option.h"
29#include "qemu/error-report.h"
30#include "qemu/osdep.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block_int.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080033#include "block/qapi.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020034#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000035
Anthony Liguoric227f092009-10-01 16:12:16 -050036typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010037 const char *name;
38 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050039} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010040
Federico Simoncelli8599ea42013-01-28 06:59:47 -050041enum {
42 OPTION_OUTPUT = 256,
43 OPTION_BACKING_CHAIN = 257,
44};
45
46typedef enum OutputFormat {
47 OFORMAT_JSON,
48 OFORMAT_HUMAN,
49} OutputFormat;
50
aurel32137519c2008-11-30 19:12:49 +000051/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010052#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040053#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000054
bellardea2384d2004-08-01 21:59:26 +000055static void format_print(void *opaque, const char *name)
56{
57 printf(" %s", name);
58}
59
Fam Zhengac1307a2014-04-22 13:36:11 +080060static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
61{
62 va_list ap;
63
64 error_printf("qemu-img: ");
65
66 va_start(ap, fmt);
67 error_vprintf(fmt, ap);
68 va_end(ap);
69
70 error_printf("\nTry 'qemu-img --help' for more information\n");
71 exit(EXIT_FAILURE);
72}
73
blueswir1d2c639d2009-01-24 18:19:25 +000074/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080075static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000076{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010077 const char *help_msg =
78 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
malc3f020d72010-02-08 12:04:56 +030079 "usage: qemu-img command [command options]\n"
80 "QEMU disk image utility\n"
81 "\n"
82 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010083#define DEF(option, callback, arg_string) \
84 " " arg_string "\n"
85#include "qemu-img-cmds.h"
86#undef DEF
87#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030088 "\n"
89 "Command parameters:\n"
90 " 'filename' is a disk image filename\n"
91 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -040092 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +080093 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
94 " 'directsync' and 'unsafe' (default for convert)\n"
malc3f020d72010-02-08 12:04:56 +030095 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +020096 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
97 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
98 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +030099 " 'output_filename' is the destination disk image filename\n"
100 " 'output_fmt' is the destination format\n"
101 " 'options' is a comma separated list of format specific options in a\n"
102 " name=value format. Use -o ? for an overview of the options supported by the\n"
103 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800104 " 'snapshot_param' is param used for internal snapshot, format\n"
105 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
106 " '[ID_OR_NAME]'\n"
107 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
108 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300109 " '-c' indicates that target image must be compressed (qcow format only)\n"
110 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
111 " match exactly. The image doesn't need a working backing file before\n"
112 " rebasing in this case (useful for renaming the backing file)\n"
113 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200114 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100115 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200116 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
117 " contain only zeros for qemu-img to create a sparse image during\n"
118 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
119 " unallocated or zero sectors, and the destination image will always be\n"
120 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200121 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100122 " '-n' skips the target volume creation (useful if the volume is created\n"
123 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300124 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200125 "Parameters to check subcommand:\n"
126 " '-r' tries to repair any inconsistencies that are found during the check.\n"
127 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
128 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200129 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200130 "\n"
malc3f020d72010-02-08 12:04:56 +0300131 "Parameters to snapshot subcommand:\n"
132 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
133 " '-a' applies a snapshot (revert disk to saved state)\n"
134 " '-c' creates a snapshot\n"
135 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100136 " '-l' lists all snapshots in the given image\n"
137 "\n"
138 "Parameters to compare subcommand:\n"
139 " '-f' first image format\n"
140 " '-F' second image format\n"
141 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100142
143 printf("%s\nSupported formats:", help_msg);
bellardea2384d2004-08-01 21:59:26 +0000144 bdrv_iterate_format(format_print, NULL);
145 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800146 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000147}
148
Stefan Weil7c30f652013-06-16 17:01:05 +0200149static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100150{
151 int ret = 0;
152 if (!quiet) {
153 va_list args;
154 va_start(args, fmt);
155 ret = vprintf(fmt, args);
156 va_end(args);
157 }
158 return ret;
159}
160
bellardea2384d2004-08-01 21:59:26 +0000161#if defined(WIN32)
162/* XXX: put correct support for win32 */
163static int read_password(char *buf, int buf_size)
164{
165 int c, i;
166 printf("Password: ");
167 fflush(stdout);
168 i = 0;
169 for(;;) {
170 c = getchar();
171 if (c == '\n')
172 break;
173 if (i < (buf_size - 1))
174 buf[i++] = c;
175 }
176 buf[i] = '\0';
177 return 0;
178}
179
180#else
181
182#include <termios.h>
183
184static struct termios oldtty;
185
186static void term_exit(void)
187{
188 tcsetattr (0, TCSANOW, &oldtty);
189}
190
191static void term_init(void)
192{
193 struct termios tty;
194
195 tcgetattr (0, &tty);
196 oldtty = tty;
197
198 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
199 |INLCR|IGNCR|ICRNL|IXON);
200 tty.c_oflag |= OPOST;
201 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
202 tty.c_cflag &= ~(CSIZE|PARENB);
203 tty.c_cflag |= CS8;
204 tty.c_cc[VMIN] = 1;
205 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000206
bellardea2384d2004-08-01 21:59:26 +0000207 tcsetattr (0, TCSANOW, &tty);
208
209 atexit(term_exit);
210}
211
pbrook3f379ab2007-11-11 03:33:13 +0000212static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000213{
214 uint8_t ch;
215 int i, ret;
216
217 printf("password: ");
218 fflush(stdout);
219 term_init();
220 i = 0;
221 for(;;) {
222 ret = read(0, &ch, 1);
223 if (ret == -1) {
224 if (errno == EAGAIN || errno == EINTR) {
225 continue;
226 } else {
227 ret = -1;
228 break;
229 }
230 } else if (ret == 0) {
231 ret = -1;
232 break;
233 } else {
234 if (ch == '\r') {
235 ret = 0;
236 break;
237 }
238 if (i < (buf_size - 1))
239 buf[i++] = ch;
240 }
241 }
242 term_exit();
243 buf[i] = '\0';
244 printf("\n");
245 return ret;
246}
247#endif
248
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100249static int print_block_option_help(const char *filename, const char *fmt)
250{
251 BlockDriver *drv, *proto_drv;
252 QEMUOptionParameter *create_options = NULL;
253
254 /* Find driver and parse its options */
255 drv = bdrv_find_format(fmt);
256 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100257 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100258 return 1;
259 }
260
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100261 create_options = append_option_parameters(create_options,
262 drv->create_options);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100263
264 if (filename) {
265 proto_drv = bdrv_find_protocol(filename, true);
266 if (!proto_drv) {
267 error_report("Unknown protocol '%s'", filename);
268 return 1;
269 }
270 create_options = append_option_parameters(create_options,
271 proto_drv->create_options);
272 }
273
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100274 print_option_help(create_options);
275 free_option_parameters(create_options);
276 return 0;
277}
278
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200279static BlockDriverState *bdrv_new_open(const char *id,
280 const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800281 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100282 int flags,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100283 bool require_io,
284 bool quiet)
bellard75c23802004-08-27 21:28:58 +0000285{
286 BlockDriverState *bs;
287 BlockDriver *drv;
288 char password[256];
Max Reitz34b5d2c2013-09-05 14:45:29 +0200289 Error *local_err = NULL;
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100290 int ret;
bellard75c23802004-08-27 21:28:58 +0000291
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200292 bs = bdrv_new(id, &error_abort);
Kevin Wolfad717132010-12-16 15:37:41 +0100293
bellard75c23802004-08-27 21:28:58 +0000294 if (fmt) {
295 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900296 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100297 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900298 goto fail;
299 }
bellard75c23802004-08-27 21:28:58 +0000300 } else {
301 drv = NULL;
302 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100303
Max Reitzddf56362014-02-18 18:33:06 +0100304 ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100305 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200306 error_report("Could not open '%s': %s", filename,
307 error_get_pretty(local_err));
308 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900309 goto fail;
bellard75c23802004-08-27 21:28:58 +0000310 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100311
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100312 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100313 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900314 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100315 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900316 goto fail;
317 }
318 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100319 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900320 goto fail;
321 }
bellard75c23802004-08-27 21:28:58 +0000322 }
323 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900324fail:
Max Reitzf67503e2014-02-18 18:33:05 +0100325 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900326 return NULL;
bellard75c23802004-08-27 21:28:58 +0000327}
328
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900329static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100330 const char *base_filename,
331 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200332{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200333 if (base_filename) {
334 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100335 error_report("Backing file not supported for file format '%s'",
336 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900337 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200338 }
339 }
340 if (base_fmt) {
341 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100342 error_report("Backing file format not supported for file "
343 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900344 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200345 }
346 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900347 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200348}
349
bellardea2384d2004-08-01 21:59:26 +0000350static int img_create(int argc, char **argv)
351{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200352 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100353 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000354 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000355 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000356 const char *filename;
357 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200358 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200359 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100360 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000361
bellardea2384d2004-08-01 21:59:26 +0000362 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100363 c = getopt(argc, argv, "F:b:f:he6o:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100364 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000365 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100366 }
bellardea2384d2004-08-01 21:59:26 +0000367 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100368 case '?':
bellardea2384d2004-08-01 21:59:26 +0000369 case 'h':
370 help();
371 break;
aliguori9230eaf2009-03-28 17:55:19 +0000372 case 'F':
373 base_fmt = optarg;
374 break;
bellardea2384d2004-08-01 21:59:26 +0000375 case 'b':
376 base_filename = optarg;
377 break;
378 case 'f':
379 fmt = optarg;
380 break;
381 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200382 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100383 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100384 goto fail;
thsd8871c52007-10-24 16:11:42 +0000385 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200386 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100387 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100388 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200389 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100390 if (!is_valid_option_list(optarg)) {
391 error_report("Invalid option list: %s", optarg);
392 goto fail;
393 }
394 if (!options) {
395 options = g_strdup(optarg);
396 } else {
397 char *old_options = options;
398 options = g_strdup_printf("%s,%s", options, optarg);
399 g_free(old_options);
400 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200401 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100402 case 'q':
403 quiet = true;
404 break;
bellardea2384d2004-08-01 21:59:26 +0000405 }
406 }
aliguori9230eaf2009-03-28 17:55:19 +0000407
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900408 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100409 filename = (optind < argc) ? argv[optind] : NULL;
410 if (options && has_help_option(options)) {
411 g_free(options);
412 return print_block_option_help(filename, fmt);
413 }
414
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100415 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800416 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100417 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100418 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900419
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100420 /* Get image size, if specified */
421 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100422 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100423 char *end;
424 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
425 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800426 if (sval == -ERANGE) {
427 error_report("Image size must be less than 8 EiB!");
428 } else {
429 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200430 "G, T, P or E suffixes for ");
431 error_report("kilobytes, megabytes, gigabytes, terabytes, "
432 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800433 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100434 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100435 }
436 img_size = (uint64_t)sval;
437 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200438 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800439 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200440 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100441
Luiz Capitulino9b375252012-11-30 10:52:05 -0200442 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100443 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100444 if (local_err) {
Max Reitzb70d8c22013-09-06 16:51:03 +0200445 error_report("%s: %s", filename, error_get_pretty(local_err));
Luiz Capitulino9b375252012-11-30 10:52:05 -0200446 error_free(local_err);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100447 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900448 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200449
Kevin Wolf77386bf2014-02-21 16:24:04 +0100450 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000451 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100452
453fail:
454 g_free(options);
455 return 1;
bellardea2384d2004-08-01 21:59:26 +0000456}
457
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100458static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500459{
460 Error *errp = NULL;
461 QString *str;
462 QmpOutputVisitor *ov = qmp_output_visitor_new();
463 QObject *obj;
464 visit_type_ImageCheck(qmp_output_get_visitor(ov),
465 &check, NULL, &errp);
466 obj = qmp_output_get_qobject(ov);
467 str = qobject_to_json_pretty(obj);
468 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100469 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500470 qobject_decref(obj);
471 qmp_output_visitor_cleanup(ov);
472 QDECREF(str);
473}
474
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100475static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500476{
477 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100478 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500479 } else {
480 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100481 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
482 "Data may be corrupted, or further writes to the image "
483 "may corrupt it.\n",
484 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500485 }
486
487 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100488 qprintf(quiet,
489 "\n%" PRId64 " leaked clusters were found on the image.\n"
490 "This means waste of disk space, but no harm to data.\n",
491 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500492 }
493
494 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100495 qprintf(quiet,
496 "\n%" PRId64
497 " internal errors have occurred during the check.\n",
498 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500499 }
500 }
501
502 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100503 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
504 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
505 check->allocated_clusters, check->total_clusters,
506 check->allocated_clusters * 100.0 / check->total_clusters,
507 check->fragmented_clusters * 100.0 / check->allocated_clusters,
508 check->compressed_clusters * 100.0 /
509 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500510 }
511
512 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100513 qprintf(quiet,
514 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500515 }
516}
517
518static int collect_image_check(BlockDriverState *bs,
519 ImageCheck *check,
520 const char *filename,
521 const char *fmt,
522 int fix)
523{
524 int ret;
525 BdrvCheckResult result;
526
527 ret = bdrv_check(bs, &result, fix);
528 if (ret < 0) {
529 return ret;
530 }
531
532 check->filename = g_strdup(filename);
533 check->format = g_strdup(bdrv_get_format_name(bs));
534 check->check_errors = result.check_errors;
535 check->corruptions = result.corruptions;
536 check->has_corruptions = result.corruptions != 0;
537 check->leaks = result.leaks;
538 check->has_leaks = result.leaks != 0;
539 check->corruptions_fixed = result.corruptions_fixed;
540 check->has_corruptions_fixed = result.corruptions != 0;
541 check->leaks_fixed = result.leaks_fixed;
542 check->has_leaks_fixed = result.leaks != 0;
543 check->image_end_offset = result.image_end_offset;
544 check->has_image_end_offset = result.image_end_offset != 0;
545 check->total_clusters = result.bfi.total_clusters;
546 check->has_total_clusters = result.bfi.total_clusters != 0;
547 check->allocated_clusters = result.bfi.allocated_clusters;
548 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
549 check->fragmented_clusters = result.bfi.fragmented_clusters;
550 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100551 check->compressed_clusters = result.bfi.compressed_clusters;
552 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500553
554 return 0;
555}
556
Kevin Wolfe076f332010-06-29 11:43:13 +0200557/*
558 * Checks an image for consistency. Exit codes:
559 *
560 * 0 - Check completed, image is good
561 * 1 - Check not completed because of internal errors
562 * 2 - Check completed, image is corrupted
563 * 3 - Check completed, image has leaked clusters, but is good otherwise
564 */
aliguori15859692009-04-21 23:11:53 +0000565static int img_check(int argc, char **argv)
566{
567 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500568 OutputFormat output_format = OFORMAT_HUMAN;
569 const char *filename, *fmt, *output;
aliguori15859692009-04-21 23:11:53 +0000570 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200571 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100572 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500573 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100574 bool quiet = false;
aliguori15859692009-04-21 23:11:53 +0000575
576 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500577 output = NULL;
aliguori15859692009-04-21 23:11:53 +0000578 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500579 int option_index = 0;
580 static const struct option long_options[] = {
581 {"help", no_argument, 0, 'h'},
582 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530583 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500584 {"output", required_argument, 0, OPTION_OUTPUT},
585 {0, 0, 0, 0}
586 };
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100587 c = getopt_long(argc, argv, "f:hr:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500588 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100589 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000590 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100591 }
aliguori15859692009-04-21 23:11:53 +0000592 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100593 case '?':
aliguori15859692009-04-21 23:11:53 +0000594 case 'h':
595 help();
596 break;
597 case 'f':
598 fmt = optarg;
599 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200600 case 'r':
601 flags |= BDRV_O_RDWR;
602
603 if (!strcmp(optarg, "leaks")) {
604 fix = BDRV_FIX_LEAKS;
605 } else if (!strcmp(optarg, "all")) {
606 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
607 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800608 error_exit("Unknown option value for -r "
609 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200610 }
611 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500612 case OPTION_OUTPUT:
613 output = optarg;
614 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100615 case 'q':
616 quiet = true;
617 break;
aliguori15859692009-04-21 23:11:53 +0000618 }
619 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200620 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800621 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100622 }
aliguori15859692009-04-21 23:11:53 +0000623 filename = argv[optind++];
624
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500625 if (output && !strcmp(output, "json")) {
626 output_format = OFORMAT_JSON;
627 } else if (output && !strcmp(output, "human")) {
628 output_format = OFORMAT_HUMAN;
629 } else if (output) {
630 error_report("--output must be used with human or json as argument.");
631 return 1;
632 }
633
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200634 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900635 if (!bs) {
636 return 1;
637 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500638
639 check = g_new0(ImageCheck, 1);
640 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200641
642 if (ret == -ENOTSUP) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500643 if (output_format == OFORMAT_HUMAN) {
644 error_report("This image format does not support checks");
645 }
Peter Lievenfefddf92013-10-24 08:53:34 +0200646 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500647 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200648 }
649
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500650 if (check->corruptions_fixed || check->leaks_fixed) {
651 int corruptions_fixed, leaks_fixed;
652
653 leaks_fixed = check->leaks_fixed;
654 corruptions_fixed = check->corruptions_fixed;
655
656 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100657 qprintf(quiet,
658 "The following inconsistencies were found and repaired:\n\n"
659 " %" PRId64 " leaked clusters\n"
660 " %" PRId64 " corruptions\n\n"
661 "Double checking the fixed image now...\n",
662 check->leaks_fixed,
663 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500664 }
665
666 ret = collect_image_check(bs, check, filename, fmt, 0);
667
668 check->leaks_fixed = leaks_fixed;
669 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200670 }
671
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500672 switch (output_format) {
673 case OFORMAT_HUMAN:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100674 dump_human_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500675 break;
676 case OFORMAT_JSON:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100677 dump_json_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500678 break;
679 }
680
681 if (ret || check->check_errors) {
682 ret = 1;
683 goto fail;
684 }
685
686 if (check->corruptions) {
687 ret = 2;
688 } else if (check->leaks) {
689 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200690 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500691 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000692 }
693
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500694fail:
695 qapi_free_ImageCheck(check);
Fam Zheng4f6fd342013-08-23 09:14:47 +0800696 bdrv_unref(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200697
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500698 return ret;
aliguori15859692009-04-21 23:11:53 +0000699}
700
bellardea2384d2004-08-01 21:59:26 +0000701static int img_commit(int argc, char **argv)
702{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400703 int c, ret, flags;
704 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000705 BlockDriverState *bs;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100706 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000707
708 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400709 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000710 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100711 c = getopt(argc, argv, "f:ht:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100712 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000713 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100714 }
bellardea2384d2004-08-01 21:59:26 +0000715 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100716 case '?':
bellardea2384d2004-08-01 21:59:26 +0000717 case 'h':
718 help();
719 break;
720 case 'f':
721 fmt = optarg;
722 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400723 case 't':
724 cache = optarg;
725 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100726 case 'q':
727 quiet = true;
728 break;
bellardea2384d2004-08-01 21:59:26 +0000729 }
730 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200731 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800732 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100733 }
bellardea2384d2004-08-01 21:59:26 +0000734 filename = argv[optind++];
735
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400736 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100737 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400738 if (ret < 0) {
739 error_report("Invalid cache option: %s", cache);
740 return -1;
741 }
742
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200743 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900744 if (!bs) {
745 return 1;
746 }
bellardea2384d2004-08-01 21:59:26 +0000747 ret = bdrv_commit(bs);
748 switch(ret) {
749 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100750 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000751 break;
752 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100753 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000754 break;
755 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100756 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000757 break;
758 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100759 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000760 break;
761 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100762 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000763 break;
764 }
765
Fam Zheng4f6fd342013-08-23 09:14:47 +0800766 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900767 if (ret) {
768 return 1;
769 }
bellardea2384d2004-08-01 21:59:26 +0000770 return 0;
771}
772
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400773/*
thsf58c7b32008-06-05 21:53:49 +0000774 * Returns true iff the first sector pointed to by 'buf' contains at least
775 * a non-NUL byte.
776 *
777 * 'pnum' is set to the number of sectors (including and immediately following
778 * the first one) that are known to be in the same allocated/unallocated state.
779 */
bellardea2384d2004-08-01 21:59:26 +0000780static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
781{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000782 bool is_zero;
783 int i;
bellardea2384d2004-08-01 21:59:26 +0000784
785 if (n <= 0) {
786 *pnum = 0;
787 return 0;
788 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000789 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000790 for(i = 1; i < n; i++) {
791 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000792 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000793 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000794 }
bellardea2384d2004-08-01 21:59:26 +0000795 }
796 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000797 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000798}
799
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100800/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200801 * Like is_allocated_sectors, but if the buffer starts with a used sector,
802 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
803 * breaking up write requests for only small sparse areas.
804 */
805static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
806 int min)
807{
808 int ret;
809 int num_checked, num_used;
810
811 if (n < min) {
812 min = n;
813 }
814
815 ret = is_allocated_sectors(buf, n, pnum);
816 if (!ret) {
817 return ret;
818 }
819
820 num_used = *pnum;
821 buf += BDRV_SECTOR_SIZE * *pnum;
822 n -= *pnum;
823 num_checked = num_used;
824
825 while (n > 0) {
826 ret = is_allocated_sectors(buf, n, pnum);
827
828 buf += BDRV_SECTOR_SIZE * *pnum;
829 n -= *pnum;
830 num_checked += *pnum;
831 if (ret) {
832 num_used = num_checked;
833 } else if (*pnum >= min) {
834 break;
835 }
836 }
837
838 *pnum = num_used;
839 return 1;
840}
841
842/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100843 * Compares two buffers sector by sector. Returns 0 if the first sector of both
844 * buffers matches, non-zero otherwise.
845 *
846 * pnum is set to the number of sectors (including and immediately following
847 * the first one) that are known to have the same comparison result
848 */
849static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
850 int *pnum)
851{
852 int res, i;
853
854 if (n <= 0) {
855 *pnum = 0;
856 return 0;
857 }
858
859 res = !!memcmp(buf1, buf2, 512);
860 for(i = 1; i < n; i++) {
861 buf1 += 512;
862 buf2 += 512;
863
864 if (!!memcmp(buf1, buf2, 512) != res) {
865 break;
866 }
867 }
868
869 *pnum = i;
870 return res;
871}
872
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200873#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000874
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100875static int64_t sectors_to_bytes(int64_t sectors)
876{
877 return sectors << BDRV_SECTOR_BITS;
878}
879
880static int64_t sectors_to_process(int64_t total, int64_t from)
881{
882 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
883}
884
885/*
886 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
887 *
888 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
889 * data and negative value on error.
890 *
891 * @param bs: Driver used for accessing file
892 * @param sect_num: Number of first sector to check
893 * @param sect_count: Number of sectors to check
894 * @param filename: Name of disk file we are checking (logging purpose)
895 * @param buffer: Allocated buffer for storing read data
896 * @param quiet: Flag for quiet mode
897 */
898static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
899 int sect_count, const char *filename,
900 uint8_t *buffer, bool quiet)
901{
902 int pnum, ret = 0;
903 ret = bdrv_read(bs, sect_num, buffer, sect_count);
904 if (ret < 0) {
905 error_report("Error while reading offset %" PRId64 " of %s: %s",
906 sectors_to_bytes(sect_num), filename, strerror(-ret));
907 return ret;
908 }
909 ret = is_allocated_sectors(buffer, sect_count, &pnum);
910 if (ret || pnum != sect_count) {
911 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
912 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
913 return 1;
914 }
915
916 return 0;
917}
918
919/*
920 * Compares two images. Exit codes:
921 *
922 * 0 - Images are identical
923 * 1 - Images differ
924 * >1 - Error occurred
925 */
926static int img_compare(int argc, char **argv)
927{
928 const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
929 BlockDriverState *bs1, *bs2;
930 int64_t total_sectors1, total_sectors2;
931 uint8_t *buf1 = NULL, *buf2 = NULL;
932 int pnum1, pnum2;
933 int allocated1, allocated2;
934 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
935 bool progress = false, quiet = false, strict = false;
936 int64_t total_sectors;
937 int64_t sector_num = 0;
938 int64_t nb_sectors;
939 int c, pnum;
940 uint64_t bs_sectors;
941 uint64_t progress_base;
942
943 for (;;) {
944 c = getopt(argc, argv, "hpf:F:sq");
945 if (c == -1) {
946 break;
947 }
948 switch (c) {
949 case '?':
950 case 'h':
951 help();
952 break;
953 case 'f':
954 fmt1 = optarg;
955 break;
956 case 'F':
957 fmt2 = optarg;
958 break;
959 case 'p':
960 progress = true;
961 break;
962 case 'q':
963 quiet = true;
964 break;
965 case 's':
966 strict = true;
967 break;
968 }
969 }
970
971 /* Progress is not shown in Quiet mode */
972 if (quiet) {
973 progress = false;
974 }
975
976
Kevin Wolffc11eb22013-08-05 10:53:04 +0200977 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800978 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100979 }
980 filename1 = argv[optind++];
981 filename2 = argv[optind++];
982
983 /* Initialize before goto out */
984 qemu_progress_init(progress, 2.0);
985
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200986 bs1 = bdrv_new_open("image 1", filename1, fmt1, BDRV_O_FLAGS, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100987 if (!bs1) {
988 error_report("Can't open file %s", filename1);
989 ret = 2;
990 goto out3;
991 }
992
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200993 bs2 = bdrv_new_open("image 2", filename2, fmt2, BDRV_O_FLAGS, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100994 if (!bs2) {
995 error_report("Can't open file %s", filename2);
996 ret = 2;
997 goto out2;
998 }
999
1000 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
1001 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
1002 bdrv_get_geometry(bs1, &bs_sectors);
1003 total_sectors1 = bs_sectors;
1004 bdrv_get_geometry(bs2, &bs_sectors);
1005 total_sectors2 = bs_sectors;
1006 total_sectors = MIN(total_sectors1, total_sectors2);
1007 progress_base = MAX(total_sectors1, total_sectors2);
1008
1009 qemu_progress_print(0, 100);
1010
1011 if (strict && total_sectors1 != total_sectors2) {
1012 ret = 1;
1013 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1014 goto out;
1015 }
1016
1017 for (;;) {
1018 nb_sectors = sectors_to_process(total_sectors, sector_num);
1019 if (nb_sectors <= 0) {
1020 break;
1021 }
1022 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
1023 &pnum1);
1024 if (allocated1 < 0) {
1025 ret = 3;
1026 error_report("Sector allocation test failed for %s", filename1);
1027 goto out;
1028 }
1029
1030 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
1031 &pnum2);
1032 if (allocated2 < 0) {
1033 ret = 3;
1034 error_report("Sector allocation test failed for %s", filename2);
1035 goto out;
1036 }
1037 nb_sectors = MIN(pnum1, pnum2);
1038
1039 if (allocated1 == allocated2) {
1040 if (allocated1) {
1041 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
1042 if (ret < 0) {
1043 error_report("Error while reading offset %" PRId64 " of %s:"
1044 " %s", sectors_to_bytes(sector_num), filename1,
1045 strerror(-ret));
1046 ret = 4;
1047 goto out;
1048 }
1049 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1050 if (ret < 0) {
1051 error_report("Error while reading offset %" PRId64
1052 " of %s: %s", sectors_to_bytes(sector_num),
1053 filename2, strerror(-ret));
1054 ret = 4;
1055 goto out;
1056 }
1057 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1058 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001059 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1060 sectors_to_bytes(
1061 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001062 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001063 goto out;
1064 }
1065 }
1066 } else {
1067 if (strict) {
1068 ret = 1;
1069 qprintf(quiet, "Strict mode: Offset %" PRId64
1070 " allocation mismatch!\n",
1071 sectors_to_bytes(sector_num));
1072 goto out;
1073 }
1074
1075 if (allocated1) {
1076 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1077 filename1, buf1, quiet);
1078 } else {
1079 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1080 filename2, buf1, quiet);
1081 }
1082 if (ret) {
1083 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001084 error_report("Error while reading offset %" PRId64 ": %s",
1085 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001086 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001087 }
1088 goto out;
1089 }
1090 }
1091 sector_num += nb_sectors;
1092 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1093 }
1094
1095 if (total_sectors1 != total_sectors2) {
1096 BlockDriverState *bs_over;
1097 int64_t total_sectors_over;
1098 const char *filename_over;
1099
1100 qprintf(quiet, "Warning: Image size mismatch!\n");
1101 if (total_sectors1 > total_sectors2) {
1102 total_sectors_over = total_sectors1;
1103 bs_over = bs1;
1104 filename_over = filename1;
1105 } else {
1106 total_sectors_over = total_sectors2;
1107 bs_over = bs2;
1108 filename_over = filename2;
1109 }
1110
1111 for (;;) {
1112 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1113 if (nb_sectors <= 0) {
1114 break;
1115 }
1116 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1117 nb_sectors, &pnum);
1118 if (ret < 0) {
1119 ret = 3;
1120 error_report("Sector allocation test failed for %s",
1121 filename_over);
1122 goto out;
1123
1124 }
1125 nb_sectors = pnum;
1126 if (ret) {
1127 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1128 filename_over, buf1, quiet);
1129 if (ret) {
1130 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001131 error_report("Error while reading offset %" PRId64
1132 " of %s: %s", sectors_to_bytes(sector_num),
1133 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001134 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001135 }
1136 goto out;
1137 }
1138 }
1139 sector_num += nb_sectors;
1140 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1141 }
1142 }
1143
1144 qprintf(quiet, "Images are identical.\n");
1145 ret = 0;
1146
1147out:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001148 bdrv_unref(bs2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001149 qemu_vfree(buf1);
1150 qemu_vfree(buf2);
1151out2:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001152 bdrv_unref(bs1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001153out3:
1154 qemu_progress_end();
1155 return ret;
1156}
1157
bellardea2384d2004-08-01 21:59:26 +00001158static int img_convert(int argc, char **argv)
1159{
Peter Lieven24f833c2013-11-27 11:07:07 +01001160 int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001161 int64_t ret = 0;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001162 int progress = 0, flags;
1163 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001164 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001165 BlockDriverState **bs = NULL, *out_bs = NULL;
Peter Lieven802c3d42013-12-05 15:54:53 +01001166 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
ths96b8f132007-12-17 01:35:20 +00001167 uint64_t bs_sectors;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001168 uint8_t * buf = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001169 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardea2384d2004-08-01 21:59:26 +00001170 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +00001171 BlockDriverInfo bdi;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001172 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001173 QEMUOptionParameter *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001174 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001175 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001176 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001177 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001178 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001179 QemuOpts *sn_opts = NULL;
bellardea2384d2004-08-01 21:59:26 +00001180
1181 fmt = NULL;
1182 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001183 cache = "unsafe";
thsf58c7b32008-06-05 21:53:49 +00001184 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001185 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001186 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001187 for(;;) {
Wenchao Xiaef806542013-12-04 17:10:57 +08001188 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001189 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001190 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001191 }
bellardea2384d2004-08-01 21:59:26 +00001192 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001193 case '?':
bellardea2384d2004-08-01 21:59:26 +00001194 case 'h':
1195 help();
1196 break;
1197 case 'f':
1198 fmt = optarg;
1199 break;
1200 case 'O':
1201 out_fmt = optarg;
1202 break;
thsf58c7b32008-06-05 21:53:49 +00001203 case 'B':
1204 out_baseimg = optarg;
1205 break;
bellardea2384d2004-08-01 21:59:26 +00001206 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001207 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001208 break;
1209 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001210 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001211 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001212 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001213 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001214 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001215 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001216 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001217 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001218 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001219 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001220 if (!is_valid_option_list(optarg)) {
1221 error_report("Invalid option list: %s", optarg);
1222 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001223 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001224 }
1225 if (!options) {
1226 options = g_strdup(optarg);
1227 } else {
1228 char *old_options = options;
1229 options = g_strdup_printf("%s,%s", options, optarg);
1230 g_free(old_options);
1231 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001232 break;
edison51ef6722010-09-21 19:58:41 -07001233 case 's':
1234 snapshot_name = optarg;
1235 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001236 case 'l':
1237 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1238 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
1239 if (!sn_opts) {
1240 error_report("Failed in parsing snapshot param '%s'",
1241 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001242 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001243 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001244 }
1245 } else {
1246 snapshot_name = optarg;
1247 }
1248 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001249 case 'S':
1250 {
1251 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001252 char *end;
1253 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1254 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001255 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001256 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001257 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001258 }
1259
1260 min_sparse = sval / BDRV_SECTOR_SIZE;
1261 break;
1262 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001263 case 'p':
1264 progress = 1;
1265 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001266 case 't':
1267 cache = optarg;
1268 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001269 case 'q':
1270 quiet = true;
1271 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001272 case 'n':
1273 skip_create = 1;
1274 break;
bellardea2384d2004-08-01 21:59:26 +00001275 }
1276 }
ths3b46e622007-09-17 08:09:54 +00001277
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001278 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001279 if (quiet) {
1280 progress = 0;
1281 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001282 qemu_progress_init(progress, 1.0);
1283
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001284
balrog926c2d22007-10-31 01:11:44 +00001285 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001286 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001287
Kevin Wolf2dc83282014-02-21 16:24:05 +01001288 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001289 ret = print_block_option_help(out_filename, out_fmt);
1290 goto out;
1291 }
1292
bellardea2384d2004-08-01 21:59:26 +00001293 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001294 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001295 }
bellardea2384d2004-08-01 21:59:26 +00001296
thsf58c7b32008-06-05 21:53:49 +00001297
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001298 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001299 error_report("-B makes no sense when concatenating multiple input "
1300 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001301 ret = -1;
1302 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001303 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001304
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001305 qemu_progress_print(0, 100);
1306
Anthony Liguori7267c092011-08-20 22:09:37 -05001307 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
balrog926c2d22007-10-31 01:11:44 +00001308
1309 total_sectors = 0;
1310 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001311 char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
1312 : g_strdup("source");
1313 bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, BDRV_O_FLAGS,
1314 true, quiet);
1315 g_free(id);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001316 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001317 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001318 ret = -1;
1319 goto out;
1320 }
balrog926c2d22007-10-31 01:11:44 +00001321 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1322 total_sectors += bs_sectors;
1323 }
bellardea2384d2004-08-01 21:59:26 +00001324
Wenchao Xiaef806542013-12-04 17:10:57 +08001325 if (sn_opts) {
1326 ret = bdrv_snapshot_load_tmp(bs[0],
1327 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1328 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1329 &local_err);
1330 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001331 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001332 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001333 ret = -1;
1334 goto out;
1335 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001336
1337 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001338 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001339 if (local_err) {
Wenchao Xiaef806542013-12-04 17:10:57 +08001340 error_report("Failed to load snapshot: %s",
1341 error_get_pretty(local_err));
1342 error_free(local_err);
1343 ret = -1;
1344 goto out;
edison51ef6722010-09-21 19:58:41 -07001345 }
1346
Kevin Wolfefa84d42009-05-18 16:42:12 +02001347 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001348 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001349 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001350 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001351 ret = -1;
1352 goto out;
1353 }
balrog926c2d22007-10-31 01:11:44 +00001354
Kevin Wolf98289622013-07-10 15:47:39 +02001355 proto_drv = bdrv_find_protocol(out_filename, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001356 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001357 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001358 ret = -1;
1359 goto out;
1360 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001361
1362 create_options = append_option_parameters(create_options,
1363 drv->create_options);
1364 create_options = append_option_parameters(create_options,
1365 proto_drv->create_options);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001366
Kevin Wolfefa84d42009-05-18 16:42:12 +02001367 if (options) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001368 param = parse_option_parameters(options, create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001369 if (param == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001370 error_report("Invalid options for file format '%s'.", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001371 ret = -1;
1372 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001373 }
1374 } else {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001375 param = parse_option_parameters("", create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001376 }
1377
1378 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
Jes Sorenseneec77d92010-12-07 17:44:34 +01001379 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001380 if (ret < 0) {
1381 goto out;
1382 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001383
Kevin Wolfa18953f2010-10-14 15:46:04 +02001384 /* Get backing file name if -o backing_file was used */
1385 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1386 if (out_baseimg_param) {
1387 out_baseimg = out_baseimg_param->value.s;
1388 }
1389
Kevin Wolfefa84d42009-05-18 16:42:12 +02001390 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001391 if (compress) {
Kevin Wolfefa84d42009-05-18 16:42:12 +02001392 QEMUOptionParameter *encryption =
1393 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
Kevin Wolf41521fa2011-10-18 16:19:42 +02001394 QEMUOptionParameter *preallocation =
1395 get_option_parameter(param, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001396
1397 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001398 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001399 ret = -1;
1400 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001401 }
1402
1403 if (encryption && encryption->value.n) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001404 error_report("Compression and encryption not supported at "
1405 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001406 ret = -1;
1407 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001408 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001409
1410 if (preallocation && preallocation->value.s
1411 && strcmp(preallocation->value.s, "off"))
1412 {
1413 error_report("Compression and preallocation not supported at "
1414 "the same time");
1415 ret = -1;
1416 goto out;
1417 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001418 }
1419
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001420 if (!skip_create) {
1421 /* Create the new image */
Max Reitzcc84d902013-09-06 17:14:26 +02001422 ret = bdrv_create(drv, out_filename, param, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001423 if (ret < 0) {
Max Reitzcc84d902013-09-06 17:14:26 +02001424 error_report("%s: error while converting %s: %s",
1425 out_filename, out_fmt, error_get_pretty(local_err));
1426 error_free(local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001427 goto out;
bellardea2384d2004-08-01 21:59:26 +00001428 }
1429 }
ths3b46e622007-09-17 08:09:54 +00001430
Peter Lieven5a37b602013-10-24 12:07:06 +02001431 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001432 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001433 if (ret < 0) {
1434 error_report("Invalid cache option: %s", cache);
1435 return -1;
1436 }
1437
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001438 out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001439 if (!out_bs) {
1440 ret = -1;
1441 goto out;
1442 }
bellardea2384d2004-08-01 21:59:26 +00001443
balrog926c2d22007-10-31 01:11:44 +00001444 bs_i = 0;
1445 bs_offset = 0;
1446 bdrv_get_geometry(bs[0], &bs_sectors);
Peter Lievenf2521c92013-11-27 11:07:06 +01001447
1448 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1449 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1450 * as maximum. */
1451 bufsectors = MIN(32768,
1452 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1453 out_bs->bl.discard_alignment))
1454 );
1455
1456 buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001457
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001458 if (skip_create) {
1459 int64_t output_length = bdrv_getlength(out_bs);
1460 if (output_length < 0) {
1461 error_report("unable to get output image length: %s\n",
1462 strerror(-output_length));
1463 ret = -1;
1464 goto out;
1465 } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
1466 error_report("output file is smaller than input file");
1467 ret = -1;
1468 goto out;
1469 }
1470 }
1471
Peter Lieven24f833c2013-11-27 11:07:07 +01001472 cluster_sectors = 0;
1473 ret = bdrv_get_info(out_bs, &bdi);
1474 if (ret < 0) {
1475 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001476 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001477 goto out;
1478 }
Peter Lieven24f833c2013-11-27 11:07:07 +01001479 } else {
1480 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1481 }
1482
1483 if (compress) {
1484 if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001485 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001486 ret = -1;
1487 goto out;
1488 }
bellardea2384d2004-08-01 21:59:26 +00001489 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001490
1491 nb_sectors = total_sectors;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001492
bellardea2384d2004-08-01 21:59:26 +00001493 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001494 int64_t bs_num;
1495 int remainder;
1496 uint8_t *buf2;
1497
bellardea2384d2004-08-01 21:59:26 +00001498 nb_sectors = total_sectors - sector_num;
1499 if (nb_sectors <= 0)
1500 break;
1501 if (nb_sectors >= cluster_sectors)
1502 n = cluster_sectors;
1503 else
1504 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001505
1506 bs_num = sector_num - bs_offset;
1507 assert (bs_num >= 0);
1508 remainder = n;
1509 buf2 = buf;
1510 while (remainder > 0) {
1511 int nlow;
1512 while (bs_num == bs_sectors) {
1513 bs_i++;
1514 assert (bs_i < bs_n);
1515 bs_offset += bs_sectors;
1516 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1517 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001518 /* printf("changing part: sector_num=%" PRId64 ", "
1519 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1520 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
balrog926c2d22007-10-31 01:11:44 +00001521 }
1522 assert (bs_num < bs_sectors);
1523
1524 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1525
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001526 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1527 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001528 error_report("error while reading sector %" PRId64 ": %s",
1529 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001530 goto out;
1531 }
balrog926c2d22007-10-31 01:11:44 +00001532
1533 buf2 += nlow * 512;
1534 bs_num += nlow;
1535
1536 remainder -= nlow;
1537 }
1538 assert (remainder == 0);
1539
Stefan Hajnoczi54f106d2013-04-15 17:17:33 +02001540 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1541 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001542 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001543 error_report("error while compressing sector %" PRId64
1544 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001545 goto out;
1546 }
bellardea2384d2004-08-01 21:59:26 +00001547 }
1548 sector_num += n;
Peter Lieven13c28af2013-11-27 11:07:01 +01001549 qemu_progress_print(100.0 * sector_num / total_sectors, 0);
bellardea2384d2004-08-01 21:59:26 +00001550 }
bellardfaea38e2006-08-05 21:31:00 +00001551 /* signal EOF to align */
1552 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001553 } else {
Peter Lieven802c3d42013-12-05 15:54:53 +01001554 int64_t sectors_to_read, sectors_read, sector_num_next_status;
1555 bool count_allocated_sectors;
Peter Lieven11b66992013-10-24 12:07:05 +02001556 int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02001557
Peter Lieven5a37b602013-10-24 12:07:06 +02001558 if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
1559 ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
1560 if (ret < 0) {
1561 goto out;
1562 }
1563 has_zero_init = 1;
1564 }
1565
Peter Lieven802c3d42013-12-05 15:54:53 +01001566 sectors_to_read = total_sectors;
1567 count_allocated_sectors = progress && (out_baseimg || has_zero_init);
1568restart:
thsf58c7b32008-06-05 21:53:49 +00001569 sector_num = 0; // total number of sectors converted so far
Peter Lieven802c3d42013-12-05 15:54:53 +01001570 sectors_read = 0;
1571 sector_num_next_status = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001572
bellardea2384d2004-08-01 21:59:26 +00001573 for(;;) {
1574 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001575 if (nb_sectors <= 0) {
Peter Lieven802c3d42013-12-05 15:54:53 +01001576 if (count_allocated_sectors) {
1577 sectors_to_read = sectors_read;
1578 count_allocated_sectors = false;
1579 goto restart;
1580 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001581 ret = 0;
bellardea2384d2004-08-01 21:59:26 +00001582 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001583 }
balrog926c2d22007-10-31 01:11:44 +00001584
1585 while (sector_num - bs_offset >= bs_sectors) {
1586 bs_i ++;
1587 assert (bs_i < bs_n);
1588 bs_offset += bs_sectors;
1589 bdrv_get_geometry(bs[bs_i], &bs_sectors);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001590 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1591 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
balrog926c2d22007-10-31 01:11:44 +00001592 sector_num, bs_i, bs_offset, bs_sectors); */
1593 }
1594
Peter Lieven13c28af2013-11-27 11:07:01 +01001595 if ((out_baseimg || has_zero_init) &&
1596 sector_num >= sector_num_next_status) {
1597 n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors;
1598 ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset,
1599 n, &n1);
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001600 if (ret < 0) {
Peter Lieven13c28af2013-11-27 11:07:01 +01001601 error_report("error while reading block status of sector %"
1602 PRId64 ": %s", sector_num - bs_offset,
1603 strerror(-ret));
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001604 goto out;
aliguori93c65b42009-04-05 17:40:43 +00001605 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001606 /* If the output image is zero initialized, we are not working
1607 * on a shared base and the input is zero we can skip the next
1608 * n1 sectors */
1609 if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) {
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001610 sector_num += n1;
1611 continue;
1612 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001613 /* If the output image is being created as a copy on write
1614 * image, assume that sectors which are unallocated in the
1615 * input image are present in both the output's and input's
1616 * base images (no need to copy them). */
1617 if (out_baseimg) {
1618 if (!(ret & BDRV_BLOCK_DATA)) {
1619 sector_num += n1;
1620 continue;
1621 }
1622 /* The next 'n1' sectors are allocated in the input image.
1623 * Copy only those as they may be followed by unallocated
1624 * sectors. */
1625 nb_sectors = n1;
1626 }
1627 /* avoid redundant callouts to get_block_status */
1628 sector_num_next_status = sector_num + n1;
thsf58c7b32008-06-05 21:53:49 +00001629 }
1630
Peter Lievenf2521c92013-11-27 11:07:06 +01001631 n = MIN(nb_sectors, bufsectors);
Peter Lieven24f833c2013-11-27 11:07:07 +01001632
1633 /* round down request length to an aligned sector, but
1634 * do not bother doing this on short requests. They happen
1635 * when we found an all-zero area, and the next sector to
1636 * write will not be sector_num + n. */
1637 if (cluster_sectors > 0 && n >= cluster_sectors) {
1638 int64_t next_aligned_sector = (sector_num + n);
1639 next_aligned_sector -= next_aligned_sector % cluster_sectors;
1640 if (sector_num + n > next_aligned_sector) {
1641 n = next_aligned_sector - sector_num;
1642 }
1643 }
1644
Peter Lieven13c28af2013-11-27 11:07:01 +01001645 n = MIN(n, bs_sectors - (sector_num - bs_offset));
Peter Lieven13c28af2013-11-27 11:07:01 +01001646
Peter Lieven802c3d42013-12-05 15:54:53 +01001647 sectors_read += n;
1648 if (count_allocated_sectors) {
1649 sector_num += n;
1650 continue;
1651 }
1652
1653 n1 = n;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001654 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1655 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001656 error_report("error while reading sector %" PRId64 ": %s",
1657 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001658 goto out;
1659 }
bellardea2384d2004-08-01 21:59:26 +00001660 /* NOTE: at the same time we convert, we do not write zero
1661 sectors to have a chance to compress the image. Ideally, we
1662 should add a specific call to have the info to go faster */
1663 buf1 = buf;
1664 while (n > 0) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02001665 if (!has_zero_init ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001666 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001667 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1668 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001669 error_report("error while writing sector %" PRId64
1670 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001671 goto out;
1672 }
bellardea2384d2004-08-01 21:59:26 +00001673 }
1674 sector_num += n1;
1675 n -= n1;
1676 buf1 += n1 * 512;
1677 }
Peter Lieven802c3d42013-12-05 15:54:53 +01001678 qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
bellardea2384d2004-08-01 21:59:26 +00001679 }
1680 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001681out:
Peter Lieven13c28af2013-11-27 11:07:01 +01001682 if (!ret) {
1683 qemu_progress_print(100, 0);
1684 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001685 qemu_progress_end();
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001686 free_option_parameters(create_options);
1687 free_option_parameters(param);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001688 qemu_vfree(buf);
Wenchao Xiaef806542013-12-04 17:10:57 +08001689 if (sn_opts) {
1690 qemu_opts_del(sn_opts);
1691 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001692 if (out_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001693 bdrv_unref(out_bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001694 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001695 if (bs) {
1696 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1697 if (bs[bs_i]) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001698 bdrv_unref(bs[bs_i]);
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001699 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001700 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001701 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001702 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001703fail_getopt:
1704 g_free(options);
1705
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001706 if (ret) {
1707 return 1;
1708 }
bellardea2384d2004-08-01 21:59:26 +00001709 return 0;
1710}
1711
bellard57d1a2b2004-08-03 21:15:11 +00001712
bellardfaea38e2006-08-05 21:31:00 +00001713static void dump_snapshots(BlockDriverState *bs)
1714{
1715 QEMUSnapshotInfo *sn_tab, *sn;
1716 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00001717
1718 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1719 if (nb_sns <= 0)
1720 return;
1721 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08001722 bdrv_snapshot_dump(fprintf, stdout, NULL);
1723 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001724 for(i = 0; i < nb_sns; i++) {
1725 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08001726 bdrv_snapshot_dump(fprintf, stdout, sn);
1727 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001728 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001729 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001730}
1731
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001732static void dump_json_image_info_list(ImageInfoList *list)
1733{
1734 Error *errp = NULL;
1735 QString *str;
1736 QmpOutputVisitor *ov = qmp_output_visitor_new();
1737 QObject *obj;
1738 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1739 &list, NULL, &errp);
1740 obj = qmp_output_get_qobject(ov);
1741 str = qobject_to_json_pretty(obj);
1742 assert(str != NULL);
1743 printf("%s\n", qstring_get_str(str));
1744 qobject_decref(obj);
1745 qmp_output_visitor_cleanup(ov);
1746 QDECREF(str);
1747}
1748
Benoît Canetc054b3f2012-09-05 13:09:02 +02001749static void dump_json_image_info(ImageInfo *info)
1750{
1751 Error *errp = NULL;
1752 QString *str;
1753 QmpOutputVisitor *ov = qmp_output_visitor_new();
1754 QObject *obj;
1755 visit_type_ImageInfo(qmp_output_get_visitor(ov),
1756 &info, NULL, &errp);
1757 obj = qmp_output_get_qobject(ov);
1758 str = qobject_to_json_pretty(obj);
1759 assert(str != NULL);
1760 printf("%s\n", qstring_get_str(str));
1761 qobject_decref(obj);
1762 qmp_output_visitor_cleanup(ov);
1763 QDECREF(str);
1764}
1765
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001766static void dump_human_image_info_list(ImageInfoList *list)
1767{
1768 ImageInfoList *elem;
1769 bool delim = false;
1770
1771 for (elem = list; elem; elem = elem->next) {
1772 if (delim) {
1773 printf("\n");
1774 }
1775 delim = true;
1776
Wenchao Xia5b917042013-05-25 11:09:45 +08001777 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001778 }
1779}
1780
1781static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1782{
1783 return strcmp(a, b) == 0;
1784}
1785
1786/**
1787 * Open an image file chain and return an ImageInfoList
1788 *
1789 * @filename: topmost image filename
1790 * @fmt: topmost image format (may be NULL to autodetect)
1791 * @chain: true - enumerate entire backing file chain
1792 * false - only topmost image file
1793 *
1794 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1795 * image file. If there was an error a message will have been printed to
1796 * stderr.
1797 */
1798static ImageInfoList *collect_image_info_list(const char *filename,
1799 const char *fmt,
1800 bool chain)
1801{
1802 ImageInfoList *head = NULL;
1803 ImageInfoList **last = &head;
1804 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08001805 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001806
1807 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1808
1809 while (filename) {
1810 BlockDriverState *bs;
1811 ImageInfo *info;
1812 ImageInfoList *elem;
1813
1814 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1815 error_report("Backing file '%s' creates an infinite loop.",
1816 filename);
1817 goto err;
1818 }
1819 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1820
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001821 bs = bdrv_new_open("image", filename, fmt,
1822 BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001823 if (!bs) {
1824 goto err;
1825 }
1826
Wenchao Xia43526ec2013-06-06 12:27:58 +08001827 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001828 if (err) {
Wenchao Xia43526ec2013-06-06 12:27:58 +08001829 error_report("%s", error_get_pretty(err));
1830 error_free(err);
Prasad Joshibdf866f2014-03-26 01:55:53 +05301831 bdrv_unref(bs);
Wenchao Xia43526ec2013-06-06 12:27:58 +08001832 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08001833 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001834
1835 elem = g_new0(ImageInfoList, 1);
1836 elem->value = info;
1837 *last = elem;
1838 last = &elem->next;
1839
Fam Zheng4f6fd342013-08-23 09:14:47 +08001840 bdrv_unref(bs);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001841
1842 filename = fmt = NULL;
1843 if (chain) {
1844 if (info->has_full_backing_filename) {
1845 filename = info->full_backing_filename;
1846 } else if (info->has_backing_filename) {
1847 filename = info->backing_filename;
1848 }
1849 if (info->has_backing_filename_format) {
1850 fmt = info->backing_filename_format;
1851 }
1852 }
1853 }
1854 g_hash_table_destroy(filenames);
1855 return head;
1856
1857err:
1858 qapi_free_ImageInfoList(head);
1859 g_hash_table_destroy(filenames);
1860 return NULL;
1861}
1862
Benoît Canetc054b3f2012-09-05 13:09:02 +02001863static int img_info(int argc, char **argv)
1864{
1865 int c;
1866 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001867 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001868 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001869 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001870
bellardea2384d2004-08-01 21:59:26 +00001871 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001872 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001873 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001874 int option_index = 0;
1875 static const struct option long_options[] = {
1876 {"help", no_argument, 0, 'h'},
1877 {"format", required_argument, 0, 'f'},
1878 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001879 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001880 {0, 0, 0, 0}
1881 };
1882 c = getopt_long(argc, argv, "f:h",
1883 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001884 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001885 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001886 }
bellardea2384d2004-08-01 21:59:26 +00001887 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001888 case '?':
bellardea2384d2004-08-01 21:59:26 +00001889 case 'h':
1890 help();
1891 break;
1892 case 'f':
1893 fmt = optarg;
1894 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001895 case OPTION_OUTPUT:
1896 output = optarg;
1897 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001898 case OPTION_BACKING_CHAIN:
1899 chain = true;
1900 break;
bellardea2384d2004-08-01 21:59:26 +00001901 }
1902 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02001903 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001904 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001905 }
bellardea2384d2004-08-01 21:59:26 +00001906 filename = argv[optind++];
1907
Benoît Canetc054b3f2012-09-05 13:09:02 +02001908 if (output && !strcmp(output, "json")) {
1909 output_format = OFORMAT_JSON;
1910 } else if (output && !strcmp(output, "human")) {
1911 output_format = OFORMAT_HUMAN;
1912 } else if (output) {
1913 error_report("--output must be used with human or json as argument.");
1914 return 1;
1915 }
1916
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001917 list = collect_image_info_list(filename, fmt, chain);
1918 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001919 return 1;
1920 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001921
Benoît Canetc054b3f2012-09-05 13:09:02 +02001922 switch (output_format) {
1923 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001924 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001925 break;
1926 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001927 if (chain) {
1928 dump_json_image_info_list(list);
1929 } else {
1930 dump_json_image_info(list->value);
1931 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001932 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001933 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001934
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001935 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001936 return 0;
1937}
1938
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02001939
1940typedef struct MapEntry {
1941 int flags;
1942 int depth;
1943 int64_t start;
1944 int64_t length;
1945 int64_t offset;
1946 BlockDriverState *bs;
1947} MapEntry;
1948
1949static void dump_map_entry(OutputFormat output_format, MapEntry *e,
1950 MapEntry *next)
1951{
1952 switch (output_format) {
1953 case OFORMAT_HUMAN:
1954 if ((e->flags & BDRV_BLOCK_DATA) &&
1955 !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
1956 error_report("File contains external, encrypted or compressed clusters.");
1957 exit(1);
1958 }
1959 if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
1960 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
1961 e->start, e->length, e->offset, e->bs->filename);
1962 }
1963 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
1964 * Modify the flags here to allow more coalescing.
1965 */
1966 if (next &&
1967 (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
1968 next->flags &= ~BDRV_BLOCK_DATA;
1969 next->flags |= BDRV_BLOCK_ZERO;
1970 }
1971 break;
1972 case OFORMAT_JSON:
1973 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
1974 " \"zero\": %s, \"data\": %s",
1975 (e->start == 0 ? "[" : ",\n"),
1976 e->start, e->length, e->depth,
1977 (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
1978 (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
1979 if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02001980 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02001981 }
1982 putchar('}');
1983
1984 if (!next) {
1985 printf("]\n");
1986 }
1987 break;
1988 }
1989}
1990
1991static int get_block_status(BlockDriverState *bs, int64_t sector_num,
1992 int nb_sectors, MapEntry *e)
1993{
1994 int64_t ret;
1995 int depth;
1996
1997 /* As an optimization, we could cache the current range of unallocated
1998 * clusters in each file of the chain, and avoid querying the same
1999 * range repeatedly.
2000 */
2001
2002 depth = 0;
2003 for (;;) {
2004 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
2005 if (ret < 0) {
2006 return ret;
2007 }
2008 assert(nb_sectors);
2009 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2010 break;
2011 }
2012 bs = bs->backing_hd;
2013 if (bs == NULL) {
2014 ret = 0;
2015 break;
2016 }
2017
2018 depth++;
2019 }
2020
2021 e->start = sector_num * BDRV_SECTOR_SIZE;
2022 e->length = nb_sectors * BDRV_SECTOR_SIZE;
2023 e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
2024 e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
2025 e->depth = depth;
2026 e->bs = bs;
2027 return 0;
2028}
2029
2030static int img_map(int argc, char **argv)
2031{
2032 int c;
2033 OutputFormat output_format = OFORMAT_HUMAN;
2034 BlockDriverState *bs;
2035 const char *filename, *fmt, *output;
2036 int64_t length;
2037 MapEntry curr = { .length = 0 }, next;
2038 int ret = 0;
2039
2040 fmt = NULL;
2041 output = NULL;
2042 for (;;) {
2043 int option_index = 0;
2044 static const struct option long_options[] = {
2045 {"help", no_argument, 0, 'h'},
2046 {"format", required_argument, 0, 'f'},
2047 {"output", required_argument, 0, OPTION_OUTPUT},
2048 {0, 0, 0, 0}
2049 };
2050 c = getopt_long(argc, argv, "f:h",
2051 long_options, &option_index);
2052 if (c == -1) {
2053 break;
2054 }
2055 switch (c) {
2056 case '?':
2057 case 'h':
2058 help();
2059 break;
2060 case 'f':
2061 fmt = optarg;
2062 break;
2063 case OPTION_OUTPUT:
2064 output = optarg;
2065 break;
2066 }
2067 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002068 if (optind != argc - 1) {
2069 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002070 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002071 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002072
2073 if (output && !strcmp(output, "json")) {
2074 output_format = OFORMAT_JSON;
2075 } else if (output && !strcmp(output, "human")) {
2076 output_format = OFORMAT_HUMAN;
2077 } else if (output) {
2078 error_report("--output must be used with human or json as argument.");
2079 return 1;
2080 }
2081
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002082 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002083 if (!bs) {
2084 return 1;
2085 }
2086
2087 if (output_format == OFORMAT_HUMAN) {
2088 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2089 }
2090
2091 length = bdrv_getlength(bs);
2092 while (curr.start + curr.length < length) {
2093 int64_t nsectors_left;
2094 int64_t sector_num;
2095 int n;
2096
2097 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2098
2099 /* Probe up to 1 GiB at a time. */
2100 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2101 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2102 ret = get_block_status(bs, sector_num, n, &next);
2103
2104 if (ret < 0) {
2105 error_report("Could not read file metadata: %s", strerror(-ret));
2106 goto out;
2107 }
2108
2109 if (curr.length != 0 && curr.flags == next.flags &&
2110 curr.depth == next.depth &&
2111 ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
2112 curr.offset + curr.length == next.offset)) {
2113 curr.length += next.length;
2114 continue;
2115 }
2116
2117 if (curr.length > 0) {
2118 dump_map_entry(output_format, &curr, &next);
2119 }
2120 curr = next;
2121 }
2122
2123 dump_map_entry(output_format, &curr, NULL);
2124
2125out:
2126 bdrv_unref(bs);
2127 return ret < 0;
2128}
2129
aliguorif7b4a942009-01-07 17:40:15 +00002130#define SNAPSHOT_LIST 1
2131#define SNAPSHOT_CREATE 2
2132#define SNAPSHOT_APPLY 3
2133#define SNAPSHOT_DELETE 4
2134
Stuart Brady153859b2009-06-07 00:42:17 +01002135static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002136{
2137 BlockDriverState *bs;
2138 QEMUSnapshotInfo sn;
2139 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002140 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002141 int action = 0;
2142 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002143 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002144 Error *err = NULL;
aliguorif7b4a942009-01-07 17:40:15 +00002145
Kevin Wolf710da702011-01-10 12:33:02 +01002146 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002147 /* Parse commandline parameters */
2148 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002149 c = getopt(argc, argv, "la:c:d:hq");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002150 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002151 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002152 }
aliguorif7b4a942009-01-07 17:40:15 +00002153 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002154 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002155 case 'h':
2156 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002157 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002158 case 'l':
2159 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002160 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002161 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002162 }
2163 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002164 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002165 break;
2166 case 'a':
2167 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002168 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002169 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002170 }
2171 action = SNAPSHOT_APPLY;
2172 snapshot_name = optarg;
2173 break;
2174 case 'c':
2175 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002176 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002177 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002178 }
2179 action = SNAPSHOT_CREATE;
2180 snapshot_name = optarg;
2181 break;
2182 case 'd':
2183 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002184 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002185 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002186 }
2187 action = SNAPSHOT_DELETE;
2188 snapshot_name = optarg;
2189 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002190 case 'q':
2191 quiet = true;
2192 break;
aliguorif7b4a942009-01-07 17:40:15 +00002193 }
2194 }
2195
Kevin Wolffc11eb22013-08-05 10:53:04 +02002196 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002197 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002198 }
aliguorif7b4a942009-01-07 17:40:15 +00002199 filename = argv[optind++];
2200
2201 /* Open the image */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002202 bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002203 if (!bs) {
2204 return 1;
2205 }
aliguorif7b4a942009-01-07 17:40:15 +00002206
2207 /* Perform the requested action */
2208 switch(action) {
2209 case SNAPSHOT_LIST:
2210 dump_snapshots(bs);
2211 break;
2212
2213 case SNAPSHOT_CREATE:
2214 memset(&sn, 0, sizeof(sn));
2215 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2216
2217 qemu_gettimeofday(&tv);
2218 sn.date_sec = tv.tv_sec;
2219 sn.date_nsec = tv.tv_usec * 1000;
2220
2221 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002222 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002223 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002224 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002225 }
aliguorif7b4a942009-01-07 17:40:15 +00002226 break;
2227
2228 case SNAPSHOT_APPLY:
2229 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002230 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002231 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002232 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002233 }
aliguorif7b4a942009-01-07 17:40:15 +00002234 break;
2235
2236 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002237 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002238 if (err) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002239 error_report("Could not delete snapshot '%s': (%s)",
2240 snapshot_name, error_get_pretty(err));
2241 error_free(err);
2242 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002243 }
aliguorif7b4a942009-01-07 17:40:15 +00002244 break;
2245 }
2246
2247 /* Cleanup */
Fam Zheng4f6fd342013-08-23 09:14:47 +08002248 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002249 if (ret) {
2250 return 1;
2251 }
Stuart Brady153859b2009-06-07 00:42:17 +01002252 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002253}
2254
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002255static int img_rebase(int argc, char **argv)
2256{
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002257 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01002258 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002259 char *filename;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002260 const char *fmt, *cache, *out_basefmt, *out_baseimg;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002261 int c, flags, ret;
2262 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002263 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002264 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002265 Error *local_err = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002266
2267 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002268 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002269 cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002270 out_baseimg = NULL;
2271 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002272 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002273 c = getopt(argc, argv, "uhf:F:b:pt:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002274 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002275 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002276 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002277 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002278 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002279 case 'h':
2280 help();
2281 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002282 case 'f':
2283 fmt = optarg;
2284 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002285 case 'F':
2286 out_basefmt = optarg;
2287 break;
2288 case 'b':
2289 out_baseimg = optarg;
2290 break;
2291 case 'u':
2292 unsafe = 1;
2293 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002294 case 'p':
2295 progress = 1;
2296 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002297 case 't':
2298 cache = optarg;
2299 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002300 case 'q':
2301 quiet = true;
2302 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002303 }
2304 }
2305
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002306 if (quiet) {
2307 progress = 0;
2308 }
2309
Fam Zhengac1307a2014-04-22 13:36:11 +08002310 if (optind != argc - 1) {
2311 error_exit("Expecting one image file name");
2312 }
2313 if (!unsafe && !out_baseimg) {
2314 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002315 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002316 filename = argv[optind++];
2317
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002318 qemu_progress_init(progress, 2.0);
2319 qemu_progress_print(0, 100);
2320
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002321 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002322 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002323 if (ret < 0) {
2324 error_report("Invalid cache option: %s", cache);
2325 return -1;
2326 }
2327
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002328 /*
2329 * Open the images.
2330 *
2331 * Ignore the old backing file for unsafe rebase in case we want to correct
2332 * the reference to a renamed or moved backing file.
2333 */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002334 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002335 if (!bs) {
2336 return 1;
2337 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002338
2339 /* Find the right drivers for the backing files */
2340 old_backing_drv = NULL;
2341 new_backing_drv = NULL;
2342
2343 if (!unsafe && bs->backing_format[0] != '\0') {
2344 old_backing_drv = bdrv_find_format(bs->backing_format);
2345 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002346 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002347 ret = -1;
2348 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002349 }
2350 }
2351
2352 if (out_basefmt != NULL) {
2353 new_backing_drv = bdrv_find_format(out_basefmt);
2354 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002355 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002356 ret = -1;
2357 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002358 }
2359 }
2360
2361 /* For safe rebasing we need to compare old and new backing file */
2362 if (unsafe) {
2363 /* Make the compiler happy */
2364 bs_old_backing = NULL;
2365 bs_new_backing = NULL;
2366 } else {
2367 char backing_name[1024];
2368
Kevin Wolf98522f62014-04-17 13:16:01 +02002369 bs_old_backing = bdrv_new("old_backing", &error_abort);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002370 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzddf56362014-02-18 18:33:06 +01002371 ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, BDRV_O_FLAGS,
Max Reitz34b5d2c2013-09-05 14:45:29 +02002372 old_backing_drv, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002373 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002374 error_report("Could not open old backing file '%s': %s",
2375 backing_name, error_get_pretty(local_err));
2376 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002377 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002378 }
Alex Bligha6166732012-10-16 13:46:18 +01002379 if (out_baseimg[0]) {
Kevin Wolf98522f62014-04-17 13:16:01 +02002380 bs_new_backing = bdrv_new("new_backing", &error_abort);
Max Reitzddf56362014-02-18 18:33:06 +01002381 ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL,
2382 BDRV_O_FLAGS, new_backing_drv, &local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002383 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002384 error_report("Could not open new backing file '%s': %s",
2385 out_baseimg, error_get_pretty(local_err));
2386 error_free(local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002387 goto out;
2388 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002389 }
2390 }
2391
2392 /*
2393 * Check each unallocated cluster in the COW file. If it is unallocated,
2394 * accesses go to the backing file. We must therefore compare this cluster
2395 * in the old and new backing file, and if they differ we need to copy it
2396 * from the old backing file into the COW file.
2397 *
2398 * If qemu-img crashes during this step, no harm is done. The content of
2399 * the image is the same as the original one at any time.
2400 */
2401 if (!unsafe) {
2402 uint64_t num_sectors;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002403 uint64_t old_backing_num_sectors;
Alex Bligha6166732012-10-16 13:46:18 +01002404 uint64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002405 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002406 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08002407 uint8_t * buf_old;
2408 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02002409 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002410
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002411 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2412 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002413
2414 bdrv_get_geometry(bs, &num_sectors);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002415 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
Alex Bligha6166732012-10-16 13:46:18 +01002416 if (bs_new_backing) {
2417 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2418 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002419
Kevin Wolf1f710492012-10-12 14:29:18 +02002420 if (num_sectors != 0) {
2421 local_progress = (float)100 /
2422 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2423 }
2424
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002425 for (sector = 0; sector < num_sectors; sector += n) {
2426
2427 /* How many sectors can we handle with the next read? */
2428 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2429 n = (IO_BUF_SIZE / 512);
2430 } else {
2431 n = num_sectors - sector;
2432 }
2433
2434 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02002435 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02002436 if (ret < 0) {
2437 error_report("error while reading image metadata: %s",
2438 strerror(-ret));
2439 goto out;
2440 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02002441 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002442 continue;
2443 }
2444
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002445 /*
2446 * Read old and new backing file and take into consideration that
2447 * backing files may be smaller than the COW image.
2448 */
2449 if (sector >= old_backing_num_sectors) {
2450 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2451 } else {
2452 if (sector + n > old_backing_num_sectors) {
2453 n = old_backing_num_sectors - sector;
2454 }
2455
2456 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2457 if (ret < 0) {
2458 error_report("error while reading from old backing file");
2459 goto out;
2460 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002461 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002462
Alex Bligha6166732012-10-16 13:46:18 +01002463 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002464 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2465 } else {
2466 if (sector + n > new_backing_num_sectors) {
2467 n = new_backing_num_sectors - sector;
2468 }
2469
2470 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2471 if (ret < 0) {
2472 error_report("error while reading from new backing file");
2473 goto out;
2474 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002475 }
2476
2477 /* If they differ, we need to write to the COW file */
2478 uint64_t written = 0;
2479
2480 while (written < n) {
2481 int pnum;
2482
2483 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01002484 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002485 {
2486 ret = bdrv_write(bs, sector + written,
2487 buf_old + written * 512, pnum);
2488 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002489 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002490 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002491 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002492 }
2493 }
2494
2495 written += pnum;
2496 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002497 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002498 }
TeLeMand6771bf2010-02-08 16:20:00 +08002499
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002500 qemu_vfree(buf_old);
2501 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002502 }
2503
2504 /*
2505 * Change the backing file. All clusters that are different from the old
2506 * backing file are overwritten in the COW file now, so the visible content
2507 * doesn't change when we switch the backing file.
2508 */
Alex Bligha6166732012-10-16 13:46:18 +01002509 if (out_baseimg && *out_baseimg) {
2510 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2511 } else {
2512 ret = bdrv_change_backing_file(bs, NULL, NULL);
2513 }
2514
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002515 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002516 error_report("Could not change the backing file to '%s': No "
2517 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002518 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002519 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002520 out_baseimg, strerror(-ret));
2521 }
2522
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002523 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002524 /*
2525 * TODO At this point it is possible to check if any clusters that are
2526 * allocated in the COW file are the same in the backing file. If so, they
2527 * could be dropped from the COW file. Don't do this before switching the
2528 * backing file, in case of a crash this would lead to corruption.
2529 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002530out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002531 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002532 /* Cleanup */
2533 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002534 if (bs_old_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002535 bdrv_unref(bs_old_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002536 }
2537 if (bs_new_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002538 bdrv_unref(bs_new_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002539 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002540 }
2541
Fam Zheng4f6fd342013-08-23 09:14:47 +08002542 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002543 if (ret) {
2544 return 1;
2545 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002546 return 0;
2547}
2548
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002549static int img_resize(int argc, char **argv)
2550{
2551 int c, ret, relative;
2552 const char *filename, *fmt, *size;
2553 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002554 bool quiet = false;
Jes Sorensen2a819982010-12-06 17:08:31 +01002555 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002556 QemuOpts *param;
2557 static QemuOptsList resize_options = {
2558 .name = "resize_options",
2559 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2560 .desc = {
2561 {
2562 .name = BLOCK_OPT_SIZE,
2563 .type = QEMU_OPT_SIZE,
2564 .help = "Virtual disk size"
2565 }, {
2566 /* end of list */
2567 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002568 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002569 };
2570
Kevin Wolfe80fec72011-04-29 10:58:12 +02002571 /* Remove size from argv manually so that negative numbers are not treated
2572 * as options by getopt. */
2573 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002574 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02002575 return 1;
2576 }
2577
2578 size = argv[--argc];
2579
2580 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002581 fmt = NULL;
2582 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002583 c = getopt(argc, argv, "f:hq");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002584 if (c == -1) {
2585 break;
2586 }
2587 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002588 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002589 case 'h':
2590 help();
2591 break;
2592 case 'f':
2593 fmt = optarg;
2594 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002595 case 'q':
2596 quiet = true;
2597 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002598 }
2599 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002600 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002601 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002602 }
2603 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002604
2605 /* Choose grow, shrink, or absolute resize mode */
2606 switch (size[0]) {
2607 case '+':
2608 relative = 1;
2609 size++;
2610 break;
2611 case '-':
2612 relative = -1;
2613 size++;
2614 break;
2615 default:
2616 relative = 0;
2617 break;
2618 }
2619
2620 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002621 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002622 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002623 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002624 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002625 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002626 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002627 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002628 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2629 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002630
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002631 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
2632 true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002633 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002634 ret = -1;
2635 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002636 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002637
2638 if (relative) {
2639 total_size = bdrv_getlength(bs) + n * relative;
2640 } else {
2641 total_size = n;
2642 }
2643 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002644 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002645 ret = -1;
2646 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002647 }
2648
2649 ret = bdrv_truncate(bs, total_size);
2650 switch (ret) {
2651 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002652 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002653 break;
2654 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002655 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002656 break;
2657 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002658 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002659 break;
2660 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002661 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002662 break;
2663 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002664out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002665 if (bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002666 bdrv_unref(bs);
Jes Sorensen2a819982010-12-06 17:08:31 +01002667 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002668 if (ret) {
2669 return 1;
2670 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002671 return 0;
2672}
2673
Max Reitz6f176b42013-09-03 10:09:50 +02002674static int img_amend(int argc, char **argv)
2675{
2676 int c, ret = 0;
2677 char *options = NULL;
2678 QEMUOptionParameter *create_options = NULL, *options_param = NULL;
2679 const char *fmt = NULL, *filename;
2680 bool quiet = false;
2681 BlockDriverState *bs = NULL;
2682
2683 for (;;) {
2684 c = getopt(argc, argv, "hqf:o:");
2685 if (c == -1) {
2686 break;
2687 }
2688
2689 switch (c) {
2690 case 'h':
2691 case '?':
2692 help();
2693 break;
2694 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01002695 if (!is_valid_option_list(optarg)) {
2696 error_report("Invalid option list: %s", optarg);
2697 ret = -1;
2698 goto out;
2699 }
2700 if (!options) {
2701 options = g_strdup(optarg);
2702 } else {
2703 char *old_options = options;
2704 options = g_strdup_printf("%s,%s", options, optarg);
2705 g_free(old_options);
2706 }
Max Reitz6f176b42013-09-03 10:09:50 +02002707 break;
2708 case 'f':
2709 fmt = optarg;
2710 break;
2711 case 'q':
2712 quiet = true;
2713 break;
2714 }
2715 }
2716
Max Reitz6f176b42013-09-03 10:09:50 +02002717 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002718 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02002719 }
2720
Kevin Wolfa283cb62014-02-21 16:24:07 +01002721 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
2722 if (fmt && has_help_option(options)) {
2723 /* If a format is explicitly specified (and possibly no filename is
2724 * given), print option help here */
2725 ret = print_block_option_help(filename, fmt);
2726 goto out;
2727 }
2728
2729 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002730 error_exit("Expecting one image file name");
Kevin Wolfa283cb62014-02-21 16:24:07 +01002731 }
Max Reitz6f176b42013-09-03 10:09:50 +02002732
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002733 bs = bdrv_new_open("image", filename, fmt,
2734 BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
Max Reitz6f176b42013-09-03 10:09:50 +02002735 if (!bs) {
2736 error_report("Could not open image '%s'", filename);
2737 ret = -1;
2738 goto out;
2739 }
2740
2741 fmt = bs->drv->format_name;
2742
Kevin Wolf626f84f2014-02-21 16:24:06 +01002743 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01002744 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02002745 ret = print_block_option_help(filename, fmt);
2746 goto out;
2747 }
2748
2749 create_options = append_option_parameters(create_options,
2750 bs->drv->create_options);
2751 options_param = parse_option_parameters(options, create_options,
2752 options_param);
2753 if (options_param == NULL) {
2754 error_report("Invalid options for file format '%s'", fmt);
2755 ret = -1;
2756 goto out;
2757 }
2758
2759 ret = bdrv_amend_options(bs, options_param);
2760 if (ret < 0) {
2761 error_report("Error while amending options: %s", strerror(-ret));
2762 goto out;
2763 }
2764
2765out:
2766 if (bs) {
2767 bdrv_unref(bs);
2768 }
2769 free_option_parameters(create_options);
2770 free_option_parameters(options_param);
Kevin Wolf626f84f2014-02-21 16:24:06 +01002771 g_free(options);
2772
Max Reitz6f176b42013-09-03 10:09:50 +02002773 if (ret) {
2774 return 1;
2775 }
2776 return 0;
2777}
2778
Anthony Liguoric227f092009-10-01 16:12:16 -05002779static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002780#define DEF(option, callback, arg_string) \
2781 { option, callback },
2782#include "qemu-img-cmds.h"
2783#undef DEF
2784#undef GEN_DOCS
2785 { NULL, NULL, },
2786};
2787
bellardea2384d2004-08-01 21:59:26 +00002788int main(int argc, char **argv)
2789{
Anthony Liguoric227f092009-10-01 16:12:16 -05002790 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002791 const char *cmdname;
bellardea2384d2004-08-01 21:59:26 +00002792
MORITA Kazutaka526eda12013-07-23 17:30:11 +09002793#ifdef CONFIG_POSIX
2794 signal(SIGPIPE, SIG_IGN);
2795#endif
2796
Kevin Wolf53f76e52010-12-16 15:10:32 +01002797 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08002798 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01002799
Paolo Bonzini2592c592012-11-03 18:10:17 +01002800 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002801 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08002802 if (argc < 2) {
2803 error_exit("Not enough arguments");
2804 }
Stuart Brady153859b2009-06-07 00:42:17 +01002805 cmdname = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00002806 argc--; argv++;
Stuart Brady153859b2009-06-07 00:42:17 +01002807
2808 /* find the command */
2809 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2810 if (!strcmp(cmdname, cmd->name)) {
2811 return cmd->handler(argc, argv);
2812 }
bellardea2384d2004-08-01 21:59:26 +00002813 }
Stuart Brady153859b2009-06-07 00:42:17 +01002814
2815 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08002816 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00002817}