blob: 96f44638b77ef0f9888829e53659ffb7a0cd7b48 [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
Jeff Cody5f6979c2014-04-28 14:37:18 -040036#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \
37 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
38
Anthony Liguoric227f092009-10-01 16:12:16 -050039typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010040 const char *name;
41 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050042} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010043
Federico Simoncelli8599ea42013-01-28 06:59:47 -050044enum {
45 OPTION_OUTPUT = 256,
46 OPTION_BACKING_CHAIN = 257,
47};
48
49typedef enum OutputFormat {
50 OFORMAT_JSON,
51 OFORMAT_HUMAN,
52} OutputFormat;
53
aurel32137519c2008-11-30 19:12:49 +000054/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010055#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040056#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000057
bellardea2384d2004-08-01 21:59:26 +000058static void format_print(void *opaque, const char *name)
59{
60 printf(" %s", name);
61}
62
Fam Zhengac1307a2014-04-22 13:36:11 +080063static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
64{
65 va_list ap;
66
67 error_printf("qemu-img: ");
68
69 va_start(ap, fmt);
70 error_vprintf(fmt, ap);
71 va_end(ap);
72
73 error_printf("\nTry 'qemu-img --help' for more information\n");
74 exit(EXIT_FAILURE);
75}
76
blueswir1d2c639d2009-01-24 18:19:25 +000077/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080078static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000079{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010080 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040081 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030082 "usage: qemu-img command [command options]\n"
83 "QEMU disk image utility\n"
84 "\n"
85 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010086#define DEF(option, callback, arg_string) \
87 " " arg_string "\n"
88#include "qemu-img-cmds.h"
89#undef DEF
90#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030091 "\n"
92 "Command parameters:\n"
93 " 'filename' is a disk image filename\n"
94 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -040095 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +080096 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
97 " 'directsync' and 'unsafe' (default for convert)\n"
malc3f020d72010-02-08 12:04:56 +030098 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +020099 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
100 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
101 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300102 " 'output_filename' is the destination disk image filename\n"
103 " 'output_fmt' is the destination format\n"
104 " 'options' is a comma separated list of format specific options in a\n"
105 " name=value format. Use -o ? for an overview of the options supported by the\n"
106 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800107 " 'snapshot_param' is param used for internal snapshot, format\n"
108 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
109 " '[ID_OR_NAME]'\n"
110 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
111 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300112 " '-c' indicates that target image must be compressed (qcow format only)\n"
113 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
114 " match exactly. The image doesn't need a working backing file before\n"
115 " rebasing in this case (useful for renaming the backing file)\n"
116 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200117 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100118 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200119 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
120 " contain only zeros for qemu-img to create a sparse image during\n"
121 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
122 " unallocated or zero sectors, and the destination image will always be\n"
123 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200124 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100125 " '-n' skips the target volume creation (useful if the volume is created\n"
126 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300127 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200128 "Parameters to check subcommand:\n"
129 " '-r' tries to repair any inconsistencies that are found during the check.\n"
130 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
131 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200132 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200133 "\n"
malc3f020d72010-02-08 12:04:56 +0300134 "Parameters to snapshot subcommand:\n"
135 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
136 " '-a' applies a snapshot (revert disk to saved state)\n"
137 " '-c' creates a snapshot\n"
138 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100139 " '-l' lists all snapshots in the given image\n"
140 "\n"
141 "Parameters to compare subcommand:\n"
142 " '-f' first image format\n"
143 " '-F' second image format\n"
144 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100145
146 printf("%s\nSupported formats:", help_msg);
bellardea2384d2004-08-01 21:59:26 +0000147 bdrv_iterate_format(format_print, NULL);
148 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800149 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000150}
151
Stefan Weil7c30f652013-06-16 17:01:05 +0200152static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100153{
154 int ret = 0;
155 if (!quiet) {
156 va_list args;
157 va_start(args, fmt);
158 ret = vprintf(fmt, args);
159 va_end(args);
160 }
161 return ret;
162}
163
bellardea2384d2004-08-01 21:59:26 +0000164#if defined(WIN32)
165/* XXX: put correct support for win32 */
166static int read_password(char *buf, int buf_size)
167{
168 int c, i;
169 printf("Password: ");
170 fflush(stdout);
171 i = 0;
172 for(;;) {
173 c = getchar();
174 if (c == '\n')
175 break;
176 if (i < (buf_size - 1))
177 buf[i++] = c;
178 }
179 buf[i] = '\0';
180 return 0;
181}
182
183#else
184
185#include <termios.h>
186
187static struct termios oldtty;
188
189static void term_exit(void)
190{
191 tcsetattr (0, TCSANOW, &oldtty);
192}
193
194static void term_init(void)
195{
196 struct termios tty;
197
198 tcgetattr (0, &tty);
199 oldtty = tty;
200
201 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
202 |INLCR|IGNCR|ICRNL|IXON);
203 tty.c_oflag |= OPOST;
204 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
205 tty.c_cflag &= ~(CSIZE|PARENB);
206 tty.c_cflag |= CS8;
207 tty.c_cc[VMIN] = 1;
208 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000209
bellardea2384d2004-08-01 21:59:26 +0000210 tcsetattr (0, TCSANOW, &tty);
211
212 atexit(term_exit);
213}
214
pbrook3f379ab2007-11-11 03:33:13 +0000215static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000216{
217 uint8_t ch;
218 int i, ret;
219
220 printf("password: ");
221 fflush(stdout);
222 term_init();
223 i = 0;
224 for(;;) {
225 ret = read(0, &ch, 1);
226 if (ret == -1) {
227 if (errno == EAGAIN || errno == EINTR) {
228 continue;
229 } else {
230 ret = -1;
231 break;
232 }
233 } else if (ret == 0) {
234 ret = -1;
235 break;
236 } else {
237 if (ch == '\r') {
238 ret = 0;
239 break;
240 }
241 if (i < (buf_size - 1))
242 buf[i++] = ch;
243 }
244 }
245 term_exit();
246 buf[i] = '\0';
247 printf("\n");
248 return ret;
249}
250#endif
251
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100252static int print_block_option_help(const char *filename, const char *fmt)
253{
254 BlockDriver *drv, *proto_drv;
255 QEMUOptionParameter *create_options = NULL;
256
257 /* Find driver and parse its options */
258 drv = bdrv_find_format(fmt);
259 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100260 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100261 return 1;
262 }
263
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100264 create_options = append_option_parameters(create_options,
265 drv->create_options);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100266
267 if (filename) {
268 proto_drv = bdrv_find_protocol(filename, true);
269 if (!proto_drv) {
270 error_report("Unknown protocol '%s'", filename);
271 return 1;
272 }
273 create_options = append_option_parameters(create_options,
274 proto_drv->create_options);
275 }
276
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100277 print_option_help(create_options);
278 free_option_parameters(create_options);
279 return 0;
280}
281
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200282static BlockDriverState *bdrv_new_open(const char *id,
283 const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800284 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100285 int flags,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100286 bool require_io,
287 bool quiet)
bellard75c23802004-08-27 21:28:58 +0000288{
289 BlockDriverState *bs;
290 BlockDriver *drv;
291 char password[256];
Max Reitz34b5d2c2013-09-05 14:45:29 +0200292 Error *local_err = NULL;
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100293 int ret;
bellard75c23802004-08-27 21:28:58 +0000294
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200295 bs = bdrv_new(id, &error_abort);
Kevin Wolfad717132010-12-16 15:37:41 +0100296
bellard75c23802004-08-27 21:28:58 +0000297 if (fmt) {
298 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900299 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100300 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900301 goto fail;
302 }
bellard75c23802004-08-27 21:28:58 +0000303 } else {
304 drv = NULL;
305 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100306
Max Reitzddf56362014-02-18 18:33:06 +0100307 ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100308 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200309 error_report("Could not open '%s': %s", filename,
310 error_get_pretty(local_err));
311 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900312 goto fail;
bellard75c23802004-08-27 21:28:58 +0000313 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100314
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100315 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100316 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900317 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100318 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900319 goto fail;
320 }
321 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100322 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900323 goto fail;
324 }
bellard75c23802004-08-27 21:28:58 +0000325 }
326 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900327fail:
Max Reitzf67503e2014-02-18 18:33:05 +0100328 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900329 return NULL;
bellard75c23802004-08-27 21:28:58 +0000330}
331
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900332static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100333 const char *base_filename,
334 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200335{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200336 if (base_filename) {
337 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100338 error_report("Backing file not supported for file format '%s'",
339 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900340 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200341 }
342 }
343 if (base_fmt) {
344 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100345 error_report("Backing file format not supported for file "
346 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900347 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200348 }
349 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900350 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200351}
352
bellardea2384d2004-08-01 21:59:26 +0000353static int img_create(int argc, char **argv)
354{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200355 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100356 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000357 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000358 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000359 const char *filename;
360 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200361 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200362 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100363 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000364
bellardea2384d2004-08-01 21:59:26 +0000365 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100366 c = getopt(argc, argv, "F:b:f:he6o:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100367 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000368 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100369 }
bellardea2384d2004-08-01 21:59:26 +0000370 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100371 case '?':
bellardea2384d2004-08-01 21:59:26 +0000372 case 'h':
373 help();
374 break;
aliguori9230eaf2009-03-28 17:55:19 +0000375 case 'F':
376 base_fmt = optarg;
377 break;
bellardea2384d2004-08-01 21:59:26 +0000378 case 'b':
379 base_filename = optarg;
380 break;
381 case 'f':
382 fmt = optarg;
383 break;
384 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200385 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100386 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100387 goto fail;
thsd8871c52007-10-24 16:11:42 +0000388 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200389 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100390 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100391 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200392 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100393 if (!is_valid_option_list(optarg)) {
394 error_report("Invalid option list: %s", optarg);
395 goto fail;
396 }
397 if (!options) {
398 options = g_strdup(optarg);
399 } else {
400 char *old_options = options;
401 options = g_strdup_printf("%s,%s", options, optarg);
402 g_free(old_options);
403 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200404 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100405 case 'q':
406 quiet = true;
407 break;
bellardea2384d2004-08-01 21:59:26 +0000408 }
409 }
aliguori9230eaf2009-03-28 17:55:19 +0000410
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900411 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100412 filename = (optind < argc) ? argv[optind] : NULL;
413 if (options && has_help_option(options)) {
414 g_free(options);
415 return print_block_option_help(filename, fmt);
416 }
417
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100418 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800419 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100420 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100421 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900422
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100423 /* Get image size, if specified */
424 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100425 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100426 char *end;
427 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
428 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800429 if (sval == -ERANGE) {
430 error_report("Image size must be less than 8 EiB!");
431 } else {
432 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200433 "G, T, P or E suffixes for ");
434 error_report("kilobytes, megabytes, gigabytes, terabytes, "
435 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800436 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100437 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100438 }
439 img_size = (uint64_t)sval;
440 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200441 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800442 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200443 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100444
Luiz Capitulino9b375252012-11-30 10:52:05 -0200445 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100446 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100447 if (local_err) {
Max Reitzb70d8c22013-09-06 16:51:03 +0200448 error_report("%s: %s", filename, error_get_pretty(local_err));
Luiz Capitulino9b375252012-11-30 10:52:05 -0200449 error_free(local_err);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100450 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900451 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200452
Kevin Wolf77386bf2014-02-21 16:24:04 +0100453 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000454 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100455
456fail:
457 g_free(options);
458 return 1;
bellardea2384d2004-08-01 21:59:26 +0000459}
460
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100461static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500462{
Markus Armbruster4399c432014-04-25 16:50:32 +0200463 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500464 QString *str;
465 QmpOutputVisitor *ov = qmp_output_visitor_new();
466 QObject *obj;
467 visit_type_ImageCheck(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +0200468 &check, NULL, &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500469 obj = qmp_output_get_qobject(ov);
470 str = qobject_to_json_pretty(obj);
471 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100472 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500473 qobject_decref(obj);
474 qmp_output_visitor_cleanup(ov);
475 QDECREF(str);
476}
477
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100478static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500479{
480 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100481 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500482 } else {
483 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100484 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
485 "Data may be corrupted, or further writes to the image "
486 "may corrupt it.\n",
487 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500488 }
489
490 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100491 qprintf(quiet,
492 "\n%" PRId64 " leaked clusters were found on the image.\n"
493 "This means waste of disk space, but no harm to data.\n",
494 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500495 }
496
497 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100498 qprintf(quiet,
499 "\n%" PRId64
500 " internal errors have occurred during the check.\n",
501 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500502 }
503 }
504
505 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100506 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
507 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
508 check->allocated_clusters, check->total_clusters,
509 check->allocated_clusters * 100.0 / check->total_clusters,
510 check->fragmented_clusters * 100.0 / check->allocated_clusters,
511 check->compressed_clusters * 100.0 /
512 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500513 }
514
515 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100516 qprintf(quiet,
517 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500518 }
519}
520
521static int collect_image_check(BlockDriverState *bs,
522 ImageCheck *check,
523 const char *filename,
524 const char *fmt,
525 int fix)
526{
527 int ret;
528 BdrvCheckResult result;
529
530 ret = bdrv_check(bs, &result, fix);
531 if (ret < 0) {
532 return ret;
533 }
534
535 check->filename = g_strdup(filename);
536 check->format = g_strdup(bdrv_get_format_name(bs));
537 check->check_errors = result.check_errors;
538 check->corruptions = result.corruptions;
539 check->has_corruptions = result.corruptions != 0;
540 check->leaks = result.leaks;
541 check->has_leaks = result.leaks != 0;
542 check->corruptions_fixed = result.corruptions_fixed;
543 check->has_corruptions_fixed = result.corruptions != 0;
544 check->leaks_fixed = result.leaks_fixed;
545 check->has_leaks_fixed = result.leaks != 0;
546 check->image_end_offset = result.image_end_offset;
547 check->has_image_end_offset = result.image_end_offset != 0;
548 check->total_clusters = result.bfi.total_clusters;
549 check->has_total_clusters = result.bfi.total_clusters != 0;
550 check->allocated_clusters = result.bfi.allocated_clusters;
551 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
552 check->fragmented_clusters = result.bfi.fragmented_clusters;
553 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100554 check->compressed_clusters = result.bfi.compressed_clusters;
555 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500556
557 return 0;
558}
559
Kevin Wolfe076f332010-06-29 11:43:13 +0200560/*
561 * Checks an image for consistency. Exit codes:
562 *
563 * 0 - Check completed, image is good
564 * 1 - Check not completed because of internal errors
565 * 2 - Check completed, image is corrupted
566 * 3 - Check completed, image has leaked clusters, but is good otherwise
567 */
aliguori15859692009-04-21 23:11:53 +0000568static int img_check(int argc, char **argv)
569{
570 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500571 OutputFormat output_format = OFORMAT_HUMAN;
572 const char *filename, *fmt, *output;
aliguori15859692009-04-21 23:11:53 +0000573 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200574 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100575 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500576 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100577 bool quiet = false;
aliguori15859692009-04-21 23:11:53 +0000578
579 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500580 output = NULL;
aliguori15859692009-04-21 23:11:53 +0000581 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500582 int option_index = 0;
583 static const struct option long_options[] = {
584 {"help", no_argument, 0, 'h'},
585 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530586 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500587 {"output", required_argument, 0, OPTION_OUTPUT},
588 {0, 0, 0, 0}
589 };
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100590 c = getopt_long(argc, argv, "f:hr:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500591 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100592 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000593 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100594 }
aliguori15859692009-04-21 23:11:53 +0000595 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100596 case '?':
aliguori15859692009-04-21 23:11:53 +0000597 case 'h':
598 help();
599 break;
600 case 'f':
601 fmt = optarg;
602 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200603 case 'r':
604 flags |= BDRV_O_RDWR;
605
606 if (!strcmp(optarg, "leaks")) {
607 fix = BDRV_FIX_LEAKS;
608 } else if (!strcmp(optarg, "all")) {
609 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
610 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800611 error_exit("Unknown option value for -r "
612 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200613 }
614 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500615 case OPTION_OUTPUT:
616 output = optarg;
617 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100618 case 'q':
619 quiet = true;
620 break;
aliguori15859692009-04-21 23:11:53 +0000621 }
622 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200623 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800624 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100625 }
aliguori15859692009-04-21 23:11:53 +0000626 filename = argv[optind++];
627
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500628 if (output && !strcmp(output, "json")) {
629 output_format = OFORMAT_JSON;
630 } else if (output && !strcmp(output, "human")) {
631 output_format = OFORMAT_HUMAN;
632 } else if (output) {
633 error_report("--output must be used with human or json as argument.");
634 return 1;
635 }
636
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200637 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900638 if (!bs) {
639 return 1;
640 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500641
642 check = g_new0(ImageCheck, 1);
643 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200644
645 if (ret == -ENOTSUP) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500646 if (output_format == OFORMAT_HUMAN) {
647 error_report("This image format does not support checks");
648 }
Peter Lievenfefddf92013-10-24 08:53:34 +0200649 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500650 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200651 }
652
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500653 if (check->corruptions_fixed || check->leaks_fixed) {
654 int corruptions_fixed, leaks_fixed;
655
656 leaks_fixed = check->leaks_fixed;
657 corruptions_fixed = check->corruptions_fixed;
658
659 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100660 qprintf(quiet,
661 "The following inconsistencies were found and repaired:\n\n"
662 " %" PRId64 " leaked clusters\n"
663 " %" PRId64 " corruptions\n\n"
664 "Double checking the fixed image now...\n",
665 check->leaks_fixed,
666 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500667 }
668
669 ret = collect_image_check(bs, check, filename, fmt, 0);
670
671 check->leaks_fixed = leaks_fixed;
672 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200673 }
674
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500675 switch (output_format) {
676 case OFORMAT_HUMAN:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100677 dump_human_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500678 break;
679 case OFORMAT_JSON:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100680 dump_json_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500681 break;
682 }
683
684 if (ret || check->check_errors) {
685 ret = 1;
686 goto fail;
687 }
688
689 if (check->corruptions) {
690 ret = 2;
691 } else if (check->leaks) {
692 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200693 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500694 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000695 }
696
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500697fail:
698 qapi_free_ImageCheck(check);
Fam Zheng4f6fd342013-08-23 09:14:47 +0800699 bdrv_unref(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200700
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500701 return ret;
aliguori15859692009-04-21 23:11:53 +0000702}
703
bellardea2384d2004-08-01 21:59:26 +0000704static int img_commit(int argc, char **argv)
705{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400706 int c, ret, flags;
707 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000708 BlockDriverState *bs;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100709 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000710
711 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400712 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000713 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100714 c = getopt(argc, argv, "f:ht:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100715 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000716 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100717 }
bellardea2384d2004-08-01 21:59:26 +0000718 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100719 case '?':
bellardea2384d2004-08-01 21:59:26 +0000720 case 'h':
721 help();
722 break;
723 case 'f':
724 fmt = optarg;
725 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400726 case 't':
727 cache = optarg;
728 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100729 case 'q':
730 quiet = true;
731 break;
bellardea2384d2004-08-01 21:59:26 +0000732 }
733 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200734 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800735 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100736 }
bellardea2384d2004-08-01 21:59:26 +0000737 filename = argv[optind++];
738
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400739 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100740 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400741 if (ret < 0) {
742 error_report("Invalid cache option: %s", cache);
743 return -1;
744 }
745
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200746 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900747 if (!bs) {
748 return 1;
749 }
bellardea2384d2004-08-01 21:59:26 +0000750 ret = bdrv_commit(bs);
751 switch(ret) {
752 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100753 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000754 break;
755 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100756 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000757 break;
758 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100759 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000760 break;
761 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100762 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000763 break;
764 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100765 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000766 break;
767 }
768
Fam Zheng4f6fd342013-08-23 09:14:47 +0800769 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900770 if (ret) {
771 return 1;
772 }
bellardea2384d2004-08-01 21:59:26 +0000773 return 0;
774}
775
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400776/*
thsf58c7b32008-06-05 21:53:49 +0000777 * Returns true iff the first sector pointed to by 'buf' contains at least
778 * a non-NUL byte.
779 *
780 * 'pnum' is set to the number of sectors (including and immediately following
781 * the first one) that are known to be in the same allocated/unallocated state.
782 */
bellardea2384d2004-08-01 21:59:26 +0000783static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
784{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000785 bool is_zero;
786 int i;
bellardea2384d2004-08-01 21:59:26 +0000787
788 if (n <= 0) {
789 *pnum = 0;
790 return 0;
791 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000792 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000793 for(i = 1; i < n; i++) {
794 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000795 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000796 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000797 }
bellardea2384d2004-08-01 21:59:26 +0000798 }
799 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000800 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000801}
802
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100803/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200804 * Like is_allocated_sectors, but if the buffer starts with a used sector,
805 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
806 * breaking up write requests for only small sparse areas.
807 */
808static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
809 int min)
810{
811 int ret;
812 int num_checked, num_used;
813
814 if (n < min) {
815 min = n;
816 }
817
818 ret = is_allocated_sectors(buf, n, pnum);
819 if (!ret) {
820 return ret;
821 }
822
823 num_used = *pnum;
824 buf += BDRV_SECTOR_SIZE * *pnum;
825 n -= *pnum;
826 num_checked = num_used;
827
828 while (n > 0) {
829 ret = is_allocated_sectors(buf, n, pnum);
830
831 buf += BDRV_SECTOR_SIZE * *pnum;
832 n -= *pnum;
833 num_checked += *pnum;
834 if (ret) {
835 num_used = num_checked;
836 } else if (*pnum >= min) {
837 break;
838 }
839 }
840
841 *pnum = num_used;
842 return 1;
843}
844
845/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100846 * Compares two buffers sector by sector. Returns 0 if the first sector of both
847 * buffers matches, non-zero otherwise.
848 *
849 * pnum is set to the number of sectors (including and immediately following
850 * the first one) that are known to have the same comparison result
851 */
852static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
853 int *pnum)
854{
855 int res, i;
856
857 if (n <= 0) {
858 *pnum = 0;
859 return 0;
860 }
861
862 res = !!memcmp(buf1, buf2, 512);
863 for(i = 1; i < n; i++) {
864 buf1 += 512;
865 buf2 += 512;
866
867 if (!!memcmp(buf1, buf2, 512) != res) {
868 break;
869 }
870 }
871
872 *pnum = i;
873 return res;
874}
875
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200876#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000877
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100878static int64_t sectors_to_bytes(int64_t sectors)
879{
880 return sectors << BDRV_SECTOR_BITS;
881}
882
883static int64_t sectors_to_process(int64_t total, int64_t from)
884{
885 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
886}
887
888/*
889 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
890 *
891 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
892 * data and negative value on error.
893 *
894 * @param bs: Driver used for accessing file
895 * @param sect_num: Number of first sector to check
896 * @param sect_count: Number of sectors to check
897 * @param filename: Name of disk file we are checking (logging purpose)
898 * @param buffer: Allocated buffer for storing read data
899 * @param quiet: Flag for quiet mode
900 */
901static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
902 int sect_count, const char *filename,
903 uint8_t *buffer, bool quiet)
904{
905 int pnum, ret = 0;
906 ret = bdrv_read(bs, sect_num, buffer, sect_count);
907 if (ret < 0) {
908 error_report("Error while reading offset %" PRId64 " of %s: %s",
909 sectors_to_bytes(sect_num), filename, strerror(-ret));
910 return ret;
911 }
912 ret = is_allocated_sectors(buffer, sect_count, &pnum);
913 if (ret || pnum != sect_count) {
914 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
915 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
916 return 1;
917 }
918
919 return 0;
920}
921
922/*
923 * Compares two images. Exit codes:
924 *
925 * 0 - Images are identical
926 * 1 - Images differ
927 * >1 - Error occurred
928 */
929static int img_compare(int argc, char **argv)
930{
931 const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
932 BlockDriverState *bs1, *bs2;
933 int64_t total_sectors1, total_sectors2;
934 uint8_t *buf1 = NULL, *buf2 = NULL;
935 int pnum1, pnum2;
936 int allocated1, allocated2;
937 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
938 bool progress = false, quiet = false, strict = false;
939 int64_t total_sectors;
940 int64_t sector_num = 0;
941 int64_t nb_sectors;
942 int c, pnum;
943 uint64_t bs_sectors;
944 uint64_t progress_base;
945
946 for (;;) {
947 c = getopt(argc, argv, "hpf:F:sq");
948 if (c == -1) {
949 break;
950 }
951 switch (c) {
952 case '?':
953 case 'h':
954 help();
955 break;
956 case 'f':
957 fmt1 = optarg;
958 break;
959 case 'F':
960 fmt2 = optarg;
961 break;
962 case 'p':
963 progress = true;
964 break;
965 case 'q':
966 quiet = true;
967 break;
968 case 's':
969 strict = true;
970 break;
971 }
972 }
973
974 /* Progress is not shown in Quiet mode */
975 if (quiet) {
976 progress = false;
977 }
978
979
Kevin Wolffc11eb22013-08-05 10:53:04 +0200980 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800981 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100982 }
983 filename1 = argv[optind++];
984 filename2 = argv[optind++];
985
986 /* Initialize before goto out */
987 qemu_progress_init(progress, 2.0);
988
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200989 bs1 = bdrv_new_open("image 1", filename1, fmt1, BDRV_O_FLAGS, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100990 if (!bs1) {
991 error_report("Can't open file %s", filename1);
992 ret = 2;
993 goto out3;
994 }
995
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200996 bs2 = bdrv_new_open("image 2", filename2, fmt2, BDRV_O_FLAGS, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100997 if (!bs2) {
998 error_report("Can't open file %s", filename2);
999 ret = 2;
1000 goto out2;
1001 }
1002
1003 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
1004 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
1005 bdrv_get_geometry(bs1, &bs_sectors);
1006 total_sectors1 = bs_sectors;
1007 bdrv_get_geometry(bs2, &bs_sectors);
1008 total_sectors2 = bs_sectors;
1009 total_sectors = MIN(total_sectors1, total_sectors2);
1010 progress_base = MAX(total_sectors1, total_sectors2);
1011
1012 qemu_progress_print(0, 100);
1013
1014 if (strict && total_sectors1 != total_sectors2) {
1015 ret = 1;
1016 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1017 goto out;
1018 }
1019
1020 for (;;) {
1021 nb_sectors = sectors_to_process(total_sectors, sector_num);
1022 if (nb_sectors <= 0) {
1023 break;
1024 }
1025 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
1026 &pnum1);
1027 if (allocated1 < 0) {
1028 ret = 3;
1029 error_report("Sector allocation test failed for %s", filename1);
1030 goto out;
1031 }
1032
1033 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
1034 &pnum2);
1035 if (allocated2 < 0) {
1036 ret = 3;
1037 error_report("Sector allocation test failed for %s", filename2);
1038 goto out;
1039 }
1040 nb_sectors = MIN(pnum1, pnum2);
1041
1042 if (allocated1 == allocated2) {
1043 if (allocated1) {
1044 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
1045 if (ret < 0) {
1046 error_report("Error while reading offset %" PRId64 " of %s:"
1047 " %s", sectors_to_bytes(sector_num), filename1,
1048 strerror(-ret));
1049 ret = 4;
1050 goto out;
1051 }
1052 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1053 if (ret < 0) {
1054 error_report("Error while reading offset %" PRId64
1055 " of %s: %s", sectors_to_bytes(sector_num),
1056 filename2, strerror(-ret));
1057 ret = 4;
1058 goto out;
1059 }
1060 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1061 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001062 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1063 sectors_to_bytes(
1064 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001065 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001066 goto out;
1067 }
1068 }
1069 } else {
1070 if (strict) {
1071 ret = 1;
1072 qprintf(quiet, "Strict mode: Offset %" PRId64
1073 " allocation mismatch!\n",
1074 sectors_to_bytes(sector_num));
1075 goto out;
1076 }
1077
1078 if (allocated1) {
1079 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1080 filename1, buf1, quiet);
1081 } else {
1082 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1083 filename2, buf1, quiet);
1084 }
1085 if (ret) {
1086 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001087 error_report("Error while reading offset %" PRId64 ": %s",
1088 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001089 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001090 }
1091 goto out;
1092 }
1093 }
1094 sector_num += nb_sectors;
1095 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1096 }
1097
1098 if (total_sectors1 != total_sectors2) {
1099 BlockDriverState *bs_over;
1100 int64_t total_sectors_over;
1101 const char *filename_over;
1102
1103 qprintf(quiet, "Warning: Image size mismatch!\n");
1104 if (total_sectors1 > total_sectors2) {
1105 total_sectors_over = total_sectors1;
1106 bs_over = bs1;
1107 filename_over = filename1;
1108 } else {
1109 total_sectors_over = total_sectors2;
1110 bs_over = bs2;
1111 filename_over = filename2;
1112 }
1113
1114 for (;;) {
1115 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1116 if (nb_sectors <= 0) {
1117 break;
1118 }
1119 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1120 nb_sectors, &pnum);
1121 if (ret < 0) {
1122 ret = 3;
1123 error_report("Sector allocation test failed for %s",
1124 filename_over);
1125 goto out;
1126
1127 }
1128 nb_sectors = pnum;
1129 if (ret) {
1130 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1131 filename_over, buf1, quiet);
1132 if (ret) {
1133 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001134 error_report("Error while reading offset %" PRId64
1135 " of %s: %s", sectors_to_bytes(sector_num),
1136 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001137 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001138 }
1139 goto out;
1140 }
1141 }
1142 sector_num += nb_sectors;
1143 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1144 }
1145 }
1146
1147 qprintf(quiet, "Images are identical.\n");
1148 ret = 0;
1149
1150out:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001151 bdrv_unref(bs2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001152 qemu_vfree(buf1);
1153 qemu_vfree(buf2);
1154out2:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001155 bdrv_unref(bs1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001156out3:
1157 qemu_progress_end();
1158 return ret;
1159}
1160
bellardea2384d2004-08-01 21:59:26 +00001161static int img_convert(int argc, char **argv)
1162{
Peter Lieven24f833c2013-11-27 11:07:07 +01001163 int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001164 int64_t ret = 0;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001165 int progress = 0, flags;
1166 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001167 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001168 BlockDriverState **bs = NULL, *out_bs = NULL;
Peter Lieven802c3d42013-12-05 15:54:53 +01001169 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
ths96b8f132007-12-17 01:35:20 +00001170 uint64_t bs_sectors;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001171 uint8_t * buf = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001172 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardea2384d2004-08-01 21:59:26 +00001173 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +00001174 BlockDriverInfo bdi;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001175 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001176 QEMUOptionParameter *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001177 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001178 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001179 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001180 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001181 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001182 QemuOpts *sn_opts = NULL;
bellardea2384d2004-08-01 21:59:26 +00001183
1184 fmt = NULL;
1185 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001186 cache = "unsafe";
thsf58c7b32008-06-05 21:53:49 +00001187 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001188 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001189 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001190 for(;;) {
Wenchao Xiaef806542013-12-04 17:10:57 +08001191 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001192 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001193 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001194 }
bellardea2384d2004-08-01 21:59:26 +00001195 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001196 case '?':
bellardea2384d2004-08-01 21:59:26 +00001197 case 'h':
1198 help();
1199 break;
1200 case 'f':
1201 fmt = optarg;
1202 break;
1203 case 'O':
1204 out_fmt = optarg;
1205 break;
thsf58c7b32008-06-05 21:53:49 +00001206 case 'B':
1207 out_baseimg = optarg;
1208 break;
bellardea2384d2004-08-01 21:59:26 +00001209 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001210 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001211 break;
1212 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001213 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001214 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001215 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001216 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001217 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001218 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001219 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001220 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001221 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001222 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001223 if (!is_valid_option_list(optarg)) {
1224 error_report("Invalid option list: %s", optarg);
1225 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001226 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001227 }
1228 if (!options) {
1229 options = g_strdup(optarg);
1230 } else {
1231 char *old_options = options;
1232 options = g_strdup_printf("%s,%s", options, optarg);
1233 g_free(old_options);
1234 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001235 break;
edison51ef6722010-09-21 19:58:41 -07001236 case 's':
1237 snapshot_name = optarg;
1238 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001239 case 'l':
1240 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1241 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
1242 if (!sn_opts) {
1243 error_report("Failed in parsing snapshot param '%s'",
1244 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001245 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001246 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001247 }
1248 } else {
1249 snapshot_name = optarg;
1250 }
1251 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001252 case 'S':
1253 {
1254 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001255 char *end;
1256 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1257 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001258 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001259 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001260 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001261 }
1262
1263 min_sparse = sval / BDRV_SECTOR_SIZE;
1264 break;
1265 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001266 case 'p':
1267 progress = 1;
1268 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001269 case 't':
1270 cache = optarg;
1271 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001272 case 'q':
1273 quiet = true;
1274 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001275 case 'n':
1276 skip_create = 1;
1277 break;
bellardea2384d2004-08-01 21:59:26 +00001278 }
1279 }
ths3b46e622007-09-17 08:09:54 +00001280
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001281 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001282 if (quiet) {
1283 progress = 0;
1284 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001285 qemu_progress_init(progress, 1.0);
1286
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001287
balrog926c2d22007-10-31 01:11:44 +00001288 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001289 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001290
Kevin Wolf2dc83282014-02-21 16:24:05 +01001291 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001292 ret = print_block_option_help(out_filename, out_fmt);
1293 goto out;
1294 }
1295
bellardea2384d2004-08-01 21:59:26 +00001296 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001297 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001298 }
bellardea2384d2004-08-01 21:59:26 +00001299
thsf58c7b32008-06-05 21:53:49 +00001300
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001301 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001302 error_report("-B makes no sense when concatenating multiple input "
1303 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001304 ret = -1;
1305 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001306 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001307
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001308 qemu_progress_print(0, 100);
1309
Anthony Liguori7267c092011-08-20 22:09:37 -05001310 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
balrog926c2d22007-10-31 01:11:44 +00001311
1312 total_sectors = 0;
1313 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001314 char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
1315 : g_strdup("source");
1316 bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, BDRV_O_FLAGS,
1317 true, quiet);
1318 g_free(id);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001319 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001320 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001321 ret = -1;
1322 goto out;
1323 }
balrog926c2d22007-10-31 01:11:44 +00001324 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1325 total_sectors += bs_sectors;
1326 }
bellardea2384d2004-08-01 21:59:26 +00001327
Wenchao Xiaef806542013-12-04 17:10:57 +08001328 if (sn_opts) {
1329 ret = bdrv_snapshot_load_tmp(bs[0],
1330 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1331 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1332 &local_err);
1333 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001334 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001335 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001336 ret = -1;
1337 goto out;
1338 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001339
1340 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001341 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001342 if (local_err) {
Wenchao Xiaef806542013-12-04 17:10:57 +08001343 error_report("Failed to load snapshot: %s",
1344 error_get_pretty(local_err));
1345 error_free(local_err);
1346 ret = -1;
1347 goto out;
edison51ef6722010-09-21 19:58:41 -07001348 }
1349
Kevin Wolfefa84d42009-05-18 16:42:12 +02001350 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001351 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001352 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001353 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001354 ret = -1;
1355 goto out;
1356 }
balrog926c2d22007-10-31 01:11:44 +00001357
Kevin Wolf98289622013-07-10 15:47:39 +02001358 proto_drv = bdrv_find_protocol(out_filename, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001359 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001360 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001361 ret = -1;
1362 goto out;
1363 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001364
1365 create_options = append_option_parameters(create_options,
1366 drv->create_options);
1367 create_options = append_option_parameters(create_options,
1368 proto_drv->create_options);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001369
Kevin Wolfefa84d42009-05-18 16:42:12 +02001370 if (options) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001371 param = parse_option_parameters(options, create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001372 if (param == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001373 error_report("Invalid options for file format '%s'.", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001374 ret = -1;
1375 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001376 }
1377 } else {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001378 param = parse_option_parameters("", create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001379 }
1380
1381 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
Jes Sorenseneec77d92010-12-07 17:44:34 +01001382 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001383 if (ret < 0) {
1384 goto out;
1385 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001386
Kevin Wolfa18953f2010-10-14 15:46:04 +02001387 /* Get backing file name if -o backing_file was used */
1388 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1389 if (out_baseimg_param) {
1390 out_baseimg = out_baseimg_param->value.s;
1391 }
1392
Kevin Wolfefa84d42009-05-18 16:42:12 +02001393 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001394 if (compress) {
Kevin Wolfefa84d42009-05-18 16:42:12 +02001395 QEMUOptionParameter *encryption =
1396 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
Kevin Wolf41521fa2011-10-18 16:19:42 +02001397 QEMUOptionParameter *preallocation =
1398 get_option_parameter(param, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001399
1400 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001401 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001402 ret = -1;
1403 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001404 }
1405
1406 if (encryption && encryption->value.n) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001407 error_report("Compression and encryption not supported at "
1408 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001409 ret = -1;
1410 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001411 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001412
1413 if (preallocation && preallocation->value.s
1414 && strcmp(preallocation->value.s, "off"))
1415 {
1416 error_report("Compression and preallocation not supported at "
1417 "the same time");
1418 ret = -1;
1419 goto out;
1420 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001421 }
1422
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001423 if (!skip_create) {
1424 /* Create the new image */
Max Reitzcc84d902013-09-06 17:14:26 +02001425 ret = bdrv_create(drv, out_filename, param, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001426 if (ret < 0) {
Max Reitzcc84d902013-09-06 17:14:26 +02001427 error_report("%s: error while converting %s: %s",
1428 out_filename, out_fmt, error_get_pretty(local_err));
1429 error_free(local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001430 goto out;
bellardea2384d2004-08-01 21:59:26 +00001431 }
1432 }
ths3b46e622007-09-17 08:09:54 +00001433
Peter Lieven5a37b602013-10-24 12:07:06 +02001434 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001435 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001436 if (ret < 0) {
1437 error_report("Invalid cache option: %s", cache);
1438 return -1;
1439 }
1440
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001441 out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001442 if (!out_bs) {
1443 ret = -1;
1444 goto out;
1445 }
bellardea2384d2004-08-01 21:59:26 +00001446
balrog926c2d22007-10-31 01:11:44 +00001447 bs_i = 0;
1448 bs_offset = 0;
1449 bdrv_get_geometry(bs[0], &bs_sectors);
Peter Lievenf2521c92013-11-27 11:07:06 +01001450
1451 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1452 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1453 * as maximum. */
1454 bufsectors = MIN(32768,
1455 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1456 out_bs->bl.discard_alignment))
1457 );
1458
1459 buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001460
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001461 if (skip_create) {
1462 int64_t output_length = bdrv_getlength(out_bs);
1463 if (output_length < 0) {
1464 error_report("unable to get output image length: %s\n",
1465 strerror(-output_length));
1466 ret = -1;
1467 goto out;
1468 } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
1469 error_report("output file is smaller than input file");
1470 ret = -1;
1471 goto out;
1472 }
1473 }
1474
Peter Lieven24f833c2013-11-27 11:07:07 +01001475 cluster_sectors = 0;
1476 ret = bdrv_get_info(out_bs, &bdi);
1477 if (ret < 0) {
1478 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001479 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001480 goto out;
1481 }
Peter Lieven24f833c2013-11-27 11:07:07 +01001482 } else {
1483 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1484 }
1485
1486 if (compress) {
1487 if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001488 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001489 ret = -1;
1490 goto out;
1491 }
bellardea2384d2004-08-01 21:59:26 +00001492 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001493
1494 nb_sectors = total_sectors;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001495
bellardea2384d2004-08-01 21:59:26 +00001496 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001497 int64_t bs_num;
1498 int remainder;
1499 uint8_t *buf2;
1500
bellardea2384d2004-08-01 21:59:26 +00001501 nb_sectors = total_sectors - sector_num;
1502 if (nb_sectors <= 0)
1503 break;
1504 if (nb_sectors >= cluster_sectors)
1505 n = cluster_sectors;
1506 else
1507 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001508
1509 bs_num = sector_num - bs_offset;
1510 assert (bs_num >= 0);
1511 remainder = n;
1512 buf2 = buf;
1513 while (remainder > 0) {
1514 int nlow;
1515 while (bs_num == bs_sectors) {
1516 bs_i++;
1517 assert (bs_i < bs_n);
1518 bs_offset += bs_sectors;
1519 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1520 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001521 /* printf("changing part: sector_num=%" PRId64 ", "
1522 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1523 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
balrog926c2d22007-10-31 01:11:44 +00001524 }
1525 assert (bs_num < bs_sectors);
1526
1527 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1528
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001529 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1530 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001531 error_report("error while reading sector %" PRId64 ": %s",
1532 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001533 goto out;
1534 }
balrog926c2d22007-10-31 01:11:44 +00001535
1536 buf2 += nlow * 512;
1537 bs_num += nlow;
1538
1539 remainder -= nlow;
1540 }
1541 assert (remainder == 0);
1542
Stefan Hajnoczi54f106d2013-04-15 17:17:33 +02001543 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1544 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001545 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001546 error_report("error while compressing sector %" PRId64
1547 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001548 goto out;
1549 }
bellardea2384d2004-08-01 21:59:26 +00001550 }
1551 sector_num += n;
Peter Lieven13c28af2013-11-27 11:07:01 +01001552 qemu_progress_print(100.0 * sector_num / total_sectors, 0);
bellardea2384d2004-08-01 21:59:26 +00001553 }
bellardfaea38e2006-08-05 21:31:00 +00001554 /* signal EOF to align */
1555 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001556 } else {
Peter Lieven802c3d42013-12-05 15:54:53 +01001557 int64_t sectors_to_read, sectors_read, sector_num_next_status;
1558 bool count_allocated_sectors;
Peter Lieven11b66992013-10-24 12:07:05 +02001559 int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02001560
Peter Lieven5a37b602013-10-24 12:07:06 +02001561 if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
1562 ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
1563 if (ret < 0) {
1564 goto out;
1565 }
1566 has_zero_init = 1;
1567 }
1568
Peter Lieven802c3d42013-12-05 15:54:53 +01001569 sectors_to_read = total_sectors;
1570 count_allocated_sectors = progress && (out_baseimg || has_zero_init);
1571restart:
thsf58c7b32008-06-05 21:53:49 +00001572 sector_num = 0; // total number of sectors converted so far
Peter Lieven802c3d42013-12-05 15:54:53 +01001573 sectors_read = 0;
1574 sector_num_next_status = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001575
bellardea2384d2004-08-01 21:59:26 +00001576 for(;;) {
1577 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001578 if (nb_sectors <= 0) {
Peter Lieven802c3d42013-12-05 15:54:53 +01001579 if (count_allocated_sectors) {
1580 sectors_to_read = sectors_read;
1581 count_allocated_sectors = false;
1582 goto restart;
1583 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001584 ret = 0;
bellardea2384d2004-08-01 21:59:26 +00001585 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001586 }
balrog926c2d22007-10-31 01:11:44 +00001587
1588 while (sector_num - bs_offset >= bs_sectors) {
1589 bs_i ++;
1590 assert (bs_i < bs_n);
1591 bs_offset += bs_sectors;
1592 bdrv_get_geometry(bs[bs_i], &bs_sectors);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001593 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1594 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
balrog926c2d22007-10-31 01:11:44 +00001595 sector_num, bs_i, bs_offset, bs_sectors); */
1596 }
1597
Peter Lieven13c28af2013-11-27 11:07:01 +01001598 if ((out_baseimg || has_zero_init) &&
1599 sector_num >= sector_num_next_status) {
1600 n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors;
1601 ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset,
1602 n, &n1);
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001603 if (ret < 0) {
Peter Lieven13c28af2013-11-27 11:07:01 +01001604 error_report("error while reading block status of sector %"
1605 PRId64 ": %s", sector_num - bs_offset,
1606 strerror(-ret));
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001607 goto out;
aliguori93c65b42009-04-05 17:40:43 +00001608 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001609 /* If the output image is zero initialized, we are not working
1610 * on a shared base and the input is zero we can skip the next
1611 * n1 sectors */
1612 if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) {
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001613 sector_num += n1;
1614 continue;
1615 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001616 /* If the output image is being created as a copy on write
1617 * image, assume that sectors which are unallocated in the
1618 * input image are present in both the output's and input's
1619 * base images (no need to copy them). */
1620 if (out_baseimg) {
1621 if (!(ret & BDRV_BLOCK_DATA)) {
1622 sector_num += n1;
1623 continue;
1624 }
1625 /* The next 'n1' sectors are allocated in the input image.
1626 * Copy only those as they may be followed by unallocated
1627 * sectors. */
1628 nb_sectors = n1;
1629 }
1630 /* avoid redundant callouts to get_block_status */
1631 sector_num_next_status = sector_num + n1;
thsf58c7b32008-06-05 21:53:49 +00001632 }
1633
Peter Lievenf2521c92013-11-27 11:07:06 +01001634 n = MIN(nb_sectors, bufsectors);
Peter Lieven24f833c2013-11-27 11:07:07 +01001635
1636 /* round down request length to an aligned sector, but
1637 * do not bother doing this on short requests. They happen
1638 * when we found an all-zero area, and the next sector to
1639 * write will not be sector_num + n. */
1640 if (cluster_sectors > 0 && n >= cluster_sectors) {
1641 int64_t next_aligned_sector = (sector_num + n);
1642 next_aligned_sector -= next_aligned_sector % cluster_sectors;
1643 if (sector_num + n > next_aligned_sector) {
1644 n = next_aligned_sector - sector_num;
1645 }
1646 }
1647
Peter Lieven13c28af2013-11-27 11:07:01 +01001648 n = MIN(n, bs_sectors - (sector_num - bs_offset));
Peter Lieven13c28af2013-11-27 11:07:01 +01001649
Peter Lieven802c3d42013-12-05 15:54:53 +01001650 sectors_read += n;
1651 if (count_allocated_sectors) {
1652 sector_num += n;
1653 continue;
1654 }
1655
1656 n1 = n;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001657 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1658 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001659 error_report("error while reading sector %" PRId64 ": %s",
1660 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001661 goto out;
1662 }
bellardea2384d2004-08-01 21:59:26 +00001663 /* NOTE: at the same time we convert, we do not write zero
1664 sectors to have a chance to compress the image. Ideally, we
1665 should add a specific call to have the info to go faster */
1666 buf1 = buf;
1667 while (n > 0) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02001668 if (!has_zero_init ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001669 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001670 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1671 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001672 error_report("error while writing sector %" PRId64
1673 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001674 goto out;
1675 }
bellardea2384d2004-08-01 21:59:26 +00001676 }
1677 sector_num += n1;
1678 n -= n1;
1679 buf1 += n1 * 512;
1680 }
Peter Lieven802c3d42013-12-05 15:54:53 +01001681 qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
bellardea2384d2004-08-01 21:59:26 +00001682 }
1683 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001684out:
Peter Lieven13c28af2013-11-27 11:07:01 +01001685 if (!ret) {
1686 qemu_progress_print(100, 0);
1687 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001688 qemu_progress_end();
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001689 free_option_parameters(create_options);
1690 free_option_parameters(param);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001691 qemu_vfree(buf);
Wenchao Xiaef806542013-12-04 17:10:57 +08001692 if (sn_opts) {
1693 qemu_opts_del(sn_opts);
1694 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001695 if (out_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001696 bdrv_unref(out_bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001697 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001698 if (bs) {
1699 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1700 if (bs[bs_i]) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001701 bdrv_unref(bs[bs_i]);
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001702 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001703 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001704 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001705 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001706fail_getopt:
1707 g_free(options);
1708
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001709 if (ret) {
1710 return 1;
1711 }
bellardea2384d2004-08-01 21:59:26 +00001712 return 0;
1713}
1714
bellard57d1a2b2004-08-03 21:15:11 +00001715
bellardfaea38e2006-08-05 21:31:00 +00001716static void dump_snapshots(BlockDriverState *bs)
1717{
1718 QEMUSnapshotInfo *sn_tab, *sn;
1719 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00001720
1721 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1722 if (nb_sns <= 0)
1723 return;
1724 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08001725 bdrv_snapshot_dump(fprintf, stdout, NULL);
1726 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001727 for(i = 0; i < nb_sns; i++) {
1728 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08001729 bdrv_snapshot_dump(fprintf, stdout, sn);
1730 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001731 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001732 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001733}
1734
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001735static void dump_json_image_info_list(ImageInfoList *list)
1736{
Markus Armbruster4399c432014-04-25 16:50:32 +02001737 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001738 QString *str;
1739 QmpOutputVisitor *ov = qmp_output_visitor_new();
1740 QObject *obj;
1741 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +02001742 &list, NULL, &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001743 obj = qmp_output_get_qobject(ov);
1744 str = qobject_to_json_pretty(obj);
1745 assert(str != NULL);
1746 printf("%s\n", qstring_get_str(str));
1747 qobject_decref(obj);
1748 qmp_output_visitor_cleanup(ov);
1749 QDECREF(str);
1750}
1751
Benoît Canetc054b3f2012-09-05 13:09:02 +02001752static void dump_json_image_info(ImageInfo *info)
1753{
Markus Armbruster4399c432014-04-25 16:50:32 +02001754 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001755 QString *str;
1756 QmpOutputVisitor *ov = qmp_output_visitor_new();
1757 QObject *obj;
1758 visit_type_ImageInfo(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +02001759 &info, NULL, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001760 obj = qmp_output_get_qobject(ov);
1761 str = qobject_to_json_pretty(obj);
1762 assert(str != NULL);
1763 printf("%s\n", qstring_get_str(str));
1764 qobject_decref(obj);
1765 qmp_output_visitor_cleanup(ov);
1766 QDECREF(str);
1767}
1768
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001769static void dump_human_image_info_list(ImageInfoList *list)
1770{
1771 ImageInfoList *elem;
1772 bool delim = false;
1773
1774 for (elem = list; elem; elem = elem->next) {
1775 if (delim) {
1776 printf("\n");
1777 }
1778 delim = true;
1779
Wenchao Xia5b917042013-05-25 11:09:45 +08001780 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001781 }
1782}
1783
1784static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1785{
1786 return strcmp(a, b) == 0;
1787}
1788
1789/**
1790 * Open an image file chain and return an ImageInfoList
1791 *
1792 * @filename: topmost image filename
1793 * @fmt: topmost image format (may be NULL to autodetect)
1794 * @chain: true - enumerate entire backing file chain
1795 * false - only topmost image file
1796 *
1797 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1798 * image file. If there was an error a message will have been printed to
1799 * stderr.
1800 */
1801static ImageInfoList *collect_image_info_list(const char *filename,
1802 const char *fmt,
1803 bool chain)
1804{
1805 ImageInfoList *head = NULL;
1806 ImageInfoList **last = &head;
1807 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08001808 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001809
1810 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1811
1812 while (filename) {
1813 BlockDriverState *bs;
1814 ImageInfo *info;
1815 ImageInfoList *elem;
1816
1817 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1818 error_report("Backing file '%s' creates an infinite loop.",
1819 filename);
1820 goto err;
1821 }
1822 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1823
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001824 bs = bdrv_new_open("image", filename, fmt,
1825 BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001826 if (!bs) {
1827 goto err;
1828 }
1829
Wenchao Xia43526ec2013-06-06 12:27:58 +08001830 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001831 if (err) {
Wenchao Xia43526ec2013-06-06 12:27:58 +08001832 error_report("%s", error_get_pretty(err));
1833 error_free(err);
Prasad Joshibdf866f2014-03-26 01:55:53 +05301834 bdrv_unref(bs);
Wenchao Xia43526ec2013-06-06 12:27:58 +08001835 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08001836 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001837
1838 elem = g_new0(ImageInfoList, 1);
1839 elem->value = info;
1840 *last = elem;
1841 last = &elem->next;
1842
Fam Zheng4f6fd342013-08-23 09:14:47 +08001843 bdrv_unref(bs);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001844
1845 filename = fmt = NULL;
1846 if (chain) {
1847 if (info->has_full_backing_filename) {
1848 filename = info->full_backing_filename;
1849 } else if (info->has_backing_filename) {
1850 filename = info->backing_filename;
1851 }
1852 if (info->has_backing_filename_format) {
1853 fmt = info->backing_filename_format;
1854 }
1855 }
1856 }
1857 g_hash_table_destroy(filenames);
1858 return head;
1859
1860err:
1861 qapi_free_ImageInfoList(head);
1862 g_hash_table_destroy(filenames);
1863 return NULL;
1864}
1865
Benoît Canetc054b3f2012-09-05 13:09:02 +02001866static int img_info(int argc, char **argv)
1867{
1868 int c;
1869 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001870 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001871 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001872 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001873
bellardea2384d2004-08-01 21:59:26 +00001874 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001875 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001876 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001877 int option_index = 0;
1878 static const struct option long_options[] = {
1879 {"help", no_argument, 0, 'h'},
1880 {"format", required_argument, 0, 'f'},
1881 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001882 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001883 {0, 0, 0, 0}
1884 };
1885 c = getopt_long(argc, argv, "f:h",
1886 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001887 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001888 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001889 }
bellardea2384d2004-08-01 21:59:26 +00001890 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001891 case '?':
bellardea2384d2004-08-01 21:59:26 +00001892 case 'h':
1893 help();
1894 break;
1895 case 'f':
1896 fmt = optarg;
1897 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001898 case OPTION_OUTPUT:
1899 output = optarg;
1900 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001901 case OPTION_BACKING_CHAIN:
1902 chain = true;
1903 break;
bellardea2384d2004-08-01 21:59:26 +00001904 }
1905 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02001906 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001907 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001908 }
bellardea2384d2004-08-01 21:59:26 +00001909 filename = argv[optind++];
1910
Benoît Canetc054b3f2012-09-05 13:09:02 +02001911 if (output && !strcmp(output, "json")) {
1912 output_format = OFORMAT_JSON;
1913 } else if (output && !strcmp(output, "human")) {
1914 output_format = OFORMAT_HUMAN;
1915 } else if (output) {
1916 error_report("--output must be used with human or json as argument.");
1917 return 1;
1918 }
1919
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001920 list = collect_image_info_list(filename, fmt, chain);
1921 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001922 return 1;
1923 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001924
Benoît Canetc054b3f2012-09-05 13:09:02 +02001925 switch (output_format) {
1926 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001927 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001928 break;
1929 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001930 if (chain) {
1931 dump_json_image_info_list(list);
1932 } else {
1933 dump_json_image_info(list->value);
1934 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001935 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001936 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001937
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001938 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001939 return 0;
1940}
1941
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02001942
1943typedef struct MapEntry {
1944 int flags;
1945 int depth;
1946 int64_t start;
1947 int64_t length;
1948 int64_t offset;
1949 BlockDriverState *bs;
1950} MapEntry;
1951
1952static void dump_map_entry(OutputFormat output_format, MapEntry *e,
1953 MapEntry *next)
1954{
1955 switch (output_format) {
1956 case OFORMAT_HUMAN:
1957 if ((e->flags & BDRV_BLOCK_DATA) &&
1958 !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
1959 error_report("File contains external, encrypted or compressed clusters.");
1960 exit(1);
1961 }
1962 if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
1963 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
1964 e->start, e->length, e->offset, e->bs->filename);
1965 }
1966 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
1967 * Modify the flags here to allow more coalescing.
1968 */
1969 if (next &&
1970 (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
1971 next->flags &= ~BDRV_BLOCK_DATA;
1972 next->flags |= BDRV_BLOCK_ZERO;
1973 }
1974 break;
1975 case OFORMAT_JSON:
1976 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
1977 " \"zero\": %s, \"data\": %s",
1978 (e->start == 0 ? "[" : ",\n"),
1979 e->start, e->length, e->depth,
1980 (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
1981 (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
1982 if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02001983 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02001984 }
1985 putchar('}');
1986
1987 if (!next) {
1988 printf("]\n");
1989 }
1990 break;
1991 }
1992}
1993
1994static int get_block_status(BlockDriverState *bs, int64_t sector_num,
1995 int nb_sectors, MapEntry *e)
1996{
1997 int64_t ret;
1998 int depth;
1999
2000 /* As an optimization, we could cache the current range of unallocated
2001 * clusters in each file of the chain, and avoid querying the same
2002 * range repeatedly.
2003 */
2004
2005 depth = 0;
2006 for (;;) {
2007 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
2008 if (ret < 0) {
2009 return ret;
2010 }
2011 assert(nb_sectors);
2012 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2013 break;
2014 }
2015 bs = bs->backing_hd;
2016 if (bs == NULL) {
2017 ret = 0;
2018 break;
2019 }
2020
2021 depth++;
2022 }
2023
2024 e->start = sector_num * BDRV_SECTOR_SIZE;
2025 e->length = nb_sectors * BDRV_SECTOR_SIZE;
2026 e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
2027 e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
2028 e->depth = depth;
2029 e->bs = bs;
2030 return 0;
2031}
2032
2033static int img_map(int argc, char **argv)
2034{
2035 int c;
2036 OutputFormat output_format = OFORMAT_HUMAN;
2037 BlockDriverState *bs;
2038 const char *filename, *fmt, *output;
2039 int64_t length;
2040 MapEntry curr = { .length = 0 }, next;
2041 int ret = 0;
2042
2043 fmt = NULL;
2044 output = NULL;
2045 for (;;) {
2046 int option_index = 0;
2047 static const struct option long_options[] = {
2048 {"help", no_argument, 0, 'h'},
2049 {"format", required_argument, 0, 'f'},
2050 {"output", required_argument, 0, OPTION_OUTPUT},
2051 {0, 0, 0, 0}
2052 };
2053 c = getopt_long(argc, argv, "f:h",
2054 long_options, &option_index);
2055 if (c == -1) {
2056 break;
2057 }
2058 switch (c) {
2059 case '?':
2060 case 'h':
2061 help();
2062 break;
2063 case 'f':
2064 fmt = optarg;
2065 break;
2066 case OPTION_OUTPUT:
2067 output = optarg;
2068 break;
2069 }
2070 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002071 if (optind != argc - 1) {
2072 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002073 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002074 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002075
2076 if (output && !strcmp(output, "json")) {
2077 output_format = OFORMAT_JSON;
2078 } else if (output && !strcmp(output, "human")) {
2079 output_format = OFORMAT_HUMAN;
2080 } else if (output) {
2081 error_report("--output must be used with human or json as argument.");
2082 return 1;
2083 }
2084
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002085 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002086 if (!bs) {
2087 return 1;
2088 }
2089
2090 if (output_format == OFORMAT_HUMAN) {
2091 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2092 }
2093
2094 length = bdrv_getlength(bs);
2095 while (curr.start + curr.length < length) {
2096 int64_t nsectors_left;
2097 int64_t sector_num;
2098 int n;
2099
2100 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2101
2102 /* Probe up to 1 GiB at a time. */
2103 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2104 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2105 ret = get_block_status(bs, sector_num, n, &next);
2106
2107 if (ret < 0) {
2108 error_report("Could not read file metadata: %s", strerror(-ret));
2109 goto out;
2110 }
2111
2112 if (curr.length != 0 && curr.flags == next.flags &&
2113 curr.depth == next.depth &&
2114 ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
2115 curr.offset + curr.length == next.offset)) {
2116 curr.length += next.length;
2117 continue;
2118 }
2119
2120 if (curr.length > 0) {
2121 dump_map_entry(output_format, &curr, &next);
2122 }
2123 curr = next;
2124 }
2125
2126 dump_map_entry(output_format, &curr, NULL);
2127
2128out:
2129 bdrv_unref(bs);
2130 return ret < 0;
2131}
2132
aliguorif7b4a942009-01-07 17:40:15 +00002133#define SNAPSHOT_LIST 1
2134#define SNAPSHOT_CREATE 2
2135#define SNAPSHOT_APPLY 3
2136#define SNAPSHOT_DELETE 4
2137
Stuart Brady153859b2009-06-07 00:42:17 +01002138static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002139{
2140 BlockDriverState *bs;
2141 QEMUSnapshotInfo sn;
2142 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002143 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002144 int action = 0;
2145 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002146 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002147 Error *err = NULL;
aliguorif7b4a942009-01-07 17:40:15 +00002148
Kevin Wolf710da702011-01-10 12:33:02 +01002149 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002150 /* Parse commandline parameters */
2151 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002152 c = getopt(argc, argv, "la:c:d:hq");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002153 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002154 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002155 }
aliguorif7b4a942009-01-07 17:40:15 +00002156 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002157 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002158 case 'h':
2159 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002160 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002161 case 'l':
2162 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002163 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002164 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002165 }
2166 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002167 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002168 break;
2169 case 'a':
2170 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002171 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002172 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002173 }
2174 action = SNAPSHOT_APPLY;
2175 snapshot_name = optarg;
2176 break;
2177 case 'c':
2178 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002179 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002180 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002181 }
2182 action = SNAPSHOT_CREATE;
2183 snapshot_name = optarg;
2184 break;
2185 case 'd':
2186 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002187 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002188 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002189 }
2190 action = SNAPSHOT_DELETE;
2191 snapshot_name = optarg;
2192 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002193 case 'q':
2194 quiet = true;
2195 break;
aliguorif7b4a942009-01-07 17:40:15 +00002196 }
2197 }
2198
Kevin Wolffc11eb22013-08-05 10:53:04 +02002199 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002200 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002201 }
aliguorif7b4a942009-01-07 17:40:15 +00002202 filename = argv[optind++];
2203
2204 /* Open the image */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002205 bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002206 if (!bs) {
2207 return 1;
2208 }
aliguorif7b4a942009-01-07 17:40:15 +00002209
2210 /* Perform the requested action */
2211 switch(action) {
2212 case SNAPSHOT_LIST:
2213 dump_snapshots(bs);
2214 break;
2215
2216 case SNAPSHOT_CREATE:
2217 memset(&sn, 0, sizeof(sn));
2218 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2219
2220 qemu_gettimeofday(&tv);
2221 sn.date_sec = tv.tv_sec;
2222 sn.date_nsec = tv.tv_usec * 1000;
2223
2224 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002225 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002226 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002227 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002228 }
aliguorif7b4a942009-01-07 17:40:15 +00002229 break;
2230
2231 case SNAPSHOT_APPLY:
2232 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002233 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002234 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002235 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002236 }
aliguorif7b4a942009-01-07 17:40:15 +00002237 break;
2238
2239 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002240 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002241 if (err) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002242 error_report("Could not delete snapshot '%s': (%s)",
2243 snapshot_name, error_get_pretty(err));
2244 error_free(err);
2245 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002246 }
aliguorif7b4a942009-01-07 17:40:15 +00002247 break;
2248 }
2249
2250 /* Cleanup */
Fam Zheng4f6fd342013-08-23 09:14:47 +08002251 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002252 if (ret) {
2253 return 1;
2254 }
Stuart Brady153859b2009-06-07 00:42:17 +01002255 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002256}
2257
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002258static int img_rebase(int argc, char **argv)
2259{
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002260 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01002261 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002262 char *filename;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002263 const char *fmt, *cache, *out_basefmt, *out_baseimg;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002264 int c, flags, ret;
2265 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002266 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002267 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002268 Error *local_err = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002269
2270 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002271 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002272 cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002273 out_baseimg = NULL;
2274 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002275 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002276 c = getopt(argc, argv, "uhf:F:b:pt:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002277 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002278 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002279 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002280 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002281 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002282 case 'h':
2283 help();
2284 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002285 case 'f':
2286 fmt = optarg;
2287 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002288 case 'F':
2289 out_basefmt = optarg;
2290 break;
2291 case 'b':
2292 out_baseimg = optarg;
2293 break;
2294 case 'u':
2295 unsafe = 1;
2296 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002297 case 'p':
2298 progress = 1;
2299 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002300 case 't':
2301 cache = optarg;
2302 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002303 case 'q':
2304 quiet = true;
2305 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002306 }
2307 }
2308
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002309 if (quiet) {
2310 progress = 0;
2311 }
2312
Fam Zhengac1307a2014-04-22 13:36:11 +08002313 if (optind != argc - 1) {
2314 error_exit("Expecting one image file name");
2315 }
2316 if (!unsafe && !out_baseimg) {
2317 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002318 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002319 filename = argv[optind++];
2320
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002321 qemu_progress_init(progress, 2.0);
2322 qemu_progress_print(0, 100);
2323
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002324 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002325 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002326 if (ret < 0) {
2327 error_report("Invalid cache option: %s", cache);
2328 return -1;
2329 }
2330
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002331 /*
2332 * Open the images.
2333 *
2334 * Ignore the old backing file for unsafe rebase in case we want to correct
2335 * the reference to a renamed or moved backing file.
2336 */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002337 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002338 if (!bs) {
2339 return 1;
2340 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002341
2342 /* Find the right drivers for the backing files */
2343 old_backing_drv = NULL;
2344 new_backing_drv = NULL;
2345
2346 if (!unsafe && bs->backing_format[0] != '\0') {
2347 old_backing_drv = bdrv_find_format(bs->backing_format);
2348 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002349 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002350 ret = -1;
2351 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002352 }
2353 }
2354
2355 if (out_basefmt != NULL) {
2356 new_backing_drv = bdrv_find_format(out_basefmt);
2357 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002358 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002359 ret = -1;
2360 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002361 }
2362 }
2363
2364 /* For safe rebasing we need to compare old and new backing file */
2365 if (unsafe) {
2366 /* Make the compiler happy */
2367 bs_old_backing = NULL;
2368 bs_new_backing = NULL;
2369 } else {
2370 char backing_name[1024];
2371
Kevin Wolf98522f62014-04-17 13:16:01 +02002372 bs_old_backing = bdrv_new("old_backing", &error_abort);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002373 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzddf56362014-02-18 18:33:06 +01002374 ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, BDRV_O_FLAGS,
Max Reitz34b5d2c2013-09-05 14:45:29 +02002375 old_backing_drv, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002376 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002377 error_report("Could not open old backing file '%s': %s",
2378 backing_name, error_get_pretty(local_err));
2379 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002380 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002381 }
Alex Bligha6166732012-10-16 13:46:18 +01002382 if (out_baseimg[0]) {
Kevin Wolf98522f62014-04-17 13:16:01 +02002383 bs_new_backing = bdrv_new("new_backing", &error_abort);
Max Reitzddf56362014-02-18 18:33:06 +01002384 ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL,
2385 BDRV_O_FLAGS, new_backing_drv, &local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002386 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002387 error_report("Could not open new backing file '%s': %s",
2388 out_baseimg, error_get_pretty(local_err));
2389 error_free(local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002390 goto out;
2391 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002392 }
2393 }
2394
2395 /*
2396 * Check each unallocated cluster in the COW file. If it is unallocated,
2397 * accesses go to the backing file. We must therefore compare this cluster
2398 * in the old and new backing file, and if they differ we need to copy it
2399 * from the old backing file into the COW file.
2400 *
2401 * If qemu-img crashes during this step, no harm is done. The content of
2402 * the image is the same as the original one at any time.
2403 */
2404 if (!unsafe) {
2405 uint64_t num_sectors;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002406 uint64_t old_backing_num_sectors;
Alex Bligha6166732012-10-16 13:46:18 +01002407 uint64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002408 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002409 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08002410 uint8_t * buf_old;
2411 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02002412 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002413
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002414 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2415 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002416
2417 bdrv_get_geometry(bs, &num_sectors);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002418 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
Alex Bligha6166732012-10-16 13:46:18 +01002419 if (bs_new_backing) {
2420 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2421 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002422
Kevin Wolf1f710492012-10-12 14:29:18 +02002423 if (num_sectors != 0) {
2424 local_progress = (float)100 /
2425 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2426 }
2427
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002428 for (sector = 0; sector < num_sectors; sector += n) {
2429
2430 /* How many sectors can we handle with the next read? */
2431 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2432 n = (IO_BUF_SIZE / 512);
2433 } else {
2434 n = num_sectors - sector;
2435 }
2436
2437 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02002438 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02002439 if (ret < 0) {
2440 error_report("error while reading image metadata: %s",
2441 strerror(-ret));
2442 goto out;
2443 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02002444 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002445 continue;
2446 }
2447
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002448 /*
2449 * Read old and new backing file and take into consideration that
2450 * backing files may be smaller than the COW image.
2451 */
2452 if (sector >= old_backing_num_sectors) {
2453 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2454 } else {
2455 if (sector + n > old_backing_num_sectors) {
2456 n = old_backing_num_sectors - sector;
2457 }
2458
2459 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2460 if (ret < 0) {
2461 error_report("error while reading from old backing file");
2462 goto out;
2463 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002464 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002465
Alex Bligha6166732012-10-16 13:46:18 +01002466 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002467 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2468 } else {
2469 if (sector + n > new_backing_num_sectors) {
2470 n = new_backing_num_sectors - sector;
2471 }
2472
2473 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2474 if (ret < 0) {
2475 error_report("error while reading from new backing file");
2476 goto out;
2477 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002478 }
2479
2480 /* If they differ, we need to write to the COW file */
2481 uint64_t written = 0;
2482
2483 while (written < n) {
2484 int pnum;
2485
2486 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01002487 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002488 {
2489 ret = bdrv_write(bs, sector + written,
2490 buf_old + written * 512, pnum);
2491 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002492 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002493 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002494 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002495 }
2496 }
2497
2498 written += pnum;
2499 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002500 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002501 }
TeLeMand6771bf2010-02-08 16:20:00 +08002502
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002503 qemu_vfree(buf_old);
2504 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002505 }
2506
2507 /*
2508 * Change the backing file. All clusters that are different from the old
2509 * backing file are overwritten in the COW file now, so the visible content
2510 * doesn't change when we switch the backing file.
2511 */
Alex Bligha6166732012-10-16 13:46:18 +01002512 if (out_baseimg && *out_baseimg) {
2513 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2514 } else {
2515 ret = bdrv_change_backing_file(bs, NULL, NULL);
2516 }
2517
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002518 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002519 error_report("Could not change the backing file to '%s': No "
2520 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002521 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002522 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002523 out_baseimg, strerror(-ret));
2524 }
2525
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002526 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002527 /*
2528 * TODO At this point it is possible to check if any clusters that are
2529 * allocated in the COW file are the same in the backing file. If so, they
2530 * could be dropped from the COW file. Don't do this before switching the
2531 * backing file, in case of a crash this would lead to corruption.
2532 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002533out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002534 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002535 /* Cleanup */
2536 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002537 if (bs_old_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002538 bdrv_unref(bs_old_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002539 }
2540 if (bs_new_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002541 bdrv_unref(bs_new_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002542 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002543 }
2544
Fam Zheng4f6fd342013-08-23 09:14:47 +08002545 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002546 if (ret) {
2547 return 1;
2548 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002549 return 0;
2550}
2551
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002552static int img_resize(int argc, char **argv)
2553{
2554 int c, ret, relative;
2555 const char *filename, *fmt, *size;
2556 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002557 bool quiet = false;
Jes Sorensen2a819982010-12-06 17:08:31 +01002558 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002559 QemuOpts *param;
2560 static QemuOptsList resize_options = {
2561 .name = "resize_options",
2562 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2563 .desc = {
2564 {
2565 .name = BLOCK_OPT_SIZE,
2566 .type = QEMU_OPT_SIZE,
2567 .help = "Virtual disk size"
2568 }, {
2569 /* end of list */
2570 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002571 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002572 };
2573
Kevin Wolfe80fec72011-04-29 10:58:12 +02002574 /* Remove size from argv manually so that negative numbers are not treated
2575 * as options by getopt. */
2576 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002577 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02002578 return 1;
2579 }
2580
2581 size = argv[--argc];
2582
2583 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002584 fmt = NULL;
2585 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002586 c = getopt(argc, argv, "f:hq");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002587 if (c == -1) {
2588 break;
2589 }
2590 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002591 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002592 case 'h':
2593 help();
2594 break;
2595 case 'f':
2596 fmt = optarg;
2597 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002598 case 'q':
2599 quiet = true;
2600 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002601 }
2602 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002603 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002604 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002605 }
2606 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002607
2608 /* Choose grow, shrink, or absolute resize mode */
2609 switch (size[0]) {
2610 case '+':
2611 relative = 1;
2612 size++;
2613 break;
2614 case '-':
2615 relative = -1;
2616 size++;
2617 break;
2618 default:
2619 relative = 0;
2620 break;
2621 }
2622
2623 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002624 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002625 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002626 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002627 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002628 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002629 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002630 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002631 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2632 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002633
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002634 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
2635 true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002636 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002637 ret = -1;
2638 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002639 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002640
2641 if (relative) {
2642 total_size = bdrv_getlength(bs) + n * relative;
2643 } else {
2644 total_size = n;
2645 }
2646 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002647 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002648 ret = -1;
2649 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002650 }
2651
2652 ret = bdrv_truncate(bs, total_size);
2653 switch (ret) {
2654 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002655 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002656 break;
2657 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002658 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002659 break;
2660 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002661 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002662 break;
2663 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002664 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002665 break;
2666 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002667out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002668 if (bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002669 bdrv_unref(bs);
Jes Sorensen2a819982010-12-06 17:08:31 +01002670 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002671 if (ret) {
2672 return 1;
2673 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002674 return 0;
2675}
2676
Max Reitz6f176b42013-09-03 10:09:50 +02002677static int img_amend(int argc, char **argv)
2678{
2679 int c, ret = 0;
2680 char *options = NULL;
2681 QEMUOptionParameter *create_options = NULL, *options_param = NULL;
2682 const char *fmt = NULL, *filename;
2683 bool quiet = false;
2684 BlockDriverState *bs = NULL;
2685
2686 for (;;) {
2687 c = getopt(argc, argv, "hqf:o:");
2688 if (c == -1) {
2689 break;
2690 }
2691
2692 switch (c) {
2693 case 'h':
2694 case '?':
2695 help();
2696 break;
2697 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01002698 if (!is_valid_option_list(optarg)) {
2699 error_report("Invalid option list: %s", optarg);
2700 ret = -1;
2701 goto out;
2702 }
2703 if (!options) {
2704 options = g_strdup(optarg);
2705 } else {
2706 char *old_options = options;
2707 options = g_strdup_printf("%s,%s", options, optarg);
2708 g_free(old_options);
2709 }
Max Reitz6f176b42013-09-03 10:09:50 +02002710 break;
2711 case 'f':
2712 fmt = optarg;
2713 break;
2714 case 'q':
2715 quiet = true;
2716 break;
2717 }
2718 }
2719
Max Reitz6f176b42013-09-03 10:09:50 +02002720 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002721 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02002722 }
2723
Kevin Wolfa283cb62014-02-21 16:24:07 +01002724 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
2725 if (fmt && has_help_option(options)) {
2726 /* If a format is explicitly specified (and possibly no filename is
2727 * given), print option help here */
2728 ret = print_block_option_help(filename, fmt);
2729 goto out;
2730 }
2731
2732 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002733 error_exit("Expecting one image file name");
Kevin Wolfa283cb62014-02-21 16:24:07 +01002734 }
Max Reitz6f176b42013-09-03 10:09:50 +02002735
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002736 bs = bdrv_new_open("image", filename, fmt,
2737 BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
Max Reitz6f176b42013-09-03 10:09:50 +02002738 if (!bs) {
2739 error_report("Could not open image '%s'", filename);
2740 ret = -1;
2741 goto out;
2742 }
2743
2744 fmt = bs->drv->format_name;
2745
Kevin Wolf626f84f2014-02-21 16:24:06 +01002746 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01002747 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02002748 ret = print_block_option_help(filename, fmt);
2749 goto out;
2750 }
2751
2752 create_options = append_option_parameters(create_options,
2753 bs->drv->create_options);
2754 options_param = parse_option_parameters(options, create_options,
2755 options_param);
2756 if (options_param == NULL) {
2757 error_report("Invalid options for file format '%s'", fmt);
2758 ret = -1;
2759 goto out;
2760 }
2761
2762 ret = bdrv_amend_options(bs, options_param);
2763 if (ret < 0) {
2764 error_report("Error while amending options: %s", strerror(-ret));
2765 goto out;
2766 }
2767
2768out:
2769 if (bs) {
2770 bdrv_unref(bs);
2771 }
2772 free_option_parameters(create_options);
2773 free_option_parameters(options_param);
Kevin Wolf626f84f2014-02-21 16:24:06 +01002774 g_free(options);
2775
Max Reitz6f176b42013-09-03 10:09:50 +02002776 if (ret) {
2777 return 1;
2778 }
2779 return 0;
2780}
2781
Anthony Liguoric227f092009-10-01 16:12:16 -05002782static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002783#define DEF(option, callback, arg_string) \
2784 { option, callback },
2785#include "qemu-img-cmds.h"
2786#undef DEF
2787#undef GEN_DOCS
2788 { NULL, NULL, },
2789};
2790
bellardea2384d2004-08-01 21:59:26 +00002791int main(int argc, char **argv)
2792{
Anthony Liguoric227f092009-10-01 16:12:16 -05002793 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002794 const char *cmdname;
Jeff Cody7db16892014-04-25 17:02:32 -04002795 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04002796 static const struct option long_options[] = {
2797 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04002798 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04002799 {0, 0, 0, 0}
2800 };
bellardea2384d2004-08-01 21:59:26 +00002801
MORITA Kazutaka526eda12013-07-23 17:30:11 +09002802#ifdef CONFIG_POSIX
2803 signal(SIGPIPE, SIG_IGN);
2804#endif
2805
Kevin Wolf53f76e52010-12-16 15:10:32 +01002806 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08002807 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01002808
Paolo Bonzini2592c592012-11-03 18:10:17 +01002809 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002810 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08002811 if (argc < 2) {
2812 error_exit("Not enough arguments");
2813 }
Stuart Brady153859b2009-06-07 00:42:17 +01002814 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01002815
2816 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04002817 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01002818 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04002819 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01002820 }
bellardea2384d2004-08-01 21:59:26 +00002821 }
Stuart Brady153859b2009-06-07 00:42:17 +01002822
Jeff Cody5f6979c2014-04-28 14:37:18 -04002823 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04002824
2825 if (c == 'h') {
2826 help();
2827 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04002828 if (c == 'v') {
2829 printf(QEMU_IMG_VERSION);
2830 return 0;
2831 }
Jeff Cody7db16892014-04-25 17:02:32 -04002832
Stuart Brady153859b2009-06-07 00:42:17 +01002833 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08002834 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00002835}