]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-img.c
Use error_is_set() only when necessary (again)
[mirror_qemu.git] / qemu-img.c
CommitLineData
ea2384d3 1/*
fb43f4dd 2 * QEMU disk image utility
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
ea2384d3
FB
6 * 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 */
c054b3fd
BC
24#include "qapi-visit.h"
25#include "qapi/qmp-output-visitor.h"
7b1b5d19 26#include "qapi/qmp/qjson.h"
faf07963 27#include "qemu-common.h"
1de7afc9
PB
28#include "qemu/option.h"
29#include "qemu/error-report.h"
30#include "qemu/osdep.h"
9c17d615 31#include "sysemu/sysemu.h"
737e150e 32#include "block/block_int.h"
f364ec65 33#include "block/qapi.h"
c054b3fd 34#include <getopt.h>
e8445331 35
c227f099 36typedef struct img_cmd_t {
153859be
SB
37 const char *name;
38 int (*handler)(int argc, char **argv);
c227f099 39} img_cmd_t;
153859be 40
8599ea4c
FS
41enum {
42 OPTION_OUTPUT = 256,
43 OPTION_BACKING_CHAIN = 257,
44};
45
46typedef enum OutputFormat {
47 OFORMAT_JSON,
48 OFORMAT_HUMAN,
49} OutputFormat;
50
137519ce 51/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
adfe078e 52#define BDRV_O_FLAGS BDRV_O_CACHE_WB
661a0f71 53#define BDRV_DEFAULT_CACHE "writeback"
137519ce 54
ea2384d3
FB
55static void format_print(void *opaque, const char *name)
56{
57 printf(" %s", name);
58}
59
ac1307ab
FZ
60static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
61{
62 va_list ap;
63
64 error_printf("qemu-img: ");
65
66 va_start(ap, fmt);
67 error_vprintf(fmt, ap);
68 va_end(ap);
69
70 error_printf("\nTry 'qemu-img --help' for more information\n");
71 exit(EXIT_FAILURE);
72}
73
d2c639d6 74/* Please keep in synch with qemu-img.texi */
ac1307ab 75static void QEMU_NORETURN help(void)
ea2384d3 76{
e00291c0
PB
77 const char *help_msg =
78 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
3f020d70 79 "usage: qemu-img command [command options]\n"
80 "QEMU disk image utility\n"
81 "\n"
82 "Command syntax:\n"
153859be
SB
83#define DEF(option, callback, arg_string) \
84 " " arg_string "\n"
85#include "qemu-img-cmds.h"
86#undef DEF
87#undef GEN_DOCS
3f020d70 88 "\n"
89 "Command parameters:\n"
90 " 'filename' is a disk image filename\n"
91 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
661a0f71 92 " 'cache' is the cache mode used to write the output disk image, the valid\n"
80ccf93b
LY
93 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
94 " 'directsync' and 'unsafe' (default for convert)\n"
3f020d70 95 " 'size' is the disk image size in bytes. Optional suffixes\n"
5e00984a
KW
96 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
97 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
98 " supported. 'b' is ignored.\n"
3f020d70 99 " 'output_filename' is the destination disk image filename\n"
100 " 'output_fmt' is the destination format\n"
101 " 'options' is a comma separated list of format specific options in a\n"
102 " name=value format. Use -o ? for an overview of the options supported by the\n"
103 " used format\n"
ef80654d
WX
104 " 'snapshot_param' is param used for internal snapshot, format\n"
105 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
106 " '[ID_OR_NAME]'\n"
107 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
108 " instead\n"
3f020d70 109 " '-c' indicates that target image must be compressed (qcow format only)\n"
110 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
111 " match exactly. The image doesn't need a working backing file before\n"
112 " rebasing in this case (useful for renaming the backing file)\n"
113 " '-h' with or without a command shows this help and lists the supported formats\n"
6b837bc4 114 " '-p' show progress of command (only certain commands)\n"
f382d43a 115 " '-q' use Quiet mode - do not print any output (except errors)\n"
11b6699a
PL
116 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
117 " contain only zeros for qemu-img to create a sparse image during\n"
118 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
119 " unallocated or zero sectors, and the destination image will always be\n"
120 " fully allocated\n"
c054b3fd 121 " '--output' takes the format in which the output must be done (human or json)\n"
b2e10493
AD
122 " '-n' skips the target volume creation (useful if the volume is created\n"
123 " prior to running qemu-img)\n"
3f020d70 124 "\n"
4534ff54
KW
125 "Parameters to check subcommand:\n"
126 " '-r' tries to repair any inconsistencies that are found during the check.\n"
127 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
128 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
0546b8c2 129 " hiding corruption that has already occurred.\n"
4534ff54 130 "\n"
3f020d70 131 "Parameters to snapshot subcommand:\n"
132 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
133 " '-a' applies a snapshot (revert disk to saved state)\n"
134 " '-c' creates a snapshot\n"
135 " '-d' deletes a snapshot\n"
d14ed18c
MR
136 " '-l' lists all snapshots in the given image\n"
137 "\n"
138 "Parameters to compare subcommand:\n"
139 " '-f' first image format\n"
140 " '-F' second image format\n"
141 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
e00291c0
PB
142
143 printf("%s\nSupported formats:", help_msg);
ea2384d3
FB
144 bdrv_iterate_format(format_print, NULL);
145 printf("\n");
ac1307ab 146 exit(EXIT_SUCCESS);
ea2384d3
FB
147}
148
7c30f657 149static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
f382d43a
MR
150{
151 int ret = 0;
152 if (!quiet) {
153 va_list args;
154 va_start(args, fmt);
155 ret = vprintf(fmt, args);
156 va_end(args);
157 }
158 return ret;
159}
160
ea2384d3
FB
161#if defined(WIN32)
162/* XXX: put correct support for win32 */
163static int read_password(char *buf, int buf_size)
164{
165 int c, i;
166 printf("Password: ");
167 fflush(stdout);
168 i = 0;
169 for(;;) {
170 c = getchar();
171 if (c == '\n')
172 break;
173 if (i < (buf_size - 1))
174 buf[i++] = c;
175 }
176 buf[i] = '\0';
177 return 0;
178}
179
180#else
181
182#include <termios.h>
183
184static struct termios oldtty;
185
186static void term_exit(void)
187{
188 tcsetattr (0, TCSANOW, &oldtty);
189}
190
191static void term_init(void)
192{
193 struct termios tty;
194
195 tcgetattr (0, &tty);
196 oldtty = tty;
197
198 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
199 |INLCR|IGNCR|ICRNL|IXON);
200 tty.c_oflag |= OPOST;
201 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
202 tty.c_cflag &= ~(CSIZE|PARENB);
203 tty.c_cflag |= CS8;
204 tty.c_cc[VMIN] = 1;
205 tty.c_cc[VTIME] = 0;
3b46e624 206
ea2384d3
FB
207 tcsetattr (0, TCSANOW, &tty);
208
209 atexit(term_exit);
210}
211
3f379ab1 212static int read_password(char *buf, int buf_size)
ea2384d3
FB
213{
214 uint8_t ch;
215 int i, ret;
216
217 printf("password: ");
218 fflush(stdout);
219 term_init();
220 i = 0;
221 for(;;) {
222 ret = read(0, &ch, 1);
223 if (ret == -1) {
224 if (errno == EAGAIN || errno == EINTR) {
225 continue;
226 } else {
227 ret = -1;
228 break;
229 }
230 } else if (ret == 0) {
231 ret = -1;
232 break;
233 } else {
234 if (ch == '\r') {
235 ret = 0;
236 break;
237 }
238 if (i < (buf_size - 1))
239 buf[i++] = ch;
240 }
241 }
242 term_exit();
243 buf[i] = '\0';
244 printf("\n");
245 return ret;
246}
247#endif
248
4ac8aacd
JS
249static int print_block_option_help(const char *filename, const char *fmt)
250{
251 BlockDriver *drv, *proto_drv;
252 QEMUOptionParameter *create_options = NULL;
253
254 /* Find driver and parse its options */
255 drv = bdrv_find_format(fmt);
256 if (!drv) {
15654a6d 257 error_report("Unknown file format '%s'", fmt);
4ac8aacd
JS
258 return 1;
259 }
260
4ac8aacd
JS
261 create_options = append_option_parameters(create_options,
262 drv->create_options);
a283cb6e
KW
263
264 if (filename) {
265 proto_drv = bdrv_find_protocol(filename, true);
266 if (!proto_drv) {
267 error_report("Unknown protocol '%s'", filename);
268 return 1;
269 }
270 create_options = append_option_parameters(create_options,
271 proto_drv->create_options);
272 }
273
4ac8aacd
JS
274 print_option_help(create_options);
275 free_option_parameters(create_options);
276 return 0;
277}
278
9ffe3332
KW
279static BlockDriverState *bdrv_new_open(const char *id,
280 const char *filename,
9bc378c1 281 const char *fmt,
f0536bb8 282 int flags,
f382d43a
MR
283 bool require_io,
284 bool quiet)
75c23805
FB
285{
286 BlockDriverState *bs;
287 BlockDriver *drv;
288 char password[256];
34b5d2c6 289 Error *local_err = NULL;
b9eaf9ec 290 int ret;
75c23805 291
9ffe3332 292 bs = bdrv_new(id, &error_abort);
ad717139 293
75c23805
FB
294 if (fmt) {
295 drv = bdrv_find_format(fmt);
c2abccec 296 if (!drv) {
15654a6d 297 error_report("Unknown file format '%s'", fmt);
c2abccec
MK
298 goto fail;
299 }
75c23805
FB
300 } else {
301 drv = NULL;
302 }
b9eaf9ec 303
ddf5636d 304 ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
b9eaf9ec 305 if (ret < 0) {
34b5d2c6
HR
306 error_report("Could not open '%s': %s", filename,
307 error_get_pretty(local_err));
308 error_free(local_err);
c2abccec 309 goto fail;
75c23805 310 }
b9eaf9ec 311
f0536bb8 312 if (bdrv_is_encrypted(bs) && require_io) {
f382d43a 313 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
c2abccec 314 if (read_password(password, sizeof(password)) < 0) {
15654a6d 315 error_report("No password given");
c2abccec
MK
316 goto fail;
317 }
318 if (bdrv_set_key(bs, password) < 0) {
15654a6d 319 error_report("invalid password");
c2abccec
MK
320 goto fail;
321 }
75c23805
FB
322 }
323 return bs;
c2abccec 324fail:
f67503e5 325 bdrv_unref(bs);
c2abccec 326 return NULL;
75c23805
FB
327}
328
c2abccec 329static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
eec77d9e
JS
330 const char *base_filename,
331 const char *base_fmt)
efa84d43 332{
efa84d43
KW
333 if (base_filename) {
334 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
15654a6d
JS
335 error_report("Backing file not supported for file format '%s'",
336 fmt);
c2abccec 337 return -1;
efa84d43
KW
338 }
339 }
340 if (base_fmt) {
341 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
15654a6d
JS
342 error_report("Backing file format not supported for file "
343 "format '%s'", fmt);
c2abccec 344 return -1;
efa84d43
KW
345 }
346 }
c2abccec 347 return 0;
efa84d43
KW
348}
349
ea2384d3
FB
350static int img_create(int argc, char **argv)
351{
a9300911 352 int c;
1da7cfbd 353 uint64_t img_size = -1;
ea2384d3 354 const char *fmt = "raw";
9230eaf6 355 const char *base_fmt = NULL;
ea2384d3
FB
356 const char *filename;
357 const char *base_filename = NULL;
9ea2ea71 358 char *options = NULL;
9b37525a 359 Error *local_err = NULL;
f382d43a 360 bool quiet = false;
3b46e624 361
ea2384d3 362 for(;;) {
f382d43a 363 c = getopt(argc, argv, "F:b:f:he6o:q");
b8fb60da 364 if (c == -1) {
ea2384d3 365 break;
b8fb60da 366 }
ea2384d3 367 switch(c) {
ef87394c 368 case '?':
ea2384d3
FB
369 case 'h':
370 help();
371 break;
9230eaf6
AL
372 case 'F':
373 base_fmt = optarg;
374 break;
ea2384d3
FB
375 case 'b':
376 base_filename = optarg;
377 break;
378 case 'f':
379 fmt = optarg;
380 break;
381 case 'e':
9d42e15d 382 error_report("option -e is deprecated, please use \'-o "
eec77d9e 383 "encryption\' instead!");
77386bf6 384 goto fail;
d8871c5a 385 case '6':
9d42e15d 386 error_report("option -6 is deprecated, please use \'-o "
eec77d9e 387 "compat6\' instead!");
77386bf6 388 goto fail;
9ea2ea71 389 case 'o':
77386bf6
KW
390 if (!is_valid_option_list(optarg)) {
391 error_report("Invalid option list: %s", optarg);
392 goto fail;
393 }
394 if (!options) {
395 options = g_strdup(optarg);
396 } else {
397 char *old_options = options;
398 options = g_strdup_printf("%s,%s", options, optarg);
399 g_free(old_options);
400 }
9ea2ea71 401 break;
f382d43a
MR
402 case 'q':
403 quiet = true;
404 break;
ea2384d3
FB
405 }
406 }
9230eaf6 407
b50cbabc 408 /* Get the filename */
a283cb6e
KW
409 filename = (optind < argc) ? argv[optind] : NULL;
410 if (options && has_help_option(options)) {
411 g_free(options);
412 return print_block_option_help(filename, fmt);
413 }
414
b8fb60da 415 if (optind >= argc) {
ac1307ab 416 error_exit("Expecting image file name");
b8fb60da 417 }
a283cb6e 418 optind++;
b50cbabc 419
1da7cfbd
JS
420 /* Get image size, if specified */
421 if (optind < argc) {
70b4f4bb 422 int64_t sval;
e36b3695
MA
423 char *end;
424 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
425 if (sval < 0 || *end) {
79443397
LG
426 if (sval == -ERANGE) {
427 error_report("Image size must be less than 8 EiB!");
428 } else {
429 error_report("Invalid image size specified! You may use k, M, "
5e00984a
KW
430 "G, T, P or E suffixes for ");
431 error_report("kilobytes, megabytes, gigabytes, terabytes, "
432 "petabytes and exabytes.");
79443397 433 }
77386bf6 434 goto fail;
1da7cfbd
JS
435 }
436 img_size = (uint64_t)sval;
437 }
fc11eb26 438 if (optind != argc) {
ac1307ab 439 error_exit("Unexpected argument: %s", argv[optind]);
fc11eb26 440 }
1da7cfbd 441
9b37525a 442 bdrv_img_create(filename, fmt, base_filename, base_fmt,
f382d43a 443 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
84d18f06 444 if (local_err) {
b70d8c23 445 error_report("%s: %s", filename, error_get_pretty(local_err));
9b37525a 446 error_free(local_err);
77386bf6 447 goto fail;
c2abccec 448 }
a9300911 449
77386bf6 450 g_free(options);
ea2384d3 451 return 0;
77386bf6
KW
452
453fail:
454 g_free(options);
455 return 1;
ea2384d3
FB
456}
457
f382d43a 458static void dump_json_image_check(ImageCheck *check, bool quiet)
8599ea4c
FS
459{
460 Error *errp = NULL;
461 QString *str;
462 QmpOutputVisitor *ov = qmp_output_visitor_new();
463 QObject *obj;
464 visit_type_ImageCheck(qmp_output_get_visitor(ov),
465 &check, NULL, &errp);
466 obj = qmp_output_get_qobject(ov);
467 str = qobject_to_json_pretty(obj);
468 assert(str != NULL);
f382d43a 469 qprintf(quiet, "%s\n", qstring_get_str(str));
8599ea4c
FS
470 qobject_decref(obj);
471 qmp_output_visitor_cleanup(ov);
472 QDECREF(str);
473}
474
f382d43a 475static void dump_human_image_check(ImageCheck *check, bool quiet)
8599ea4c
FS
476{
477 if (!(check->corruptions || check->leaks || check->check_errors)) {
f382d43a 478 qprintf(quiet, "No errors were found on the image.\n");
8599ea4c
FS
479 } else {
480 if (check->corruptions) {
f382d43a
MR
481 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
482 "Data may be corrupted, or further writes to the image "
483 "may corrupt it.\n",
484 check->corruptions);
8599ea4c
FS
485 }
486
487 if (check->leaks) {
f382d43a
MR
488 qprintf(quiet,
489 "\n%" PRId64 " leaked clusters were found on the image.\n"
490 "This means waste of disk space, but no harm to data.\n",
491 check->leaks);
8599ea4c
FS
492 }
493
494 if (check->check_errors) {
f382d43a
MR
495 qprintf(quiet,
496 "\n%" PRId64
497 " internal errors have occurred during the check.\n",
498 check->check_errors);
8599ea4c
FS
499 }
500 }
501
502 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
f382d43a
MR
503 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
504 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
505 check->allocated_clusters, check->total_clusters,
506 check->allocated_clusters * 100.0 / check->total_clusters,
507 check->fragmented_clusters * 100.0 / check->allocated_clusters,
508 check->compressed_clusters * 100.0 /
509 check->allocated_clusters);
8599ea4c
FS
510 }
511
512 if (check->image_end_offset) {
f382d43a
MR
513 qprintf(quiet,
514 "Image end offset: %" PRId64 "\n", check->image_end_offset);
8599ea4c
FS
515 }
516}
517
518static int collect_image_check(BlockDriverState *bs,
519 ImageCheck *check,
520 const char *filename,
521 const char *fmt,
522 int fix)
523{
524 int ret;
525 BdrvCheckResult result;
526
527 ret = bdrv_check(bs, &result, fix);
528 if (ret < 0) {
529 return ret;
530 }
531
532 check->filename = g_strdup(filename);
533 check->format = g_strdup(bdrv_get_format_name(bs));
534 check->check_errors = result.check_errors;
535 check->corruptions = result.corruptions;
536 check->has_corruptions = result.corruptions != 0;
537 check->leaks = result.leaks;
538 check->has_leaks = result.leaks != 0;
539 check->corruptions_fixed = result.corruptions_fixed;
540 check->has_corruptions_fixed = result.corruptions != 0;
541 check->leaks_fixed = result.leaks_fixed;
542 check->has_leaks_fixed = result.leaks != 0;
543 check->image_end_offset = result.image_end_offset;
544 check->has_image_end_offset = result.image_end_offset != 0;
545 check->total_clusters = result.bfi.total_clusters;
546 check->has_total_clusters = result.bfi.total_clusters != 0;
547 check->allocated_clusters = result.bfi.allocated_clusters;
548 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
549 check->fragmented_clusters = result.bfi.fragmented_clusters;
550 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
e6439d78
SH
551 check->compressed_clusters = result.bfi.compressed_clusters;
552 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
8599ea4c
FS
553
554 return 0;
555}
556
e076f338
KW
557/*
558 * Checks an image for consistency. Exit codes:
559 *
560 * 0 - Check completed, image is good
561 * 1 - Check not completed because of internal errors
562 * 2 - Check completed, image is corrupted
563 * 3 - Check completed, image has leaked clusters, but is good otherwise
564 */
1585969c
AL
565static int img_check(int argc, char **argv)
566{
567 int c, ret;
8599ea4c
FS
568 OutputFormat output_format = OFORMAT_HUMAN;
569 const char *filename, *fmt, *output;
1585969c 570 BlockDriverState *bs;
4534ff54 571 int fix = 0;
058f8f16 572 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
8599ea4c 573 ImageCheck *check;
f382d43a 574 bool quiet = false;
1585969c
AL
575
576 fmt = NULL;
8599ea4c 577 output = NULL;
1585969c 578 for(;;) {
8599ea4c
FS
579 int option_index = 0;
580 static const struct option long_options[] = {
581 {"help", no_argument, 0, 'h'},
582 {"format", required_argument, 0, 'f'},
4fd6a984 583 {"repair", required_argument, 0, 'r'},
8599ea4c
FS
584 {"output", required_argument, 0, OPTION_OUTPUT},
585 {0, 0, 0, 0}
586 };
f382d43a 587 c = getopt_long(argc, argv, "f:hr:q",
8599ea4c 588 long_options, &option_index);
b8fb60da 589 if (c == -1) {
1585969c 590 break;
b8fb60da 591 }
1585969c 592 switch(c) {
ef87394c 593 case '?':
1585969c
AL
594 case 'h':
595 help();
596 break;
597 case 'f':
598 fmt = optarg;
599 break;
4534ff54
KW
600 case 'r':
601 flags |= BDRV_O_RDWR;
602
603 if (!strcmp(optarg, "leaks")) {
604 fix = BDRV_FIX_LEAKS;
605 } else if (!strcmp(optarg, "all")) {
606 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
607 } else {
ac1307ab
FZ
608 error_exit("Unknown option value for -r "
609 "(expecting 'leaks' or 'all'): %s", optarg);
4534ff54
KW
610 }
611 break;
8599ea4c
FS
612 case OPTION_OUTPUT:
613 output = optarg;
614 break;
f382d43a
MR
615 case 'q':
616 quiet = true;
617 break;
1585969c
AL
618 }
619 }
fc11eb26 620 if (optind != argc - 1) {
ac1307ab 621 error_exit("Expecting one image file name");
b8fb60da 622 }
1585969c
AL
623 filename = argv[optind++];
624
8599ea4c
FS
625 if (output && !strcmp(output, "json")) {
626 output_format = OFORMAT_JSON;
627 } else if (output && !strcmp(output, "human")) {
628 output_format = OFORMAT_HUMAN;
629 } else if (output) {
630 error_report("--output must be used with human or json as argument.");
631 return 1;
632 }
633
9ffe3332 634 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
c2abccec
MK
635 if (!bs) {
636 return 1;
637 }
8599ea4c
FS
638
639 check = g_new0(ImageCheck, 1);
640 ret = collect_image_check(bs, check, filename, fmt, fix);
e076f338
KW
641
642 if (ret == -ENOTSUP) {
8599ea4c
FS
643 if (output_format == OFORMAT_HUMAN) {
644 error_report("This image format does not support checks");
645 }
fefddf95 646 ret = 63;
8599ea4c 647 goto fail;
e076f338
KW
648 }
649
8599ea4c
FS
650 if (check->corruptions_fixed || check->leaks_fixed) {
651 int corruptions_fixed, leaks_fixed;
ccf34716 652
8599ea4c
FS
653 leaks_fixed = check->leaks_fixed;
654 corruptions_fixed = check->corruptions_fixed;
e076f338 655
8599ea4c 656 if (output_format == OFORMAT_HUMAN) {
f382d43a
MR
657 qprintf(quiet,
658 "The following inconsistencies were found and repaired:\n\n"
659 " %" PRId64 " leaked clusters\n"
660 " %" PRId64 " corruptions\n\n"
661 "Double checking the fixed image now...\n",
662 check->leaks_fixed,
663 check->corruptions_fixed);
e076f338
KW
664 }
665
8599ea4c 666 ret = collect_image_check(bs, check, filename, fmt, 0);
1585969c 667
8599ea4c
FS
668 check->leaks_fixed = leaks_fixed;
669 check->corruptions_fixed = corruptions_fixed;
f8111c24
DXW
670 }
671
8599ea4c
FS
672 switch (output_format) {
673 case OFORMAT_HUMAN:
f382d43a 674 dump_human_image_check(check, quiet);
8599ea4c
FS
675 break;
676 case OFORMAT_JSON:
f382d43a 677 dump_json_image_check(check, quiet);
8599ea4c 678 break;
c6bb9ad1
FS
679 }
680
8599ea4c
FS
681 if (ret || check->check_errors) {
682 ret = 1;
683 goto fail;
c2abccec 684 }
e076f338 685
8599ea4c
FS
686 if (check->corruptions) {
687 ret = 2;
688 } else if (check->leaks) {
689 ret = 3;
e076f338 690 } else {
8599ea4c 691 ret = 0;
e076f338 692 }
8599ea4c
FS
693
694fail:
695 qapi_free_ImageCheck(check);
4f6fd349 696 bdrv_unref(bs);
8599ea4c
FS
697
698 return ret;
1585969c
AL
699}
700
ea2384d3
FB
701static int img_commit(int argc, char **argv)
702{
661a0f71
FS
703 int c, ret, flags;
704 const char *filename, *fmt, *cache;
ea2384d3 705 BlockDriverState *bs;
f382d43a 706 bool quiet = false;
ea2384d3
FB
707
708 fmt = NULL;
661a0f71 709 cache = BDRV_DEFAULT_CACHE;
ea2384d3 710 for(;;) {
f382d43a 711 c = getopt(argc, argv, "f:ht:q");
b8fb60da 712 if (c == -1) {
ea2384d3 713 break;
b8fb60da 714 }
ea2384d3 715 switch(c) {
ef87394c 716 case '?':
ea2384d3
FB
717 case 'h':
718 help();
719 break;
720 case 'f':
721 fmt = optarg;
722 break;
661a0f71
FS
723 case 't':
724 cache = optarg;
725 break;
f382d43a
MR
726 case 'q':
727 quiet = true;
728 break;
ea2384d3
FB
729 }
730 }
fc11eb26 731 if (optind != argc - 1) {
ac1307ab 732 error_exit("Expecting one image file name");
b8fb60da 733 }
ea2384d3
FB
734 filename = argv[optind++];
735
661a0f71 736 flags = BDRV_O_RDWR;
c3993cdc 737 ret = bdrv_parse_cache_flags(cache, &flags);
661a0f71
FS
738 if (ret < 0) {
739 error_report("Invalid cache option: %s", cache);
740 return -1;
741 }
742
9ffe3332 743 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
c2abccec
MK
744 if (!bs) {
745 return 1;
746 }
ea2384d3
FB
747 ret = bdrv_commit(bs);
748 switch(ret) {
749 case 0:
f382d43a 750 qprintf(quiet, "Image committed.\n");
ea2384d3
FB
751 break;
752 case -ENOENT:
15654a6d 753 error_report("No disk inserted");
ea2384d3
FB
754 break;
755 case -EACCES:
15654a6d 756 error_report("Image is read-only");
ea2384d3
FB
757 break;
758 case -ENOTSUP:
15654a6d 759 error_report("Image is already committed");
ea2384d3
FB
760 break;
761 default:
15654a6d 762 error_report("Error while committing image");
ea2384d3
FB
763 break;
764 }
765
4f6fd349 766 bdrv_unref(bs);
c2abccec
MK
767 if (ret) {
768 return 1;
769 }
ea2384d3
FB
770 return 0;
771}
772
f58c7b35
TS
773/*
774 * Returns true iff the first sector pointed to by 'buf' contains at least
775 * a non-NUL byte.
776 *
777 * 'pnum' is set to the number of sectors (including and immediately following
778 * the first one) that are known to be in the same allocated/unallocated state.
779 */
ea2384d3
FB
780static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
781{
1a6d39fd
SH
782 bool is_zero;
783 int i;
ea2384d3
FB
784
785 if (n <= 0) {
786 *pnum = 0;
787 return 0;
788 }
1a6d39fd 789 is_zero = buffer_is_zero(buf, 512);
ea2384d3
FB
790 for(i = 1; i < n; i++) {
791 buf += 512;
1a6d39fd 792 if (is_zero != buffer_is_zero(buf, 512)) {
ea2384d3 793 break;
1a6d39fd 794 }
ea2384d3
FB
795 }
796 *pnum = i;
1a6d39fd 797 return !is_zero;
ea2384d3
FB
798}
799
a22f123c
KW
800/*
801 * Like is_allocated_sectors, but if the buffer starts with a used sector,
802 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
803 * breaking up write requests for only small sparse areas.
804 */
805static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
806 int min)
807{
808 int ret;
809 int num_checked, num_used;
810
811 if (n < min) {
812 min = n;
813 }
814
815 ret = is_allocated_sectors(buf, n, pnum);
816 if (!ret) {
817 return ret;
818 }
819
820 num_used = *pnum;
821 buf += BDRV_SECTOR_SIZE * *pnum;
822 n -= *pnum;
823 num_checked = num_used;
824
825 while (n > 0) {
826 ret = is_allocated_sectors(buf, n, pnum);
827
828 buf += BDRV_SECTOR_SIZE * *pnum;
829 n -= *pnum;
830 num_checked += *pnum;
831 if (ret) {
832 num_used = num_checked;
833 } else if (*pnum >= min) {
834 break;
835 }
836 }
837
838 *pnum = num_used;
839 return 1;
840}
841
3e85c6fd
KW
842/*
843 * Compares two buffers sector by sector. Returns 0 if the first sector of both
844 * buffers matches, non-zero otherwise.
845 *
846 * pnum is set to the number of sectors (including and immediately following
847 * the first one) that are known to have the same comparison result
848 */
849static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
850 int *pnum)
851{
852 int res, i;
853
854 if (n <= 0) {
855 *pnum = 0;
856 return 0;
857 }
858
859 res = !!memcmp(buf1, buf2, 512);
860 for(i = 1; i < n; i++) {
861 buf1 += 512;
862 buf2 += 512;
863
864 if (!!memcmp(buf1, buf2, 512) != res) {
865 break;
866 }
867 }
868
869 *pnum = i;
870 return res;
871}
872
80ee15a6 873#define IO_BUF_SIZE (2 * 1024 * 1024)
ea2384d3 874
d14ed18c
MR
875static int64_t sectors_to_bytes(int64_t sectors)
876{
877 return sectors << BDRV_SECTOR_BITS;
878}
879
880static int64_t sectors_to_process(int64_t total, int64_t from)
881{
882 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
883}
884
885/*
886 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
887 *
888 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
889 * data and negative value on error.
890 *
891 * @param bs: Driver used for accessing file
892 * @param sect_num: Number of first sector to check
893 * @param sect_count: Number of sectors to check
894 * @param filename: Name of disk file we are checking (logging purpose)
895 * @param buffer: Allocated buffer for storing read data
896 * @param quiet: Flag for quiet mode
897 */
898static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
899 int sect_count, const char *filename,
900 uint8_t *buffer, bool quiet)
901{
902 int pnum, ret = 0;
903 ret = bdrv_read(bs, sect_num, buffer, sect_count);
904 if (ret < 0) {
905 error_report("Error while reading offset %" PRId64 " of %s: %s",
906 sectors_to_bytes(sect_num), filename, strerror(-ret));
907 return ret;
908 }
909 ret = is_allocated_sectors(buffer, sect_count, &pnum);
910 if (ret || pnum != sect_count) {
911 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
912 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
913 return 1;
914 }
915
916 return 0;
917}
918
919/*
920 * Compares two images. Exit codes:
921 *
922 * 0 - Images are identical
923 * 1 - Images differ
924 * >1 - Error occurred
925 */
926static int img_compare(int argc, char **argv)
927{
928 const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
929 BlockDriverState *bs1, *bs2;
930 int64_t total_sectors1, total_sectors2;
931 uint8_t *buf1 = NULL, *buf2 = NULL;
932 int pnum1, pnum2;
933 int allocated1, allocated2;
934 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
935 bool progress = false, quiet = false, strict = false;
936 int64_t total_sectors;
937 int64_t sector_num = 0;
938 int64_t nb_sectors;
939 int c, pnum;
940 uint64_t bs_sectors;
941 uint64_t progress_base;
942
943 for (;;) {
944 c = getopt(argc, argv, "hpf:F:sq");
945 if (c == -1) {
946 break;
947 }
948 switch (c) {
949 case '?':
950 case 'h':
951 help();
952 break;
953 case 'f':
954 fmt1 = optarg;
955 break;
956 case 'F':
957 fmt2 = optarg;
958 break;
959 case 'p':
960 progress = true;
961 break;
962 case 'q':
963 quiet = true;
964 break;
965 case 's':
966 strict = true;
967 break;
968 }
969 }
970
971 /* Progress is not shown in Quiet mode */
972 if (quiet) {
973 progress = false;
974 }
975
976
fc11eb26 977 if (optind != argc - 2) {
ac1307ab 978 error_exit("Expecting two image file names");
d14ed18c
MR
979 }
980 filename1 = argv[optind++];
981 filename2 = argv[optind++];
982
983 /* Initialize before goto out */
984 qemu_progress_init(progress, 2.0);
985
9ffe3332 986 bs1 = bdrv_new_open("image 1", filename1, fmt1, BDRV_O_FLAGS, true, quiet);
d14ed18c
MR
987 if (!bs1) {
988 error_report("Can't open file %s", filename1);
989 ret = 2;
990 goto out3;
991 }
992
9ffe3332 993 bs2 = bdrv_new_open("image 2", filename2, fmt2, BDRV_O_FLAGS, true, quiet);
d14ed18c
MR
994 if (!bs2) {
995 error_report("Can't open file %s", filename2);
996 ret = 2;
997 goto out2;
998 }
999
1000 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
1001 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
1002 bdrv_get_geometry(bs1, &bs_sectors);
1003 total_sectors1 = bs_sectors;
1004 bdrv_get_geometry(bs2, &bs_sectors);
1005 total_sectors2 = bs_sectors;
1006 total_sectors = MIN(total_sectors1, total_sectors2);
1007 progress_base = MAX(total_sectors1, total_sectors2);
1008
1009 qemu_progress_print(0, 100);
1010
1011 if (strict && total_sectors1 != total_sectors2) {
1012 ret = 1;
1013 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1014 goto out;
1015 }
1016
1017 for (;;) {
1018 nb_sectors = sectors_to_process(total_sectors, sector_num);
1019 if (nb_sectors <= 0) {
1020 break;
1021 }
1022 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
1023 &pnum1);
1024 if (allocated1 < 0) {
1025 ret = 3;
1026 error_report("Sector allocation test failed for %s", filename1);
1027 goto out;
1028 }
1029
1030 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
1031 &pnum2);
1032 if (allocated2 < 0) {
1033 ret = 3;
1034 error_report("Sector allocation test failed for %s", filename2);
1035 goto out;
1036 }
1037 nb_sectors = MIN(pnum1, pnum2);
1038
1039 if (allocated1 == allocated2) {
1040 if (allocated1) {
1041 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
1042 if (ret < 0) {
1043 error_report("Error while reading offset %" PRId64 " of %s:"
1044 " %s", sectors_to_bytes(sector_num), filename1,
1045 strerror(-ret));
1046 ret = 4;
1047 goto out;
1048 }
1049 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1050 if (ret < 0) {
1051 error_report("Error while reading offset %" PRId64
1052 " of %s: %s", sectors_to_bytes(sector_num),
1053 filename2, strerror(-ret));
1054 ret = 4;
1055 goto out;
1056 }
1057 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1058 if (ret || pnum != nb_sectors) {
d14ed18c
MR
1059 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1060 sectors_to_bytes(
1061 ret ? sector_num : sector_num + pnum));
36452f12 1062 ret = 1;
d14ed18c
MR
1063 goto out;
1064 }
1065 }
1066 } else {
1067 if (strict) {
1068 ret = 1;
1069 qprintf(quiet, "Strict mode: Offset %" PRId64
1070 " allocation mismatch!\n",
1071 sectors_to_bytes(sector_num));
1072 goto out;
1073 }
1074
1075 if (allocated1) {
1076 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1077 filename1, buf1, quiet);
1078 } else {
1079 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1080 filename2, buf1, quiet);
1081 }
1082 if (ret) {
1083 if (ret < 0) {
d14ed18c
MR
1084 error_report("Error while reading offset %" PRId64 ": %s",
1085 sectors_to_bytes(sector_num), strerror(-ret));
36452f12 1086 ret = 4;
d14ed18c
MR
1087 }
1088 goto out;
1089 }
1090 }
1091 sector_num += nb_sectors;
1092 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1093 }
1094
1095 if (total_sectors1 != total_sectors2) {
1096 BlockDriverState *bs_over;
1097 int64_t total_sectors_over;
1098 const char *filename_over;
1099
1100 qprintf(quiet, "Warning: Image size mismatch!\n");
1101 if (total_sectors1 > total_sectors2) {
1102 total_sectors_over = total_sectors1;
1103 bs_over = bs1;
1104 filename_over = filename1;
1105 } else {
1106 total_sectors_over = total_sectors2;
1107 bs_over = bs2;
1108 filename_over = filename2;
1109 }
1110
1111 for (;;) {
1112 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1113 if (nb_sectors <= 0) {
1114 break;
1115 }
1116 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1117 nb_sectors, &pnum);
1118 if (ret < 0) {
1119 ret = 3;
1120 error_report("Sector allocation test failed for %s",
1121 filename_over);
1122 goto out;
1123
1124 }
1125 nb_sectors = pnum;
1126 if (ret) {
1127 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1128 filename_over, buf1, quiet);
1129 if (ret) {
1130 if (ret < 0) {
d14ed18c
MR
1131 error_report("Error while reading offset %" PRId64
1132 " of %s: %s", sectors_to_bytes(sector_num),
1133 filename_over, strerror(-ret));
36452f12 1134 ret = 4;
d14ed18c
MR
1135 }
1136 goto out;
1137 }
1138 }
1139 sector_num += nb_sectors;
1140 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1141 }
1142 }
1143
1144 qprintf(quiet, "Images are identical.\n");
1145 ret = 0;
1146
1147out:
4f6fd349 1148 bdrv_unref(bs2);
d14ed18c
MR
1149 qemu_vfree(buf1);
1150 qemu_vfree(buf2);
1151out2:
4f6fd349 1152 bdrv_unref(bs1);
d14ed18c
MR
1153out3:
1154 qemu_progress_end();
1155 return ret;
1156}
1157
ea2384d3
FB
1158static int img_convert(int argc, char **argv)
1159{
24f833cd 1160 int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
13c28af8 1161 int64_t ret = 0;
661a0f71
FS
1162 int progress = 0, flags;
1163 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
b50cbabc 1164 BlockDriver *drv, *proto_drv;
c2abccec 1165 BlockDriverState **bs = NULL, *out_bs = NULL;
802c3d4c 1166 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
96b8f136 1167 uint64_t bs_sectors;
c2abccec 1168 uint8_t * buf = NULL;
f2521c90 1169 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
ea2384d3 1170 const uint8_t *buf1;
faea38e7 1171 BlockDriverInfo bdi;
b50cbabc 1172 QEMUOptionParameter *param = NULL, *create_options = NULL;
a18953fb 1173 QEMUOptionParameter *out_baseimg_param;
efa84d43 1174 char *options = NULL;
51ef6727 1175 const char *snapshot_name = NULL;
a22f123c 1176 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
f382d43a 1177 bool quiet = false;
cc84d90f 1178 Error *local_err = NULL;
ef80654d 1179 QemuOpts *sn_opts = NULL;
ea2384d3
FB
1180
1181 fmt = NULL;
1182 out_fmt = "raw";
661a0f71 1183 cache = "unsafe";
f58c7b35 1184 out_baseimg = NULL;
eec77d9e 1185 compress = 0;
b2e10493 1186 skip_create = 0;
ea2384d3 1187 for(;;) {
ef80654d 1188 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:");
b8fb60da 1189 if (c == -1) {
ea2384d3 1190 break;
b8fb60da 1191 }
ea2384d3 1192 switch(c) {
ef87394c 1193 case '?':
ea2384d3
FB
1194 case 'h':
1195 help();
1196 break;
1197 case 'f':
1198 fmt = optarg;
1199 break;
1200 case 'O':
1201 out_fmt = optarg;
1202 break;
f58c7b35
TS
1203 case 'B':
1204 out_baseimg = optarg;
1205 break;
ea2384d3 1206 case 'c':
eec77d9e 1207 compress = 1;
ea2384d3
FB
1208 break;
1209 case 'e':
9d42e15d 1210 error_report("option -e is deprecated, please use \'-o "
eec77d9e 1211 "encryption\' instead!");
2dc8328b 1212 ret = -1;
64bb01aa 1213 goto fail_getopt;
ec36ba14 1214 case '6':
9d42e15d 1215 error_report("option -6 is deprecated, please use \'-o "
eec77d9e 1216 "compat6\' instead!");
2dc8328b 1217 ret = -1;
64bb01aa 1218 goto fail_getopt;
efa84d43 1219 case 'o':
2dc8328b
KW
1220 if (!is_valid_option_list(optarg)) {
1221 error_report("Invalid option list: %s", optarg);
1222 ret = -1;
64bb01aa 1223 goto fail_getopt;
2dc8328b
KW
1224 }
1225 if (!options) {
1226 options = g_strdup(optarg);
1227 } else {
1228 char *old_options = options;
1229 options = g_strdup_printf("%s,%s", options, optarg);
1230 g_free(old_options);
1231 }
efa84d43 1232 break;
51ef6727 1233 case 's':
1234 snapshot_name = optarg;
1235 break;
ef80654d
WX
1236 case 'l':
1237 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1238 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
1239 if (!sn_opts) {
1240 error_report("Failed in parsing snapshot param '%s'",
1241 optarg);
2dc8328b 1242 ret = -1;
64bb01aa 1243 goto fail_getopt;
ef80654d
WX
1244 }
1245 } else {
1246 snapshot_name = optarg;
1247 }
1248 break;
a22f123c
KW
1249 case 'S':
1250 {
1251 int64_t sval;
e36b3695
MA
1252 char *end;
1253 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1254 if (sval < 0 || *end) {
a22f123c 1255 error_report("Invalid minimum zero buffer size for sparse output specified");
2dc8328b 1256 ret = -1;
64bb01aa 1257 goto fail_getopt;
a22f123c
KW
1258 }
1259
1260 min_sparse = sval / BDRV_SECTOR_SIZE;
1261 break;
1262 }
6b837bc4
JS
1263 case 'p':
1264 progress = 1;
1265 break;
661a0f71
FS
1266 case 't':
1267 cache = optarg;
1268 break;
f382d43a
MR
1269 case 'q':
1270 quiet = true;
1271 break;
b2e10493
AD
1272 case 'n':
1273 skip_create = 1;
1274 break;
ea2384d3
FB
1275 }
1276 }
3b46e624 1277
64bb01aa 1278 /* Initialize before goto out */
f382d43a
MR
1279 if (quiet) {
1280 progress = 0;
1281 }
64bb01aa
KW
1282 qemu_progress_init(progress, 1.0);
1283
f382d43a 1284
926c2d23 1285 bs_n = argc - optind - 1;
a283cb6e 1286 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
f58c7b35 1287
2dc8328b 1288 if (options && has_help_option(options)) {
4ac8aacd
JS
1289 ret = print_block_option_help(out_filename, out_fmt);
1290 goto out;
1291 }
1292
a283cb6e 1293 if (bs_n < 1) {
ac1307ab 1294 error_exit("Must specify image file name");
a283cb6e
KW
1295 }
1296
1297
c2abccec 1298 if (bs_n > 1 && out_baseimg) {
15654a6d
JS
1299 error_report("-B makes no sense when concatenating multiple input "
1300 "images");
31ca34b8
JS
1301 ret = -1;
1302 goto out;
c2abccec 1303 }
f8111c24 1304
6b837bc4
JS
1305 qemu_progress_print(0, 100);
1306
7267c094 1307 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
926c2d23
AZ
1308
1309 total_sectors = 0;
1310 for (bs_i = 0; bs_i < bs_n; bs_i++) {
9ffe3332
KW
1311 char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
1312 : g_strdup("source");
1313 bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, BDRV_O_FLAGS,
1314 true, quiet);
1315 g_free(id);
c2abccec 1316 if (!bs[bs_i]) {
15654a6d 1317 error_report("Could not open '%s'", argv[optind + bs_i]);
c2abccec
MK
1318 ret = -1;
1319 goto out;
1320 }
926c2d23
AZ
1321 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1322 total_sectors += bs_sectors;
1323 }
ea2384d3 1324
ef80654d
WX
1325 if (sn_opts) {
1326 ret = bdrv_snapshot_load_tmp(bs[0],
1327 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1328 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1329 &local_err);
1330 } else if (snapshot_name != NULL) {
51ef6727 1331 if (bs_n > 1) {
6daf194d 1332 error_report("No support for concatenating multiple snapshot");
51ef6727 1333 ret = -1;
1334 goto out;
1335 }
7b4c4781
WX
1336
1337 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
ef80654d 1338 }
84d18f06 1339 if (local_err) {
ef80654d
WX
1340 error_report("Failed to load snapshot: %s",
1341 error_get_pretty(local_err));
1342 error_free(local_err);
1343 ret = -1;
1344 goto out;
51ef6727 1345 }
1346
efa84d43 1347 /* Find driver and parse its options */
ea2384d3 1348 drv = bdrv_find_format(out_fmt);
c2abccec 1349 if (!drv) {
15654a6d 1350 error_report("Unknown file format '%s'", out_fmt);
c2abccec
MK
1351 ret = -1;
1352 goto out;
1353 }
efa84d43 1354
98289620 1355 proto_drv = bdrv_find_protocol(out_filename, true);
c2abccec 1356 if (!proto_drv) {
15654a6d 1357 error_report("Unknown protocol '%s'", out_filename);
c2abccec
MK
1358 ret = -1;
1359 goto out;
1360 }
b50cbabc
MK
1361
1362 create_options = append_option_parameters(create_options,
1363 drv->create_options);
1364 create_options = append_option_parameters(create_options,
1365 proto_drv->create_options);
db08adf5 1366
efa84d43 1367 if (options) {
b50cbabc 1368 param = parse_option_parameters(options, create_options, param);
efa84d43 1369 if (param == NULL) {
15654a6d 1370 error_report("Invalid options for file format '%s'.", out_fmt);
c2abccec
MK
1371 ret = -1;
1372 goto out;
efa84d43
KW
1373 }
1374 } else {
b50cbabc 1375 param = parse_option_parameters("", create_options, param);
efa84d43
KW
1376 }
1377
1378 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
eec77d9e 1379 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
c2abccec
MK
1380 if (ret < 0) {
1381 goto out;
1382 }
efa84d43 1383
a18953fb
KW
1384 /* Get backing file name if -o backing_file was used */
1385 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1386 if (out_baseimg_param) {
1387 out_baseimg = out_baseimg_param->value.s;
1388 }
1389
efa84d43 1390 /* Check if compression is supported */
eec77d9e 1391 if (compress) {
efa84d43
KW
1392 QEMUOptionParameter *encryption =
1393 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
41521fa4
KW
1394 QEMUOptionParameter *preallocation =
1395 get_option_parameter(param, BLOCK_OPT_PREALLOC);
efa84d43
KW
1396
1397 if (!drv->bdrv_write_compressed) {
15654a6d 1398 error_report("Compression not supported for this file format");
c2abccec
MK
1399 ret = -1;
1400 goto out;
efa84d43
KW
1401 }
1402
1403 if (encryption && encryption->value.n) {
15654a6d
JS
1404 error_report("Compression and encryption not supported at "
1405 "the same time");
c2abccec
MK
1406 ret = -1;
1407 goto out;
efa84d43 1408 }
41521fa4
KW
1409
1410 if (preallocation && preallocation->value.s
1411 && strcmp(preallocation->value.s, "off"))
1412 {
1413 error_report("Compression and preallocation not supported at "
1414 "the same time");
1415 ret = -1;
1416 goto out;
1417 }
efa84d43
KW
1418 }
1419
b2e10493
AD
1420 if (!skip_create) {
1421 /* Create the new image */
cc84d90f 1422 ret = bdrv_create(drv, out_filename, param, &local_err);
b2e10493 1423 if (ret < 0) {
cc84d90f
HR
1424 error_report("%s: error while converting %s: %s",
1425 out_filename, out_fmt, error_get_pretty(local_err));
1426 error_free(local_err);
b2e10493 1427 goto out;
ea2384d3
FB
1428 }
1429 }
3b46e624 1430
5a37b60a 1431 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
c3993cdc 1432 ret = bdrv_parse_cache_flags(cache, &flags);
661a0f71
FS
1433 if (ret < 0) {
1434 error_report("Invalid cache option: %s", cache);
1435 return -1;
1436 }
1437
9ffe3332 1438 out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
c2abccec
MK
1439 if (!out_bs) {
1440 ret = -1;
1441 goto out;
1442 }
ea2384d3 1443
926c2d23
AZ
1444 bs_i = 0;
1445 bs_offset = 0;
1446 bdrv_get_geometry(bs[0], &bs_sectors);
f2521c90
PL
1447
1448 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1449 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1450 * as maximum. */
1451 bufsectors = MIN(32768,
1452 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1453 out_bs->bl.discard_alignment))
1454 );
1455
1456 buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
926c2d23 1457
b2e10493
AD
1458 if (skip_create) {
1459 int64_t output_length = bdrv_getlength(out_bs);
1460 if (output_length < 0) {
1461 error_report("unable to get output image length: %s\n",
1462 strerror(-output_length));
1463 ret = -1;
1464 goto out;
1465 } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
1466 error_report("output file is smaller than input file");
1467 ret = -1;
1468 goto out;
1469 }
1470 }
1471
24f833cd
PL
1472 cluster_sectors = 0;
1473 ret = bdrv_get_info(out_bs, &bdi);
1474 if (ret < 0) {
1475 if (compress) {
15654a6d 1476 error_report("could not get block driver info");
c2abccec
MK
1477 goto out;
1478 }
24f833cd
PL
1479 } else {
1480 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1481 }
1482
1483 if (compress) {
1484 if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
15654a6d 1485 error_report("invalid cluster size");
c2abccec
MK
1486 ret = -1;
1487 goto out;
1488 }
ea2384d3 1489 sector_num = 0;
6b837bc4
JS
1490
1491 nb_sectors = total_sectors;
6b837bc4 1492
ea2384d3 1493 for(;;) {
926c2d23
AZ
1494 int64_t bs_num;
1495 int remainder;
1496 uint8_t *buf2;
1497
ea2384d3
FB
1498 nb_sectors = total_sectors - sector_num;
1499 if (nb_sectors <= 0)
1500 break;
1501 if (nb_sectors >= cluster_sectors)
1502 n = cluster_sectors;
1503 else
1504 n = nb_sectors;
926c2d23
AZ
1505
1506 bs_num = sector_num - bs_offset;
1507 assert (bs_num >= 0);
1508 remainder = n;
1509 buf2 = buf;
1510 while (remainder > 0) {
1511 int nlow;
1512 while (bs_num == bs_sectors) {
1513 bs_i++;
1514 assert (bs_i < bs_n);
1515 bs_offset += bs_sectors;
1516 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1517 bs_num = 0;
0bfcd599
BS
1518 /* printf("changing part: sector_num=%" PRId64 ", "
1519 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1520 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
926c2d23
AZ
1521 }
1522 assert (bs_num < bs_sectors);
1523
1524 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1525
c2abccec
MK
1526 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1527 if (ret < 0) {
3fba9d81
SH
1528 error_report("error while reading sector %" PRId64 ": %s",
1529 bs_num, strerror(-ret));
c2abccec
MK
1530 goto out;
1531 }
926c2d23
AZ
1532
1533 buf2 += nlow * 512;
1534 bs_num += nlow;
1535
1536 remainder -= nlow;
1537 }
1538 assert (remainder == 0);
1539
54f106d5
SH
1540 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1541 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
c2abccec 1542 if (ret != 0) {
3fba9d81
SH
1543 error_report("error while compressing sector %" PRId64
1544 ": %s", sector_num, strerror(-ret));
c2abccec
MK
1545 goto out;
1546 }
ea2384d3
FB
1547 }
1548 sector_num += n;
13c28af8 1549 qemu_progress_print(100.0 * sector_num / total_sectors, 0);
ea2384d3 1550 }
faea38e7
FB
1551 /* signal EOF to align */
1552 bdrv_write_compressed(out_bs, 0, NULL, 0);
ea2384d3 1553 } else {
802c3d4c
PL
1554 int64_t sectors_to_read, sectors_read, sector_num_next_status;
1555 bool count_allocated_sectors;
11b6699a 1556 int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0;
f2feebbd 1557
5a37b60a
PL
1558 if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
1559 ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
1560 if (ret < 0) {
1561 goto out;
1562 }
1563 has_zero_init = 1;
1564 }
1565
802c3d4c
PL
1566 sectors_to_read = total_sectors;
1567 count_allocated_sectors = progress && (out_baseimg || has_zero_init);
1568restart:
f58c7b35 1569 sector_num = 0; // total number of sectors converted so far
802c3d4c
PL
1570 sectors_read = 0;
1571 sector_num_next_status = 0;
6b837bc4 1572
ea2384d3
FB
1573 for(;;) {
1574 nb_sectors = total_sectors - sector_num;
b8fb60da 1575 if (nb_sectors <= 0) {
802c3d4c
PL
1576 if (count_allocated_sectors) {
1577 sectors_to_read = sectors_read;
1578 count_allocated_sectors = false;
1579 goto restart;
1580 }
13c28af8 1581 ret = 0;
ea2384d3 1582 break;
b8fb60da 1583 }
926c2d23
AZ
1584
1585 while (sector_num - bs_offset >= bs_sectors) {
1586 bs_i ++;
1587 assert (bs_i < bs_n);
1588 bs_offset += bs_sectors;
1589 bdrv_get_geometry(bs[bs_i], &bs_sectors);
0bfcd599
BS
1590 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1591 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
926c2d23
AZ
1592 sector_num, bs_i, bs_offset, bs_sectors); */
1593 }
1594
13c28af8
PL
1595 if ((out_baseimg || has_zero_init) &&
1596 sector_num >= sector_num_next_status) {
1597 n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors;
1598 ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset,
1599 n, &n1);
e4a86f88 1600 if (ret < 0) {
13c28af8
PL
1601 error_report("error while reading block status of sector %"
1602 PRId64 ": %s", sector_num - bs_offset,
1603 strerror(-ret));
e4a86f88
PB
1604 goto out;
1605 }
13c28af8
PL
1606 /* If the output image is zero initialized, we are not working
1607 * on a shared base and the input is zero we can skip the next
1608 * n1 sectors */
1609 if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) {
e4a86f88
PB
1610 sector_num += n1;
1611 continue;
93c65b47 1612 }
13c28af8
PL
1613 /* If the output image is being created as a copy on write
1614 * image, assume that sectors which are unallocated in the
1615 * input image are present in both the output's and input's
1616 * base images (no need to copy them). */
1617 if (out_baseimg) {
1618 if (!(ret & BDRV_BLOCK_DATA)) {
1619 sector_num += n1;
1620 continue;
1621 }
1622 /* The next 'n1' sectors are allocated in the input image.
1623 * Copy only those as they may be followed by unallocated
1624 * sectors. */
1625 nb_sectors = n1;
1626 }
1627 /* avoid redundant callouts to get_block_status */
1628 sector_num_next_status = sector_num + n1;
f58c7b35
TS
1629 }
1630
f2521c90 1631 n = MIN(nb_sectors, bufsectors);
24f833cd
PL
1632
1633 /* round down request length to an aligned sector, but
1634 * do not bother doing this on short requests. They happen
1635 * when we found an all-zero area, and the next sector to
1636 * write will not be sector_num + n. */
1637 if (cluster_sectors > 0 && n >= cluster_sectors) {
1638 int64_t next_aligned_sector = (sector_num + n);
1639 next_aligned_sector -= next_aligned_sector % cluster_sectors;
1640 if (sector_num + n > next_aligned_sector) {
1641 n = next_aligned_sector - sector_num;
1642 }
1643 }
1644
13c28af8 1645 n = MIN(n, bs_sectors - (sector_num - bs_offset));
13c28af8 1646
802c3d4c
PL
1647 sectors_read += n;
1648 if (count_allocated_sectors) {
1649 sector_num += n;
1650 continue;
1651 }
1652
1653 n1 = n;
c2abccec
MK
1654 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1655 if (ret < 0) {
3fba9d81
SH
1656 error_report("error while reading sector %" PRId64 ": %s",
1657 sector_num - bs_offset, strerror(-ret));
c2abccec
MK
1658 goto out;
1659 }
ea2384d3
FB
1660 /* NOTE: at the same time we convert, we do not write zero
1661 sectors to have a chance to compress the image. Ideally, we
1662 should add a specific call to have the info to go faster */
1663 buf1 = buf;
1664 while (n > 0) {
11212d8f 1665 if (!has_zero_init ||
a22f123c 1666 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
c2abccec
MK
1667 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1668 if (ret < 0) {
3fba9d81
SH
1669 error_report("error while writing sector %" PRId64
1670 ": %s", sector_num, strerror(-ret));
c2abccec
MK
1671 goto out;
1672 }
ea2384d3
FB
1673 }
1674 sector_num += n1;
1675 n -= n1;
1676 buf1 += n1 * 512;
1677 }
802c3d4c 1678 qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
ea2384d3
FB
1679 }
1680 }
c2abccec 1681out:
13c28af8
PL
1682 if (!ret) {
1683 qemu_progress_print(100, 0);
1684 }
6b837bc4 1685 qemu_progress_end();
c2abccec
MK
1686 free_option_parameters(create_options);
1687 free_option_parameters(param);
bb1c0597 1688 qemu_vfree(buf);
ef80654d
WX
1689 if (sn_opts) {
1690 qemu_opts_del(sn_opts);
1691 }
c2abccec 1692 if (out_bs) {
4f6fd349 1693 bdrv_unref(out_bs);
c2abccec 1694 }
31ca34b8
JS
1695 if (bs) {
1696 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1697 if (bs[bs_i]) {
4f6fd349 1698 bdrv_unref(bs[bs_i]);
31ca34b8 1699 }
c2abccec 1700 }
7267c094 1701 g_free(bs);
c2abccec 1702 }
64bb01aa
KW
1703fail_getopt:
1704 g_free(options);
1705
c2abccec
MK
1706 if (ret) {
1707 return 1;
1708 }
ea2384d3
FB
1709 return 0;
1710}
1711
57d1a2b6 1712
faea38e7
FB
1713static void dump_snapshots(BlockDriverState *bs)
1714{
1715 QEMUSnapshotInfo *sn_tab, *sn;
1716 int nb_sns, i;
faea38e7
FB
1717
1718 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1719 if (nb_sns <= 0)
1720 return;
1721 printf("Snapshot list:\n");
5b917044
WX
1722 bdrv_snapshot_dump(fprintf, stdout, NULL);
1723 printf("\n");
faea38e7
FB
1724 for(i = 0; i < nb_sns; i++) {
1725 sn = &sn_tab[i];
5b917044
WX
1726 bdrv_snapshot_dump(fprintf, stdout, sn);
1727 printf("\n");
faea38e7 1728 }
7267c094 1729 g_free(sn_tab);
faea38e7
FB
1730}
1731
9699bf0d
SH
1732static void dump_json_image_info_list(ImageInfoList *list)
1733{
1734 Error *errp = NULL;
1735 QString *str;
1736 QmpOutputVisitor *ov = qmp_output_visitor_new();
1737 QObject *obj;
1738 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1739 &list, NULL, &errp);
1740 obj = qmp_output_get_qobject(ov);
1741 str = qobject_to_json_pretty(obj);
1742 assert(str != NULL);
1743 printf("%s\n", qstring_get_str(str));
1744 qobject_decref(obj);
1745 qmp_output_visitor_cleanup(ov);
1746 QDECREF(str);
1747}
1748
c054b3fd
BC
1749static void dump_json_image_info(ImageInfo *info)
1750{
1751 Error *errp = NULL;
1752 QString *str;
1753 QmpOutputVisitor *ov = qmp_output_visitor_new();
1754 QObject *obj;
1755 visit_type_ImageInfo(qmp_output_get_visitor(ov),
1756 &info, NULL, &errp);
1757 obj = qmp_output_get_qobject(ov);
1758 str = qobject_to_json_pretty(obj);
1759 assert(str != NULL);
1760 printf("%s\n", qstring_get_str(str));
1761 qobject_decref(obj);
1762 qmp_output_visitor_cleanup(ov);
1763 QDECREF(str);
1764}
1765
9699bf0d
SH
1766static void dump_human_image_info_list(ImageInfoList *list)
1767{
1768 ImageInfoList *elem;
1769 bool delim = false;
1770
1771 for (elem = list; elem; elem = elem->next) {
1772 if (delim) {
1773 printf("\n");
1774 }
1775 delim = true;
1776
5b917044 1777 bdrv_image_info_dump(fprintf, stdout, elem->value);
9699bf0d
SH
1778 }
1779}
1780
1781static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1782{
1783 return strcmp(a, b) == 0;
1784}
1785
1786/**
1787 * Open an image file chain and return an ImageInfoList
1788 *
1789 * @filename: topmost image filename
1790 * @fmt: topmost image format (may be NULL to autodetect)
1791 * @chain: true - enumerate entire backing file chain
1792 * false - only topmost image file
1793 *
1794 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1795 * image file. If there was an error a message will have been printed to
1796 * stderr.
1797 */
1798static ImageInfoList *collect_image_info_list(const char *filename,
1799 const char *fmt,
1800 bool chain)
1801{
1802 ImageInfoList *head = NULL;
1803 ImageInfoList **last = &head;
1804 GHashTable *filenames;
43526ec8 1805 Error *err = NULL;
9699bf0d
SH
1806
1807 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1808
1809 while (filename) {
1810 BlockDriverState *bs;
1811 ImageInfo *info;
1812 ImageInfoList *elem;
1813
1814 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1815 error_report("Backing file '%s' creates an infinite loop.",
1816 filename);
1817 goto err;
1818 }
1819 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1820
9ffe3332
KW
1821 bs = bdrv_new_open("image", filename, fmt,
1822 BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
9699bf0d
SH
1823 if (!bs) {
1824 goto err;
1825 }
1826
43526ec8 1827 bdrv_query_image_info(bs, &info, &err);
84d18f06 1828 if (err) {
43526ec8
WX
1829 error_report("%s", error_get_pretty(err));
1830 error_free(err);
bdf866fe 1831 bdrv_unref(bs);
43526ec8 1832 goto err;
fb0ed453 1833 }
9699bf0d
SH
1834
1835 elem = g_new0(ImageInfoList, 1);
1836 elem->value = info;
1837 *last = elem;
1838 last = &elem->next;
1839
4f6fd349 1840 bdrv_unref(bs);
9699bf0d
SH
1841
1842 filename = fmt = NULL;
1843 if (chain) {
1844 if (info->has_full_backing_filename) {
1845 filename = info->full_backing_filename;
1846 } else if (info->has_backing_filename) {
1847 filename = info->backing_filename;
1848 }
1849 if (info->has_backing_filename_format) {
1850 fmt = info->backing_filename_format;
1851 }
1852 }
1853 }
1854 g_hash_table_destroy(filenames);
1855 return head;
1856
1857err:
1858 qapi_free_ImageInfoList(head);
1859 g_hash_table_destroy(filenames);
1860 return NULL;
1861}
1862
c054b3fd
BC
1863static int img_info(int argc, char **argv)
1864{
1865 int c;
1866 OutputFormat output_format = OFORMAT_HUMAN;
9699bf0d 1867 bool chain = false;
c054b3fd 1868 const char *filename, *fmt, *output;
9699bf0d 1869 ImageInfoList *list;
c054b3fd 1870
ea2384d3 1871 fmt = NULL;
c054b3fd 1872 output = NULL;
ea2384d3 1873 for(;;) {
c054b3fd
BC
1874 int option_index = 0;
1875 static const struct option long_options[] = {
1876 {"help", no_argument, 0, 'h'},
1877 {"format", required_argument, 0, 'f'},
1878 {"output", required_argument, 0, OPTION_OUTPUT},
9699bf0d 1879 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
c054b3fd
BC
1880 {0, 0, 0, 0}
1881 };
1882 c = getopt_long(argc, argv, "f:h",
1883 long_options, &option_index);
b8fb60da 1884 if (c == -1) {
ea2384d3 1885 break;
b8fb60da 1886 }
ea2384d3 1887 switch(c) {
ef87394c 1888 case '?':
ea2384d3
FB
1889 case 'h':
1890 help();
1891 break;
1892 case 'f':
1893 fmt = optarg;
1894 break;
c054b3fd
BC
1895 case OPTION_OUTPUT:
1896 output = optarg;
1897 break;
9699bf0d
SH
1898 case OPTION_BACKING_CHAIN:
1899 chain = true;
1900 break;
ea2384d3
FB
1901 }
1902 }
fc11eb26 1903 if (optind != argc - 1) {
ac1307ab 1904 error_exit("Expecting one image file name");
b8fb60da 1905 }
ea2384d3
FB
1906 filename = argv[optind++];
1907
c054b3fd
BC
1908 if (output && !strcmp(output, "json")) {
1909 output_format = OFORMAT_JSON;
1910 } else if (output && !strcmp(output, "human")) {
1911 output_format = OFORMAT_HUMAN;
1912 } else if (output) {
1913 error_report("--output must be used with human or json as argument.");
c2abccec
MK
1914 return 1;
1915 }
c054b3fd 1916
9699bf0d
SH
1917 list = collect_image_info_list(filename, fmt, chain);
1918 if (!list) {
c2abccec 1919 return 1;
faea38e7 1920 }
c054b3fd 1921
c054b3fd
BC
1922 switch (output_format) {
1923 case OFORMAT_HUMAN:
9699bf0d 1924 dump_human_image_info_list(list);
c054b3fd
BC
1925 break;
1926 case OFORMAT_JSON:
9699bf0d
SH
1927 if (chain) {
1928 dump_json_image_info_list(list);
1929 } else {
1930 dump_json_image_info(list->value);
1931 }
c054b3fd 1932 break;
faea38e7 1933 }
c054b3fd 1934
9699bf0d 1935 qapi_free_ImageInfoList(list);
ea2384d3
FB
1936 return 0;
1937}
1938
4c93a13b
PB
1939
1940typedef struct MapEntry {
1941 int flags;
1942 int depth;
1943 int64_t start;
1944 int64_t length;
1945 int64_t offset;
1946 BlockDriverState *bs;
1947} MapEntry;
1948
1949static void dump_map_entry(OutputFormat output_format, MapEntry *e,
1950 MapEntry *next)
1951{
1952 switch (output_format) {
1953 case OFORMAT_HUMAN:
1954 if ((e->flags & BDRV_BLOCK_DATA) &&
1955 !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
1956 error_report("File contains external, encrypted or compressed clusters.");
1957 exit(1);
1958 }
1959 if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
1960 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
1961 e->start, e->length, e->offset, e->bs->filename);
1962 }
1963 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
1964 * Modify the flags here to allow more coalescing.
1965 */
1966 if (next &&
1967 (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
1968 next->flags &= ~BDRV_BLOCK_DATA;
1969 next->flags |= BDRV_BLOCK_ZERO;
1970 }
1971 break;
1972 case OFORMAT_JSON:
1973 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
1974 " \"zero\": %s, \"data\": %s",
1975 (e->start == 0 ? "[" : ",\n"),
1976 e->start, e->length, e->depth,
1977 (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
1978 (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
1979 if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
c745bfb4 1980 printf(", \"offset\": %"PRId64"", e->offset);
4c93a13b
PB
1981 }
1982 putchar('}');
1983
1984 if (!next) {
1985 printf("]\n");
1986 }
1987 break;
1988 }
1989}
1990
1991static int get_block_status(BlockDriverState *bs, int64_t sector_num,
1992 int nb_sectors, MapEntry *e)
1993{
1994 int64_t ret;
1995 int depth;
1996
1997 /* As an optimization, we could cache the current range of unallocated
1998 * clusters in each file of the chain, and avoid querying the same
1999 * range repeatedly.
2000 */
2001
2002 depth = 0;
2003 for (;;) {
2004 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
2005 if (ret < 0) {
2006 return ret;
2007 }
2008 assert(nb_sectors);
2009 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2010 break;
2011 }
2012 bs = bs->backing_hd;
2013 if (bs == NULL) {
2014 ret = 0;
2015 break;
2016 }
2017
2018 depth++;
2019 }
2020
2021 e->start = sector_num * BDRV_SECTOR_SIZE;
2022 e->length = nb_sectors * BDRV_SECTOR_SIZE;
2023 e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
2024 e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
2025 e->depth = depth;
2026 e->bs = bs;
2027 return 0;
2028}
2029
2030static int img_map(int argc, char **argv)
2031{
2032 int c;
2033 OutputFormat output_format = OFORMAT_HUMAN;
2034 BlockDriverState *bs;
2035 const char *filename, *fmt, *output;
2036 int64_t length;
2037 MapEntry curr = { .length = 0 }, next;
2038 int ret = 0;
2039
2040 fmt = NULL;
2041 output = NULL;
2042 for (;;) {
2043 int option_index = 0;
2044 static const struct option long_options[] = {
2045 {"help", no_argument, 0, 'h'},
2046 {"format", required_argument, 0, 'f'},
2047 {"output", required_argument, 0, OPTION_OUTPUT},
2048 {0, 0, 0, 0}
2049 };
2050 c = getopt_long(argc, argv, "f:h",
2051 long_options, &option_index);
2052 if (c == -1) {
2053 break;
2054 }
2055 switch (c) {
2056 case '?':
2057 case 'h':
2058 help();
2059 break;
2060 case 'f':
2061 fmt = optarg;
2062 break;
2063 case OPTION_OUTPUT:
2064 output = optarg;
2065 break;
2066 }
2067 }
ac1307ab
FZ
2068 if (optind != argc - 1) {
2069 error_exit("Expecting one image file name");
4c93a13b 2070 }
ac1307ab 2071 filename = argv[optind];
4c93a13b
PB
2072
2073 if (output && !strcmp(output, "json")) {
2074 output_format = OFORMAT_JSON;
2075 } else if (output && !strcmp(output, "human")) {
2076 output_format = OFORMAT_HUMAN;
2077 } else if (output) {
2078 error_report("--output must be used with human or json as argument.");
2079 return 1;
2080 }
2081
9ffe3332 2082 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
4c93a13b
PB
2083 if (!bs) {
2084 return 1;
2085 }
2086
2087 if (output_format == OFORMAT_HUMAN) {
2088 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2089 }
2090
2091 length = bdrv_getlength(bs);
2092 while (curr.start + curr.length < length) {
2093 int64_t nsectors_left;
2094 int64_t sector_num;
2095 int n;
2096
2097 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2098
2099 /* Probe up to 1 GiB at a time. */
2100 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2101 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2102 ret = get_block_status(bs, sector_num, n, &next);
2103
2104 if (ret < 0) {
2105 error_report("Could not read file metadata: %s", strerror(-ret));
2106 goto out;
2107 }
2108
2109 if (curr.length != 0 && curr.flags == next.flags &&
2110 curr.depth == next.depth &&
2111 ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
2112 curr.offset + curr.length == next.offset)) {
2113 curr.length += next.length;
2114 continue;
2115 }
2116
2117 if (curr.length > 0) {
2118 dump_map_entry(output_format, &curr, &next);
2119 }
2120 curr = next;
2121 }
2122
2123 dump_map_entry(output_format, &curr, NULL);
2124
2125out:
2126 bdrv_unref(bs);
2127 return ret < 0;
2128}
2129
f7b4a940
AL
2130#define SNAPSHOT_LIST 1
2131#define SNAPSHOT_CREATE 2
2132#define SNAPSHOT_APPLY 3
2133#define SNAPSHOT_DELETE 4
2134
153859be 2135static int img_snapshot(int argc, char **argv)
f7b4a940
AL
2136{
2137 BlockDriverState *bs;
2138 QEMUSnapshotInfo sn;
2139 char *filename, *snapshot_name = NULL;
c2abccec 2140 int c, ret = 0, bdrv_oflags;
f7b4a940
AL
2141 int action = 0;
2142 qemu_timeval tv;
f382d43a 2143 bool quiet = false;
a89d89d3 2144 Error *err = NULL;
f7b4a940 2145
710da702 2146 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
f7b4a940
AL
2147 /* Parse commandline parameters */
2148 for(;;) {
f382d43a 2149 c = getopt(argc, argv, "la:c:d:hq");
b8fb60da 2150 if (c == -1) {
f7b4a940 2151 break;
b8fb60da 2152 }
f7b4a940 2153 switch(c) {
ef87394c 2154 case '?':
f7b4a940
AL
2155 case 'h':
2156 help();
153859be 2157 return 0;
f7b4a940
AL
2158 case 'l':
2159 if (action) {
ac1307ab 2160 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 2161 return 0;
f7b4a940
AL
2162 }
2163 action = SNAPSHOT_LIST;
f5edb014 2164 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
f7b4a940
AL
2165 break;
2166 case 'a':
2167 if (action) {
ac1307ab 2168 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 2169 return 0;
f7b4a940
AL
2170 }
2171 action = SNAPSHOT_APPLY;
2172 snapshot_name = optarg;
2173 break;
2174 case 'c':
2175 if (action) {
ac1307ab 2176 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 2177 return 0;
f7b4a940
AL
2178 }
2179 action = SNAPSHOT_CREATE;
2180 snapshot_name = optarg;
2181 break;
2182 case 'd':
2183 if (action) {
ac1307ab 2184 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
153859be 2185 return 0;
f7b4a940
AL
2186 }
2187 action = SNAPSHOT_DELETE;
2188 snapshot_name = optarg;
2189 break;
f382d43a
MR
2190 case 'q':
2191 quiet = true;
2192 break;
f7b4a940
AL
2193 }
2194 }
2195
fc11eb26 2196 if (optind != argc - 1) {
ac1307ab 2197 error_exit("Expecting one image file name");
b8fb60da 2198 }
f7b4a940
AL
2199 filename = argv[optind++];
2200
2201 /* Open the image */
9ffe3332 2202 bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
c2abccec
MK
2203 if (!bs) {
2204 return 1;
2205 }
f7b4a940
AL
2206
2207 /* Perform the requested action */
2208 switch(action) {
2209 case SNAPSHOT_LIST:
2210 dump_snapshots(bs);
2211 break;
2212
2213 case SNAPSHOT_CREATE:
2214 memset(&sn, 0, sizeof(sn));
2215 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2216
2217 qemu_gettimeofday(&tv);
2218 sn.date_sec = tv.tv_sec;
2219 sn.date_nsec = tv.tv_usec * 1000;
2220
2221 ret = bdrv_snapshot_create(bs, &sn);
b8fb60da 2222 if (ret) {
15654a6d 2223 error_report("Could not create snapshot '%s': %d (%s)",
f7b4a940 2224 snapshot_name, ret, strerror(-ret));
b8fb60da 2225 }
f7b4a940
AL
2226 break;
2227
2228 case SNAPSHOT_APPLY:
2229 ret = bdrv_snapshot_goto(bs, snapshot_name);
b8fb60da 2230 if (ret) {
15654a6d 2231 error_report("Could not apply snapshot '%s': %d (%s)",
f7b4a940 2232 snapshot_name, ret, strerror(-ret));
b8fb60da 2233 }
f7b4a940
AL
2234 break;
2235
2236 case SNAPSHOT_DELETE:
a89d89d3 2237 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
84d18f06 2238 if (err) {
a89d89d3
WX
2239 error_report("Could not delete snapshot '%s': (%s)",
2240 snapshot_name, error_get_pretty(err));
2241 error_free(err);
2242 ret = 1;
b8fb60da 2243 }
f7b4a940
AL
2244 break;
2245 }
2246
2247 /* Cleanup */
4f6fd349 2248 bdrv_unref(bs);
c2abccec
MK
2249 if (ret) {
2250 return 1;
2251 }
153859be 2252 return 0;
f7b4a940
AL
2253}
2254
3e85c6fd
KW
2255static int img_rebase(int argc, char **argv)
2256{
c2abccec 2257 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
f163d073 2258 BlockDriver *old_backing_drv, *new_backing_drv;
3e85c6fd 2259 char *filename;
661a0f71 2260 const char *fmt, *cache, *out_basefmt, *out_baseimg;
3e85c6fd
KW
2261 int c, flags, ret;
2262 int unsafe = 0;
6b837bc4 2263 int progress = 0;
f382d43a 2264 bool quiet = false;
34b5d2c6 2265 Error *local_err = NULL;
3e85c6fd
KW
2266
2267 /* Parse commandline parameters */
e53dbee0 2268 fmt = NULL;
661a0f71 2269 cache = BDRV_DEFAULT_CACHE;
3e85c6fd
KW
2270 out_baseimg = NULL;
2271 out_basefmt = NULL;
3e85c6fd 2272 for(;;) {
f382d43a 2273 c = getopt(argc, argv, "uhf:F:b:pt:q");
b8fb60da 2274 if (c == -1) {
3e85c6fd 2275 break;
b8fb60da 2276 }
3e85c6fd 2277 switch(c) {
ef87394c 2278 case '?':
3e85c6fd
KW
2279 case 'h':
2280 help();
2281 return 0;
e53dbee0
KW
2282 case 'f':
2283 fmt = optarg;
2284 break;
3e85c6fd
KW
2285 case 'F':
2286 out_basefmt = optarg;
2287 break;
2288 case 'b':
2289 out_baseimg = optarg;
2290 break;
2291 case 'u':
2292 unsafe = 1;
2293 break;
6b837bc4
JS
2294 case 'p':
2295 progress = 1;
2296 break;
661a0f71
FS
2297 case 't':
2298 cache = optarg;
2299 break;
f382d43a
MR
2300 case 'q':
2301 quiet = true;
2302 break;
3e85c6fd
KW
2303 }
2304 }
2305
f382d43a
MR
2306 if (quiet) {
2307 progress = 0;
2308 }
2309
ac1307ab
FZ
2310 if (optind != argc - 1) {
2311 error_exit("Expecting one image file name");
2312 }
2313 if (!unsafe && !out_baseimg) {
2314 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
b8fb60da 2315 }
3e85c6fd
KW
2316 filename = argv[optind++];
2317
6b837bc4
JS
2318 qemu_progress_init(progress, 2.0);
2319 qemu_progress_print(0, 100);
2320
661a0f71 2321 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
c3993cdc 2322 ret = bdrv_parse_cache_flags(cache, &flags);
661a0f71
FS
2323 if (ret < 0) {
2324 error_report("Invalid cache option: %s", cache);
2325 return -1;
2326 }
2327
3e85c6fd
KW
2328 /*
2329 * Open the images.
2330 *
2331 * Ignore the old backing file for unsafe rebase in case we want to correct
2332 * the reference to a renamed or moved backing file.
2333 */
9ffe3332 2334 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
c2abccec
MK
2335 if (!bs) {
2336 return 1;
2337 }
3e85c6fd
KW
2338
2339 /* Find the right drivers for the backing files */
2340 old_backing_drv = NULL;
2341 new_backing_drv = NULL;
2342
2343 if (!unsafe && bs->backing_format[0] != '\0') {
2344 old_backing_drv = bdrv_find_format(bs->backing_format);
2345 if (old_backing_drv == NULL) {
15654a6d 2346 error_report("Invalid format name: '%s'", bs->backing_format);
c2abccec
MK
2347 ret = -1;
2348 goto out;
3e85c6fd
KW
2349 }
2350 }
2351
2352 if (out_basefmt != NULL) {
2353 new_backing_drv = bdrv_find_format(out_basefmt);
2354 if (new_backing_drv == NULL) {
15654a6d 2355 error_report("Invalid format name: '%s'", out_basefmt);
c2abccec
MK
2356 ret = -1;
2357 goto out;
3e85c6fd
KW
2358 }
2359 }
2360
2361 /* For safe rebasing we need to compare old and new backing file */
2362 if (unsafe) {
2363 /* Make the compiler happy */
2364 bs_old_backing = NULL;
2365 bs_new_backing = NULL;
2366 } else {
2367 char backing_name[1024];
2368
98522f63 2369 bs_old_backing = bdrv_new("old_backing", &error_abort);
3e85c6fd 2370 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
ddf5636d 2371 ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, BDRV_O_FLAGS,
34b5d2c6 2372 old_backing_drv, &local_err);
c2abccec 2373 if (ret) {
34b5d2c6
HR
2374 error_report("Could not open old backing file '%s': %s",
2375 backing_name, error_get_pretty(local_err));
2376 error_free(local_err);
c2abccec 2377 goto out;
3e85c6fd 2378 }
a616673d 2379 if (out_baseimg[0]) {
98522f63 2380 bs_new_backing = bdrv_new("new_backing", &error_abort);
ddf5636d
HR
2381 ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL,
2382 BDRV_O_FLAGS, new_backing_drv, &local_err);
a616673d 2383 if (ret) {
34b5d2c6
HR
2384 error_report("Could not open new backing file '%s': %s",
2385 out_baseimg, error_get_pretty(local_err));
2386 error_free(local_err);
a616673d
AB
2387 goto out;
2388 }
3e85c6fd
KW
2389 }
2390 }
2391
2392 /*
2393 * Check each unallocated cluster in the COW file. If it is unallocated,
2394 * accesses go to the backing file. We must therefore compare this cluster
2395 * in the old and new backing file, and if they differ we need to copy it
2396 * from the old backing file into the COW file.
2397 *
2398 * If qemu-img crashes during this step, no harm is done. The content of
2399 * the image is the same as the original one at any time.
2400 */
2401 if (!unsafe) {
2402 uint64_t num_sectors;
87a1b3e3 2403 uint64_t old_backing_num_sectors;
a616673d 2404 uint64_t new_backing_num_sectors = 0;
3e85c6fd 2405 uint64_t sector;
cc60e327 2406 int n;
d6771bfa
T
2407 uint8_t * buf_old;
2408 uint8_t * buf_new;
1f710495 2409 float local_progress = 0;
d6771bfa 2410
bb1c0597
KW
2411 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2412 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
3e85c6fd
KW
2413
2414 bdrv_get_geometry(bs, &num_sectors);
87a1b3e3 2415 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
a616673d
AB
2416 if (bs_new_backing) {
2417 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2418 }
3e85c6fd 2419
1f710495
KW
2420 if (num_sectors != 0) {
2421 local_progress = (float)100 /
2422 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2423 }
2424
3e85c6fd
KW
2425 for (sector = 0; sector < num_sectors; sector += n) {
2426
2427 /* How many sectors can we handle with the next read? */
2428 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2429 n = (IO_BUF_SIZE / 512);
2430 } else {
2431 n = num_sectors - sector;
2432 }
2433
2434 /* If the cluster is allocated, we don't need to take action */
cc60e327 2435 ret = bdrv_is_allocated(bs, sector, n, &n);
d663640c
PB
2436 if (ret < 0) {
2437 error_report("error while reading image metadata: %s",
2438 strerror(-ret));
2439 goto out;
2440 }
cc60e327 2441 if (ret) {
3e85c6fd
KW
2442 continue;
2443 }
2444
87a1b3e3
KW
2445 /*
2446 * Read old and new backing file and take into consideration that
2447 * backing files may be smaller than the COW image.
2448 */
2449 if (sector >= old_backing_num_sectors) {
2450 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2451 } else {
2452 if (sector + n > old_backing_num_sectors) {
2453 n = old_backing_num_sectors - sector;
2454 }
2455
2456 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2457 if (ret < 0) {
2458 error_report("error while reading from old backing file");
2459 goto out;
2460 }
3e85c6fd 2461 }
87a1b3e3 2462
a616673d 2463 if (sector >= new_backing_num_sectors || !bs_new_backing) {
87a1b3e3
KW
2464 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2465 } else {
2466 if (sector + n > new_backing_num_sectors) {
2467 n = new_backing_num_sectors - sector;
2468 }
2469
2470 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2471 if (ret < 0) {
2472 error_report("error while reading from new backing file");
2473 goto out;
2474 }
3e85c6fd
KW
2475 }
2476
2477 /* If they differ, we need to write to the COW file */
2478 uint64_t written = 0;
2479
2480 while (written < n) {
2481 int pnum;
2482
2483 if (compare_sectors(buf_old + written * 512,
60b1bd4f 2484 buf_new + written * 512, n - written, &pnum))
3e85c6fd
KW
2485 {
2486 ret = bdrv_write(bs, sector + written,
2487 buf_old + written * 512, pnum);
2488 if (ret < 0) {
15654a6d 2489 error_report("Error while writing to COW image: %s",
3e85c6fd 2490 strerror(-ret));
c2abccec 2491 goto out;
3e85c6fd
KW
2492 }
2493 }
2494
2495 written += pnum;
2496 }
6b837bc4 2497 qemu_progress_print(local_progress, 100);
3e85c6fd 2498 }
d6771bfa 2499
bb1c0597
KW
2500 qemu_vfree(buf_old);
2501 qemu_vfree(buf_new);
3e85c6fd
KW
2502 }
2503
2504 /*
2505 * Change the backing file. All clusters that are different from the old
2506 * backing file are overwritten in the COW file now, so the visible content
2507 * doesn't change when we switch the backing file.
2508 */
a616673d
AB
2509 if (out_baseimg && *out_baseimg) {
2510 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2511 } else {
2512 ret = bdrv_change_backing_file(bs, NULL, NULL);
2513 }
2514
3e85c6fd 2515 if (ret == -ENOSPC) {
15654a6d
JS
2516 error_report("Could not change the backing file to '%s': No "
2517 "space left in the file header", out_baseimg);
3e85c6fd 2518 } else if (ret < 0) {
15654a6d 2519 error_report("Could not change the backing file to '%s': %s",
3e85c6fd
KW
2520 out_baseimg, strerror(-ret));
2521 }
2522
6b837bc4 2523 qemu_progress_print(100, 0);
3e85c6fd
KW
2524 /*
2525 * TODO At this point it is possible to check if any clusters that are
2526 * allocated in the COW file are the same in the backing file. If so, they
2527 * could be dropped from the COW file. Don't do this before switching the
2528 * backing file, in case of a crash this would lead to corruption.
2529 */
c2abccec 2530out:
6b837bc4 2531 qemu_progress_end();
3e85c6fd
KW
2532 /* Cleanup */
2533 if (!unsafe) {
eb863add 2534 if (bs_old_backing != NULL) {
4f6fd349 2535 bdrv_unref(bs_old_backing);
eb863add
KW
2536 }
2537 if (bs_new_backing != NULL) {
4f6fd349 2538 bdrv_unref(bs_new_backing);
eb863add 2539 }
3e85c6fd
KW
2540 }
2541
4f6fd349 2542 bdrv_unref(bs);
c2abccec
MK
2543 if (ret) {
2544 return 1;
2545 }
3e85c6fd
KW
2546 return 0;
2547}
2548
ae6b0ed6
SH
2549static int img_resize(int argc, char **argv)
2550{
2551 int c, ret, relative;
2552 const char *filename, *fmt, *size;
2553 int64_t n, total_size;
f382d43a 2554 bool quiet = false;
2a81998a 2555 BlockDriverState *bs = NULL;
20caf0f7
DXW
2556 QemuOpts *param;
2557 static QemuOptsList resize_options = {
2558 .name = "resize_options",
2559 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2560 .desc = {
2561 {
2562 .name = BLOCK_OPT_SIZE,
2563 .type = QEMU_OPT_SIZE,
2564 .help = "Virtual disk size"
2565 }, {
2566 /* end of list */
2567 }
ae6b0ed6 2568 },
ae6b0ed6
SH
2569 };
2570
e80fec7f
KW
2571 /* Remove size from argv manually so that negative numbers are not treated
2572 * as options by getopt. */
2573 if (argc < 3) {
ac1307ab 2574 error_exit("Not enough arguments");
e80fec7f
KW
2575 return 1;
2576 }
2577
2578 size = argv[--argc];
2579
2580 /* Parse getopt arguments */
ae6b0ed6
SH
2581 fmt = NULL;
2582 for(;;) {
f382d43a 2583 c = getopt(argc, argv, "f:hq");
ae6b0ed6
SH
2584 if (c == -1) {
2585 break;
2586 }
2587 switch(c) {
ef87394c 2588 case '?':
ae6b0ed6
SH
2589 case 'h':
2590 help();
2591 break;
2592 case 'f':
2593 fmt = optarg;
2594 break;
f382d43a
MR
2595 case 'q':
2596 quiet = true;
2597 break;
ae6b0ed6
SH
2598 }
2599 }
fc11eb26 2600 if (optind != argc - 1) {
ac1307ab 2601 error_exit("Expecting one image file name");
ae6b0ed6
SH
2602 }
2603 filename = argv[optind++];
ae6b0ed6
SH
2604
2605 /* Choose grow, shrink, or absolute resize mode */
2606 switch (size[0]) {
2607 case '+':
2608 relative = 1;
2609 size++;
2610 break;
2611 case '-':
2612 relative = -1;
2613 size++;
2614 break;
2615 default:
2616 relative = 0;
2617 break;
2618 }
2619
2620 /* Parse size */
87ea75d5 2621 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
20caf0f7 2622 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
ae6b0ed6 2623 /* Error message already printed when size parsing fails */
2a81998a 2624 ret = -1;
20caf0f7 2625 qemu_opts_del(param);
2a81998a 2626 goto out;
ae6b0ed6 2627 }
20caf0f7
DXW
2628 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2629 qemu_opts_del(param);
ae6b0ed6 2630
9ffe3332
KW
2631 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
2632 true, quiet);
c2abccec 2633 if (!bs) {
2a81998a
JS
2634 ret = -1;
2635 goto out;
c2abccec 2636 }
ae6b0ed6
SH
2637
2638 if (relative) {
2639 total_size = bdrv_getlength(bs) + n * relative;
2640 } else {
2641 total_size = n;
2642 }
2643 if (total_size <= 0) {
15654a6d 2644 error_report("New image size must be positive");
c2abccec
MK
2645 ret = -1;
2646 goto out;
ae6b0ed6
SH
2647 }
2648
2649 ret = bdrv_truncate(bs, total_size);
2650 switch (ret) {
2651 case 0:
f382d43a 2652 qprintf(quiet, "Image resized.\n");
ae6b0ed6
SH
2653 break;
2654 case -ENOTSUP:
259b2173 2655 error_report("This image does not support resize");
ae6b0ed6
SH
2656 break;
2657 case -EACCES:
15654a6d 2658 error_report("Image is read-only");
ae6b0ed6
SH
2659 break;
2660 default:
15654a6d 2661 error_report("Error resizing image (%d)", -ret);
ae6b0ed6
SH
2662 break;
2663 }
c2abccec 2664out:
2a81998a 2665 if (bs) {
4f6fd349 2666 bdrv_unref(bs);
2a81998a 2667 }
c2abccec
MK
2668 if (ret) {
2669 return 1;
2670 }
ae6b0ed6
SH
2671 return 0;
2672}
2673
6f176b48
HR
2674static int img_amend(int argc, char **argv)
2675{
2676 int c, ret = 0;
2677 char *options = NULL;
2678 QEMUOptionParameter *create_options = NULL, *options_param = NULL;
2679 const char *fmt = NULL, *filename;
2680 bool quiet = false;
2681 BlockDriverState *bs = NULL;
2682
2683 for (;;) {
2684 c = getopt(argc, argv, "hqf:o:");
2685 if (c == -1) {
2686 break;
2687 }
2688
2689 switch (c) {
2690 case 'h':
2691 case '?':
2692 help();
2693 break;
2694 case 'o':
626f84f3
KW
2695 if (!is_valid_option_list(optarg)) {
2696 error_report("Invalid option list: %s", optarg);
2697 ret = -1;
2698 goto out;
2699 }
2700 if (!options) {
2701 options = g_strdup(optarg);
2702 } else {
2703 char *old_options = options;
2704 options = g_strdup_printf("%s,%s", options, optarg);
2705 g_free(old_options);
2706 }
6f176b48
HR
2707 break;
2708 case 'f':
2709 fmt = optarg;
2710 break;
2711 case 'q':
2712 quiet = true;
2713 break;
2714 }
2715 }
2716
a283cb6e 2717 if (!options) {
ac1307ab 2718 error_exit("Must specify options (-o)");
6f176b48
HR
2719 }
2720
a283cb6e
KW
2721 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
2722 if (fmt && has_help_option(options)) {
2723 /* If a format is explicitly specified (and possibly no filename is
2724 * given), print option help here */
2725 ret = print_block_option_help(filename, fmt);
2726 goto out;
6f176b48
HR
2727 }
2728
a283cb6e 2729 if (optind != argc - 1) {
ac1307ab 2730 error_exit("Expecting one image file name");
a283cb6e 2731 }
6f176b48 2732
9ffe3332
KW
2733 bs = bdrv_new_open("image", filename, fmt,
2734 BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
6f176b48
HR
2735 if (!bs) {
2736 error_report("Could not open image '%s'", filename);
2737 ret = -1;
2738 goto out;
2739 }
2740
2741 fmt = bs->drv->format_name;
2742
626f84f3 2743 if (has_help_option(options)) {
a283cb6e 2744 /* If the format was auto-detected, print option help here */
6f176b48
HR
2745 ret = print_block_option_help(filename, fmt);
2746 goto out;
2747 }
2748
2749 create_options = append_option_parameters(create_options,
2750 bs->drv->create_options);
2751 options_param = parse_option_parameters(options, create_options,
2752 options_param);
2753 if (options_param == NULL) {
2754 error_report("Invalid options for file format '%s'", fmt);
2755 ret = -1;
2756 goto out;
2757 }
2758
2759 ret = bdrv_amend_options(bs, options_param);
2760 if (ret < 0) {
2761 error_report("Error while amending options: %s", strerror(-ret));
2762 goto out;
2763 }
2764
2765out:
2766 if (bs) {
2767 bdrv_unref(bs);
2768 }
2769 free_option_parameters(create_options);
2770 free_option_parameters(options_param);
626f84f3
KW
2771 g_free(options);
2772
6f176b48
HR
2773 if (ret) {
2774 return 1;
2775 }
2776 return 0;
2777}
2778
c227f099 2779static const img_cmd_t img_cmds[] = {
153859be
SB
2780#define DEF(option, callback, arg_string) \
2781 { option, callback },
2782#include "qemu-img-cmds.h"
2783#undef DEF
2784#undef GEN_DOCS
2785 { NULL, NULL, },
2786};
2787
ea2384d3
FB
2788int main(int argc, char **argv)
2789{
c227f099 2790 const img_cmd_t *cmd;
153859be 2791 const char *cmdname;
ea2384d3 2792
526eda14
MK
2793#ifdef CONFIG_POSIX
2794 signal(SIGPIPE, SIG_IGN);
2795#endif
2796
53f76e58 2797 error_set_progname(argv[0]);
10f5bff6 2798 qemu_init_exec_dir(argv[0]);
53f76e58 2799
2592c59a 2800 qemu_init_main_loop();
ea2384d3 2801 bdrv_init();
ac1307ab
FZ
2802 if (argc < 2) {
2803 error_exit("Not enough arguments");
2804 }
153859be 2805 cmdname = argv[1];
8f9b157e 2806 argc--; argv++;
153859be
SB
2807
2808 /* find the command */
2809 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2810 if (!strcmp(cmdname, cmd->name)) {
2811 return cmd->handler(argc, argv);
2812 }
ea2384d3 2813 }
153859be
SB
2814
2815 /* not found */
ac1307ab 2816 error_exit("Command not found: %s", cmdname);
ea2384d3 2817}