]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-img.c
Consolidate printing of block driver options
[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 */
faf07963 24#include "qemu-common.h"
9ea2ea71 25#include "qemu-option.h"
f7b4a940 26#include "osdep.h"
dc786bc9 27#include "sysemu.h"
ec36ba14 28#include "block_int.h"
9230eaf6 29#include <stdio.h>
ea2384d3 30
e8445331
FB
31#ifdef _WIN32
32#include <windows.h>
33#endif
34
c227f099 35typedef struct img_cmd_t {
153859be
SB
36 const char *name;
37 int (*handler)(int argc, char **argv);
c227f099 38} img_cmd_t;
153859be 39
137519ce 40/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
adfe078e 41#define BDRV_O_FLAGS BDRV_O_CACHE_WB
137519ce 42
8b7968f7 43static void GCC_FMT_ATTR(1, 2) error(const char *fmt, ...)
ea2384d3
FB
44{
45 va_list ap;
46 va_start(ap, fmt);
57d1a2b6 47 fprintf(stderr, "qemu-img: ");
ea2384d3
FB
48 vfprintf(stderr, fmt, ap);
49 fprintf(stderr, "\n");
ea2384d3
FB
50 va_end(ap);
51}
52
53static void format_print(void *opaque, const char *name)
54{
55 printf(" %s", name);
56}
57
d2c639d6 58/* Please keep in synch with qemu-img.texi */
3f379ab1 59static void help(void)
ea2384d3 60{
e00291c0
PB
61 const char *help_msg =
62 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
3f020d70 63 "usage: qemu-img command [command options]\n"
64 "QEMU disk image utility\n"
65 "\n"
66 "Command syntax:\n"
153859be
SB
67#define DEF(option, callback, arg_string) \
68 " " arg_string "\n"
69#include "qemu-img-cmds.h"
70#undef DEF
71#undef GEN_DOCS
3f020d70 72 "\n"
73 "Command parameters:\n"
74 " 'filename' is a disk image filename\n"
75 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
76 " 'size' is the disk image size in bytes. Optional suffixes\n"
77 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
78 " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
79 " 'output_filename' is the destination disk image filename\n"
80 " 'output_fmt' is the destination format\n"
81 " 'options' is a comma separated list of format specific options in a\n"
82 " name=value format. Use -o ? for an overview of the options supported by the\n"
83 " used format\n"
84 " '-c' indicates that target image must be compressed (qcow format only)\n"
85 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
86 " match exactly. The image doesn't need a working backing file before\n"
87 " rebasing in this case (useful for renaming the backing file)\n"
88 " '-h' with or without a command shows this help and lists the supported formats\n"
89 "\n"
90 "Parameters to snapshot subcommand:\n"
91 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
92 " '-a' applies a snapshot (revert disk to saved state)\n"
93 " '-c' creates a snapshot\n"
94 " '-d' deletes a snapshot\n"
e00291c0
PB
95 " '-l' lists all snapshots in the given image\n";
96
97 printf("%s\nSupported formats:", help_msg);
ea2384d3
FB
98 bdrv_iterate_format(format_print, NULL);
99 printf("\n");
100 exit(1);
101}
102
ea2384d3
FB
103#if defined(WIN32)
104/* XXX: put correct support for win32 */
105static int read_password(char *buf, int buf_size)
106{
107 int c, i;
108 printf("Password: ");
109 fflush(stdout);
110 i = 0;
111 for(;;) {
112 c = getchar();
113 if (c == '\n')
114 break;
115 if (i < (buf_size - 1))
116 buf[i++] = c;
117 }
118 buf[i] = '\0';
119 return 0;
120}
121
122#else
123
124#include <termios.h>
125
126static struct termios oldtty;
127
128static void term_exit(void)
129{
130 tcsetattr (0, TCSANOW, &oldtty);
131}
132
133static void term_init(void)
134{
135 struct termios tty;
136
137 tcgetattr (0, &tty);
138 oldtty = tty;
139
140 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
141 |INLCR|IGNCR|ICRNL|IXON);
142 tty.c_oflag |= OPOST;
143 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
144 tty.c_cflag &= ~(CSIZE|PARENB);
145 tty.c_cflag |= CS8;
146 tty.c_cc[VMIN] = 1;
147 tty.c_cc[VTIME] = 0;
3b46e624 148
ea2384d3
FB
149 tcsetattr (0, TCSANOW, &tty);
150
151 atexit(term_exit);
152}
153
3f379ab1 154static int read_password(char *buf, int buf_size)
ea2384d3
FB
155{
156 uint8_t ch;
157 int i, ret;
158
159 printf("password: ");
160 fflush(stdout);
161 term_init();
162 i = 0;
163 for(;;) {
164 ret = read(0, &ch, 1);
165 if (ret == -1) {
166 if (errno == EAGAIN || errno == EINTR) {
167 continue;
168 } else {
169 ret = -1;
170 break;
171 }
172 } else if (ret == 0) {
173 ret = -1;
174 break;
175 } else {
176 if (ch == '\r') {
177 ret = 0;
178 break;
179 }
180 if (i < (buf_size - 1))
181 buf[i++] = ch;
182 }
183 }
184 term_exit();
185 buf[i] = '\0';
186 printf("\n");
187 return ret;
188}
189#endif
190
4ac8aacd
JS
191static int print_block_option_help(const char *filename, const char *fmt)
192{
193 BlockDriver *drv, *proto_drv;
194 QEMUOptionParameter *create_options = NULL;
195
196 /* Find driver and parse its options */
197 drv = bdrv_find_format(fmt);
198 if (!drv) {
199 error("Unknown file format '%s'", fmt);
200 return 1;
201 }
202
203 proto_drv = bdrv_find_protocol(filename);
204 if (!proto_drv) {
205 error("Unknown protocol '%s'", filename);
206 return 1;
207 }
208
209 create_options = append_option_parameters(create_options,
210 drv->create_options);
211 create_options = append_option_parameters(create_options,
212 proto_drv->create_options);
213 print_option_help(create_options);
214 free_option_parameters(create_options);
215 return 0;
216}
217
75c23805 218static BlockDriverState *bdrv_new_open(const char *filename,
9bc378c1 219 const char *fmt,
f163d073 220 int flags)
75c23805
FB
221{
222 BlockDriverState *bs;
223 BlockDriver *drv;
224 char password[256];
225
226 bs = bdrv_new("");
c2abccec 227 if (!bs) {
75c23805 228 error("Not enough memory");
c2abccec
MK
229 goto fail;
230 }
75c23805
FB
231 if (fmt) {
232 drv = bdrv_find_format(fmt);
c2abccec 233 if (!drv) {
75c23805 234 error("Unknown file format '%s'", fmt);
c2abccec
MK
235 goto fail;
236 }
75c23805
FB
237 } else {
238 drv = NULL;
239 }
d6e9098e 240 if (bdrv_open(bs, filename, flags, drv) < 0) {
75c23805 241 error("Could not open '%s'", filename);
c2abccec 242 goto fail;
75c23805
FB
243 }
244 if (bdrv_is_encrypted(bs)) {
245 printf("Disk image '%s' is encrypted.\n", filename);
c2abccec 246 if (read_password(password, sizeof(password)) < 0) {
75c23805 247 error("No password given");
c2abccec
MK
248 goto fail;
249 }
250 if (bdrv_set_key(bs, password) < 0) {
75c23805 251 error("invalid password");
c2abccec
MK
252 goto fail;
253 }
75c23805
FB
254 }
255 return bs;
c2abccec
MK
256fail:
257 if (bs) {
258 bdrv_delete(bs);
259 }
260 return NULL;
75c23805
FB
261}
262
c2abccec 263static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
efa84d43
KW
264 int flags, const char *base_filename, const char *base_fmt)
265{
266 if (flags & BLOCK_FLAG_ENCRYPT) {
267 if (set_option_parameter(list, BLOCK_OPT_ENCRYPT, "on")) {
268 error("Encryption not supported for file format '%s'", fmt);
c2abccec 269 return -1;
efa84d43
KW
270 }
271 }
272 if (flags & BLOCK_FLAG_COMPAT6) {
273 if (set_option_parameter(list, BLOCK_OPT_COMPAT6, "on")) {
274 error("VMDK version 6 not supported for file format '%s'", fmt);
c2abccec 275 return -1;
efa84d43
KW
276 }
277 }
278
279 if (base_filename) {
280 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
281 error("Backing file not supported for file format '%s'", fmt);
c2abccec 282 return -1;
efa84d43
KW
283 }
284 }
285 if (base_fmt) {
286 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
287 error("Backing file format not supported for file format '%s'", fmt);
c2abccec 288 return -1;
efa84d43
KW
289 }
290 }
c2abccec 291 return 0;
efa84d43
KW
292}
293
ea2384d3
FB
294static int img_create(int argc, char **argv)
295{
c2abccec 296 int c, ret = 0, flags;
ea2384d3 297 const char *fmt = "raw";
9230eaf6 298 const char *base_fmt = NULL;
ea2384d3
FB
299 const char *filename;
300 const char *base_filename = NULL;
b50cbabc
MK
301 BlockDriver *drv, *proto_drv;
302 QEMUOptionParameter *param = NULL, *create_options = NULL;
9ea2ea71 303 char *options = NULL;
3b46e624 304
ec36ba14 305 flags = 0;
ea2384d3 306 for(;;) {
9ea2ea71 307 c = getopt(argc, argv, "F:b:f:he6o:");
ea2384d3
FB
308 if (c == -1)
309 break;
310 switch(c) {
311 case 'h':
312 help();
313 break;
9230eaf6
AL
314 case 'F':
315 base_fmt = optarg;
316 break;
ea2384d3
FB
317 case 'b':
318 base_filename = optarg;
319 break;
320 case 'f':
321 fmt = optarg;
322 break;
323 case 'e':
ec36ba14 324 flags |= BLOCK_FLAG_ENCRYPT;
ea2384d3 325 break;
d8871c5a 326 case '6':
ec36ba14 327 flags |= BLOCK_FLAG_COMPAT6;
d8871c5a 328 break;
9ea2ea71
KW
329 case 'o':
330 options = optarg;
331 break;
ea2384d3
FB
332 }
333 }
9230eaf6 334
b50cbabc
MK
335 /* Get the filename */
336 if (optind >= argc)
337 help();
338 filename = argv[optind++];
339
4ac8aacd
JS
340 if (options && !strcmp(options, "?")) {
341 ret = print_block_option_help(filename, fmt);
342 goto out;
343 }
344
9ea2ea71
KW
345 /* Find driver and parse its options */
346 drv = bdrv_find_format(fmt);
c2abccec 347 if (!drv) {
9ea2ea71 348 error("Unknown file format '%s'", fmt);
c2abccec
MK
349 return 1;
350 }
9230eaf6 351
b50cbabc 352 proto_drv = bdrv_find_protocol(filename);
c2abccec 353 if (!proto_drv) {
b50cbabc 354 error("Unknown protocol '%s'", filename);
c2abccec
MK
355 return 1;
356 }
b50cbabc
MK
357
358 create_options = append_option_parameters(create_options,
359 drv->create_options);
360 create_options = append_option_parameters(create_options,
361 proto_drv->create_options);
362
9f56640c 363 /* Create parameter list with default values */
b50cbabc 364 param = parse_option_parameters("", create_options, param);
9f56640c
KW
365 set_option_parameter_int(param, BLOCK_OPT_SIZE, -1);
366
367 /* Parse -o options */
9ea2ea71 368 if (options) {
b50cbabc 369 param = parse_option_parameters(options, create_options, param);
9ea2ea71
KW
370 if (param == NULL) {
371 error("Invalid options for file format '%s'.", fmt);
c2abccec
MK
372 ret = -1;
373 goto out;
9ea2ea71 374 }
9ea2ea71
KW
375 }
376
377 /* Add size to parameters */
378 if (optind < argc) {
379 set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
380 }
381
382 /* Add old-style options to parameters */
c2abccec
MK
383 ret = add_old_style_options(fmt, param, flags, base_filename, base_fmt);
384 if (ret < 0) {
385 goto out;
386 }
9ea2ea71
KW
387
388 // The size for the image must always be specified, with one exception:
389 // If we are using a backing file, we can obtain the size from there
9f56640c 390 if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
9ea2ea71
KW
391
392 QEMUOptionParameter *backing_file =
393 get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
394 QEMUOptionParameter *backing_fmt =
395 get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
396
397 if (backing_file && backing_file->value.s) {
398 BlockDriverState *bs;
399 uint64_t size;
400 const char *fmt = NULL;
401 char buf[32];
402
403 if (backing_fmt && backing_fmt->value.s) {
404 if (bdrv_find_format(backing_fmt->value.s)) {
405 fmt = backing_fmt->value.s;
406 } else {
407 error("Unknown backing file format '%s'",
408 backing_fmt->value.s);
c2abccec
MK
409 ret = -1;
410 goto out;
9ea2ea71
KW
411 }
412 }
413
adfe078e 414 bs = bdrv_new_open(backing_file->value.s, fmt, BDRV_O_FLAGS);
c2abccec
MK
415 if (!bs) {
416 ret = -1;
417 goto out;
418 }
9ea2ea71
KW
419 bdrv_get_geometry(bs, &size);
420 size *= 512;
421 bdrv_delete(bs);
422
423 snprintf(buf, sizeof(buf), "%" PRId64, size);
424 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
425 } else {
426 error("Image creation needs a size parameter");
c2abccec
MK
427 ret = -1;
428 goto out;
9ea2ea71 429 }
75c23805 430 }
9ea2ea71
KW
431
432 printf("Formatting '%s', fmt=%s ", filename, fmt);
433 print_option_parameters(param);
434 puts("");
435
436 ret = bdrv_create(drv, filename, param);
b50cbabc 437 free_option_parameters(create_options);
9ea2ea71
KW
438 free_option_parameters(param);
439
ea2384d3
FB
440 if (ret < 0) {
441 if (ret == -ENOTSUP) {
3c56521b 442 error("Formatting or formatting option not supported for file format '%s'", fmt);
6e9ea0c0
AJ
443 } else if (ret == -EFBIG) {
444 error("The image size is too large for file format '%s'", fmt);
ea2384d3 445 } else {
3e7896de 446 error("%s: error while creating %s: %s", filename, fmt, strerror(-ret));
ea2384d3
FB
447 }
448 }
c2abccec
MK
449out:
450 if (ret) {
451 return 1;
452 }
ea2384d3
FB
453 return 0;
454}
455
e076f338
KW
456/*
457 * Checks an image for consistency. Exit codes:
458 *
459 * 0 - Check completed, image is good
460 * 1 - Check not completed because of internal errors
461 * 2 - Check completed, image is corrupted
462 * 3 - Check completed, image has leaked clusters, but is good otherwise
463 */
1585969c
AL
464static int img_check(int argc, char **argv)
465{
466 int c, ret;
467 const char *filename, *fmt;
1585969c 468 BlockDriverState *bs;
e076f338 469 BdrvCheckResult result;
1585969c
AL
470
471 fmt = NULL;
472 for(;;) {
473 c = getopt(argc, argv, "f:h");
474 if (c == -1)
475 break;
476 switch(c) {
477 case 'h':
478 help();
479 break;
480 case 'f':
481 fmt = optarg;
482 break;
483 }
484 }
485 if (optind >= argc)
486 help();
487 filename = argv[optind++];
488
adfe078e 489 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS);
c2abccec
MK
490 if (!bs) {
491 return 1;
492 }
e076f338
KW
493 ret = bdrv_check(bs, &result);
494
495 if (ret == -ENOTSUP) {
1585969c 496 error("This image format does not support checks");
e076f338
KW
497 bdrv_delete(bs);
498 return 1;
499 }
500
501 if (!(result.corruptions || result.leaks || result.check_errors)) {
502 printf("No errors were found on the image.\n");
503 } else {
504 if (result.corruptions) {
505 printf("\n%d errors were found on the image.\n"
506 "Data may be corrupted, or further writes to the image "
507 "may corrupt it.\n",
508 result.corruptions);
509 }
510
511 if (result.leaks) {
512 printf("\n%d leaked clusters were found on the image.\n"
513 "This means waste of disk space, but no harm to data.\n",
514 result.leaks);
515 }
516
517 if (result.check_errors) {
518 printf("\n%d internal errors have occurred during the check.\n",
519 result.check_errors);
1585969c 520 }
1585969c
AL
521 }
522
523 bdrv_delete(bs);
e076f338
KW
524
525 if (ret < 0 || result.check_errors) {
526 printf("\nAn error has occurred during the check: %s\n"
527 "The check is not complete and may have missed error.\n",
528 strerror(-ret));
c2abccec
MK
529 return 1;
530 }
e076f338
KW
531
532 if (result.corruptions) {
533 return 2;
534 } else if (result.leaks) {
535 return 3;
536 } else {
537 return 0;
538 }
1585969c
AL
539}
540
ea2384d3
FB
541static int img_commit(int argc, char **argv)
542{
543 int c, ret;
544 const char *filename, *fmt;
ea2384d3
FB
545 BlockDriverState *bs;
546
547 fmt = NULL;
548 for(;;) {
549 c = getopt(argc, argv, "f:h");
550 if (c == -1)
551 break;
552 switch(c) {
553 case 'h':
554 help();
555 break;
556 case 'f':
557 fmt = optarg;
558 break;
559 }
560 }
5fafdf24 561 if (optind >= argc)
ea2384d3
FB
562 help();
563 filename = argv[optind++];
564
adfe078e 565 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR);
c2abccec
MK
566 if (!bs) {
567 return 1;
568 }
ea2384d3
FB
569 ret = bdrv_commit(bs);
570 switch(ret) {
571 case 0:
572 printf("Image committed.\n");
573 break;
574 case -ENOENT:
575 error("No disk inserted");
576 break;
577 case -EACCES:
578 error("Image is read-only");
579 break;
580 case -ENOTSUP:
581 error("Image is already committed");
582 break;
583 default:
584 error("Error while committing image");
585 break;
586 }
587
588 bdrv_delete(bs);
c2abccec
MK
589 if (ret) {
590 return 1;
591 }
ea2384d3
FB
592 return 0;
593}
594
595static int is_not_zero(const uint8_t *sector, int len)
596{
597 int i;
598 len >>= 2;
599 for(i = 0;i < len; i++) {
600 if (((uint32_t *)sector)[i] != 0)
601 return 1;
602 }
603 return 0;
604}
605
f58c7b35
TS
606/*
607 * Returns true iff the first sector pointed to by 'buf' contains at least
608 * a non-NUL byte.
609 *
610 * 'pnum' is set to the number of sectors (including and immediately following
611 * the first one) that are known to be in the same allocated/unallocated state.
612 */
ea2384d3
FB
613static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
614{
615 int v, i;
616
617 if (n <= 0) {
618 *pnum = 0;
619 return 0;
620 }
621 v = is_not_zero(buf, 512);
622 for(i = 1; i < n; i++) {
623 buf += 512;
624 if (v != is_not_zero(buf, 512))
625 break;
626 }
627 *pnum = i;
628 return v;
629}
630
3e85c6fd
KW
631/*
632 * Compares two buffers sector by sector. Returns 0 if the first sector of both
633 * buffers matches, non-zero otherwise.
634 *
635 * pnum is set to the number of sectors (including and immediately following
636 * the first one) that are known to have the same comparison result
637 */
638static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
639 int *pnum)
640{
641 int res, i;
642
643 if (n <= 0) {
644 *pnum = 0;
645 return 0;
646 }
647
648 res = !!memcmp(buf1, buf2, 512);
649 for(i = 1; i < n; i++) {
650 buf1 += 512;
651 buf2 += 512;
652
653 if (!!memcmp(buf1, buf2, 512) != res) {
654 break;
655 }
656 }
657
658 *pnum = i;
659 return res;
660}
661
80ee15a6 662#define IO_BUF_SIZE (2 * 1024 * 1024)
ea2384d3
FB
663
664static int img_convert(int argc, char **argv)
665{
c2abccec 666 int c, ret = 0, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
f58c7b35 667 const char *fmt, *out_fmt, *out_baseimg, *out_filename;
b50cbabc 668 BlockDriver *drv, *proto_drv;
c2abccec 669 BlockDriverState **bs = NULL, *out_bs = NULL;
96b8f136
TS
670 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
671 uint64_t bs_sectors;
c2abccec 672 uint8_t * buf = NULL;
ea2384d3 673 const uint8_t *buf1;
faea38e7 674 BlockDriverInfo bdi;
b50cbabc 675 QEMUOptionParameter *param = NULL, *create_options = NULL;
a18953fb 676 QEMUOptionParameter *out_baseimg_param;
efa84d43 677 char *options = NULL;
51ef6727 678 const char *snapshot_name = NULL;
ea2384d3
FB
679
680 fmt = NULL;
681 out_fmt = "raw";
f58c7b35 682 out_baseimg = NULL;
ec36ba14 683 flags = 0;
ea2384d3 684 for(;;) {
51ef6727 685 c = getopt(argc, argv, "f:O:B:s:hce6o:");
ea2384d3
FB
686 if (c == -1)
687 break;
688 switch(c) {
689 case 'h':
690 help();
691 break;
692 case 'f':
693 fmt = optarg;
694 break;
695 case 'O':
696 out_fmt = optarg;
697 break;
f58c7b35
TS
698 case 'B':
699 out_baseimg = optarg;
700 break;
ea2384d3 701 case 'c':
ec36ba14 702 flags |= BLOCK_FLAG_COMPRESS;
ea2384d3
FB
703 break;
704 case 'e':
ec36ba14
TS
705 flags |= BLOCK_FLAG_ENCRYPT;
706 break;
707 case '6':
708 flags |= BLOCK_FLAG_COMPAT6;
ea2384d3 709 break;
efa84d43
KW
710 case 'o':
711 options = optarg;
712 break;
51ef6727 713 case 's':
714 snapshot_name = optarg;
715 break;
ea2384d3
FB
716 }
717 }
3b46e624 718
926c2d23
AZ
719 bs_n = argc - optind - 1;
720 if (bs_n < 1) help();
721
722 out_filename = argv[argc - 1];
f58c7b35 723
4ac8aacd
JS
724 if (options && !strcmp(options, "?")) {
725 ret = print_block_option_help(out_filename, out_fmt);
726 goto out;
727 }
728
c2abccec 729 if (bs_n > 1 && out_baseimg) {
f58c7b35 730 error("-B makes no sense when concatenating multiple input images");
31ca34b8
JS
731 ret = -1;
732 goto out;
c2abccec 733 }
926c2d23 734
5bdf61fd 735 bs = qemu_mallocz(bs_n * sizeof(BlockDriverState *));
926c2d23
AZ
736
737 total_sectors = 0;
738 for (bs_i = 0; bs_i < bs_n; bs_i++) {
adfe078e 739 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS);
c2abccec 740 if (!bs[bs_i]) {
926c2d23 741 error("Could not open '%s'", argv[optind + bs_i]);
c2abccec
MK
742 ret = -1;
743 goto out;
744 }
926c2d23
AZ
745 bdrv_get_geometry(bs[bs_i], &bs_sectors);
746 total_sectors += bs_sectors;
747 }
ea2384d3 748
51ef6727 749 if (snapshot_name != NULL) {
750 if (bs_n > 1) {
751 error("No support for concatenating multiple snapshot\n");
752 ret = -1;
753 goto out;
754 }
755 if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) {
756 error("Failed to load snapshot\n");
757 ret = -1;
758 goto out;
759 }
760 }
761
efa84d43 762 /* Find driver and parse its options */
ea2384d3 763 drv = bdrv_find_format(out_fmt);
c2abccec 764 if (!drv) {
d34dda5e 765 error("Unknown file format '%s'", out_fmt);
c2abccec
MK
766 ret = -1;
767 goto out;
768 }
efa84d43 769
b50cbabc 770 proto_drv = bdrv_find_protocol(out_filename);
c2abccec 771 if (!proto_drv) {
b50cbabc 772 error("Unknown protocol '%s'", out_filename);
c2abccec
MK
773 ret = -1;
774 goto out;
775 }
b50cbabc
MK
776
777 create_options = append_option_parameters(create_options,
778 drv->create_options);
779 create_options = append_option_parameters(create_options,
780 proto_drv->create_options);
db08adf5 781
efa84d43 782 if (options) {
b50cbabc 783 param = parse_option_parameters(options, create_options, param);
efa84d43
KW
784 if (param == NULL) {
785 error("Invalid options for file format '%s'.", out_fmt);
c2abccec
MK
786 ret = -1;
787 goto out;
efa84d43
KW
788 }
789 } else {
b50cbabc 790 param = parse_option_parameters("", create_options, param);
efa84d43
KW
791 }
792
793 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
c2abccec
MK
794 ret = add_old_style_options(out_fmt, param, flags, out_baseimg, NULL);
795 if (ret < 0) {
796 goto out;
797 }
efa84d43 798
a18953fb
KW
799 /* Get backing file name if -o backing_file was used */
800 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
801 if (out_baseimg_param) {
802 out_baseimg = out_baseimg_param->value.s;
803 }
804
efa84d43
KW
805 /* Check if compression is supported */
806 if (flags & BLOCK_FLAG_COMPRESS) {
807 QEMUOptionParameter *encryption =
808 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
809
810 if (!drv->bdrv_write_compressed) {
811 error("Compression not supported for this file format");
c2abccec
MK
812 ret = -1;
813 goto out;
efa84d43
KW
814 }
815
816 if (encryption && encryption->value.n) {
817 error("Compression and encryption not supported at the same time");
c2abccec
MK
818 ret = -1;
819 goto out;
efa84d43
KW
820 }
821 }
822
823 /* Create the new image */
824 ret = bdrv_create(drv, out_filename, param);
ea2384d3
FB
825 if (ret < 0) {
826 if (ret == -ENOTSUP) {
93c65b47 827 error("Formatting not supported for file format '%s'", out_fmt);
6e9ea0c0
AJ
828 } else if (ret == -EFBIG) {
829 error("The image size is too large for file format '%s'", out_fmt);
ea2384d3 830 } else {
3e7896de 831 error("%s: error while converting %s: %s", out_filename, out_fmt, strerror(-ret));
ea2384d3 832 }
c2abccec 833 goto out;
ea2384d3 834 }
3b46e624 835
1bd8e175
KW
836 out_bs = bdrv_new_open(out_filename, out_fmt,
837 BDRV_O_FLAGS | BDRV_O_RDWR | BDRV_O_NO_FLUSH);
c2abccec
MK
838 if (!out_bs) {
839 ret = -1;
840 goto out;
841 }
ea2384d3 842
926c2d23
AZ
843 bs_i = 0;
844 bs_offset = 0;
845 bdrv_get_geometry(bs[0], &bs_sectors);
d6771bfa 846 buf = qemu_malloc(IO_BUF_SIZE);
926c2d23
AZ
847
848 if (flags & BLOCK_FLAG_COMPRESS) {
c2abccec
MK
849 ret = bdrv_get_info(out_bs, &bdi);
850 if (ret < 0) {
faea38e7 851 error("could not get block driver info");
c2abccec
MK
852 goto out;
853 }
faea38e7 854 cluster_size = bdi.cluster_size;
c2abccec 855 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
ea2384d3 856 error("invalid cluster size");
c2abccec
MK
857 ret = -1;
858 goto out;
859 }
ea2384d3
FB
860 cluster_sectors = cluster_size >> 9;
861 sector_num = 0;
862 for(;;) {
926c2d23
AZ
863 int64_t bs_num;
864 int remainder;
865 uint8_t *buf2;
866
ea2384d3
FB
867 nb_sectors = total_sectors - sector_num;
868 if (nb_sectors <= 0)
869 break;
870 if (nb_sectors >= cluster_sectors)
871 n = cluster_sectors;
872 else
873 n = nb_sectors;
926c2d23
AZ
874
875 bs_num = sector_num - bs_offset;
876 assert (bs_num >= 0);
877 remainder = n;
878 buf2 = buf;
879 while (remainder > 0) {
880 int nlow;
881 while (bs_num == bs_sectors) {
882 bs_i++;
883 assert (bs_i < bs_n);
884 bs_offset += bs_sectors;
885 bdrv_get_geometry(bs[bs_i], &bs_sectors);
886 bs_num = 0;
0bfcd599
BS
887 /* printf("changing part: sector_num=%" PRId64 ", "
888 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
889 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
926c2d23
AZ
890 }
891 assert (bs_num < bs_sectors);
892
893 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
894
c2abccec
MK
895 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
896 if (ret < 0) {
926c2d23 897 error("error while reading");
c2abccec
MK
898 goto out;
899 }
926c2d23
AZ
900
901 buf2 += nlow * 512;
902 bs_num += nlow;
903
904 remainder -= nlow;
905 }
906 assert (remainder == 0);
907
ea2384d3
FB
908 if (n < cluster_sectors)
909 memset(buf + n * 512, 0, cluster_size - n * 512);
910 if (is_not_zero(buf, cluster_size)) {
c2abccec
MK
911 ret = bdrv_write_compressed(out_bs, sector_num, buf,
912 cluster_sectors);
913 if (ret != 0) {
ec3757de
FB
914 error("error while compressing sector %" PRId64,
915 sector_num);
c2abccec
MK
916 goto out;
917 }
ea2384d3
FB
918 }
919 sector_num += n;
920 }
faea38e7
FB
921 /* signal EOF to align */
922 bdrv_write_compressed(out_bs, 0, NULL, 0);
ea2384d3 923 } else {
f2feebbd
KW
924 int has_zero_init = bdrv_has_zero_init(out_bs);
925
f58c7b35 926 sector_num = 0; // total number of sectors converted so far
ea2384d3
FB
927 for(;;) {
928 nb_sectors = total_sectors - sector_num;
929 if (nb_sectors <= 0)
930 break;
931 if (nb_sectors >= (IO_BUF_SIZE / 512))
932 n = (IO_BUF_SIZE / 512);
933 else
934 n = nb_sectors;
926c2d23
AZ
935
936 while (sector_num - bs_offset >= bs_sectors) {
937 bs_i ++;
938 assert (bs_i < bs_n);
939 bs_offset += bs_sectors;
940 bdrv_get_geometry(bs[bs_i], &bs_sectors);
0bfcd599
BS
941 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
942 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
926c2d23
AZ
943 sector_num, bs_i, bs_offset, bs_sectors); */
944 }
945
946 if (n > bs_offset + bs_sectors - sector_num)
947 n = bs_offset + bs_sectors - sector_num;
948
f2feebbd 949 if (has_zero_init) {
d032044f
AS
950 /* If the output image is being created as a copy on write image,
951 assume that sectors which are unallocated in the input image
952 are present in both the output's and input's base images (no
953 need to copy them). */
954 if (out_baseimg) {
955 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
956 n, &n1)) {
957 sector_num += n1;
958 continue;
959 }
960 /* The next 'n1' sectors are allocated in the input image. Copy
961 only those as they may be followed by unallocated sectors. */
962 n = n1;
93c65b47 963 }
93c65b47
AL
964 } else {
965 n1 = n;
f58c7b35
TS
966 }
967
c2abccec
MK
968 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
969 if (ret < 0) {
ea2384d3 970 error("error while reading");
c2abccec
MK
971 goto out;
972 }
ea2384d3
FB
973 /* NOTE: at the same time we convert, we do not write zero
974 sectors to have a chance to compress the image. Ideally, we
975 should add a specific call to have the info to go faster */
976 buf1 = buf;
977 while (n > 0) {
f58c7b35
TS
978 /* If the output image is being created as a copy on write image,
979 copy all sectors even the ones containing only NUL bytes,
93c65b47
AL
980 because they may differ from the sectors in the base image.
981
982 If the output is to a host device, we also write out
983 sectors that are entirely 0, since whatever data was
984 already there is garbage, not 0s. */
f2feebbd 985 if (!has_zero_init || out_baseimg ||
93c65b47 986 is_allocated_sectors(buf1, n, &n1)) {
c2abccec
MK
987 ret = bdrv_write(out_bs, sector_num, buf1, n1);
988 if (ret < 0) {
ea2384d3 989 error("error while writing");
c2abccec
MK
990 goto out;
991 }
ea2384d3
FB
992 }
993 sector_num += n1;
994 n -= n1;
995 buf1 += n1 * 512;
996 }
997 }
998 }
c2abccec
MK
999out:
1000 free_option_parameters(create_options);
1001 free_option_parameters(param);
d6771bfa 1002 qemu_free(buf);
c2abccec
MK
1003 if (out_bs) {
1004 bdrv_delete(out_bs);
1005 }
31ca34b8
JS
1006 if (bs) {
1007 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1008 if (bs[bs_i]) {
1009 bdrv_delete(bs[bs_i]);
1010 }
c2abccec 1011 }
31ca34b8 1012 qemu_free(bs);
c2abccec 1013 }
c2abccec
MK
1014 if (ret) {
1015 return 1;
1016 }
ea2384d3
FB
1017 return 0;
1018}
1019
57d1a2b6
FB
1020#ifdef _WIN32
1021static int64_t get_allocated_file_size(const char *filename)
1022{
e8445331
FB
1023 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
1024 get_compressed_t get_compressed;
57d1a2b6 1025 struct _stati64 st;
e8445331
FB
1026
1027 /* WinNT support GetCompressedFileSize to determine allocate size */
1028 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
1029 if (get_compressed) {
1030 DWORD high, low;
1031 low = get_compressed(filename, &high);
1032 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
1033 return (((int64_t) high) << 32) + low;
1034 }
1035
5fafdf24 1036 if (_stati64(filename, &st) < 0)
57d1a2b6
FB
1037 return -1;
1038 return st.st_size;
1039}
1040#else
1041static int64_t get_allocated_file_size(const char *filename)
1042{
1043 struct stat st;
5fafdf24 1044 if (stat(filename, &st) < 0)
57d1a2b6
FB
1045 return -1;
1046 return (int64_t)st.st_blocks * 512;
1047}
1048#endif
1049
faea38e7
FB
1050static void dump_snapshots(BlockDriverState *bs)
1051{
1052 QEMUSnapshotInfo *sn_tab, *sn;
1053 int nb_sns, i;
1054 char buf[256];
1055
1056 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1057 if (nb_sns <= 0)
1058 return;
1059 printf("Snapshot list:\n");
1060 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1061 for(i = 0; i < nb_sns; i++) {
1062 sn = &sn_tab[i];
1063 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1064 }
1065 qemu_free(sn_tab);
1066}
1067
ea2384d3
FB
1068static int img_info(int argc, char **argv)
1069{
1070 int c;
1071 const char *filename, *fmt;
ea2384d3
FB
1072 BlockDriverState *bs;
1073 char fmt_name[128], size_buf[128], dsize_buf[128];
96b8f136
TS
1074 uint64_t total_sectors;
1075 int64_t allocated_size;
93b6b2a3
FB
1076 char backing_filename[1024];
1077 char backing_filename2[1024];
faea38e7 1078 BlockDriverInfo bdi;
ea2384d3
FB
1079
1080 fmt = NULL;
1081 for(;;) {
1082 c = getopt(argc, argv, "f:h");
1083 if (c == -1)
1084 break;
1085 switch(c) {
1086 case 'h':
1087 help();
1088 break;
1089 case 'f':
1090 fmt = optarg;
1091 break;
1092 }
1093 }
5fafdf24 1094 if (optind >= argc)
ea2384d3
FB
1095 help();
1096 filename = argv[optind++];
1097
adfe078e 1098 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING);
c2abccec
MK
1099 if (!bs) {
1100 return 1;
1101 }
ea2384d3
FB
1102 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
1103 bdrv_get_geometry(bs, &total_sectors);
1104 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
57d1a2b6
FB
1105 allocated_size = get_allocated_file_size(filename);
1106 if (allocated_size < 0)
a10ea30b 1107 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
de167e41 1108 else
5fafdf24 1109 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
de167e41 1110 allocated_size);
ea2384d3
FB
1111 printf("image: %s\n"
1112 "file format: %s\n"
ec3757de 1113 "virtual size: %s (%" PRId64 " bytes)\n"
ea2384d3 1114 "disk size: %s\n",
5fafdf24 1115 filename, fmt_name, size_buf,
ec3757de 1116 (total_sectors * 512),
ea2384d3
FB
1117 dsize_buf);
1118 if (bdrv_is_encrypted(bs))
1119 printf("encrypted: yes\n");
faea38e7 1120 if (bdrv_get_info(bs, &bdi) >= 0) {
5fafdf24 1121 if (bdi.cluster_size != 0)
faea38e7
FB
1122 printf("cluster_size: %d\n", bdi.cluster_size);
1123 }
93b6b2a3 1124 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
faea38e7 1125 if (backing_filename[0] != '\0') {
93b6b2a3
FB
1126 path_combine(backing_filename2, sizeof(backing_filename2),
1127 filename, backing_filename);
5fafdf24 1128 printf("backing file: %s (actual path: %s)\n",
93b6b2a3
FB
1129 backing_filename,
1130 backing_filename2);
faea38e7
FB
1131 }
1132 dump_snapshots(bs);
ea2384d3
FB
1133 bdrv_delete(bs);
1134 return 0;
1135}
1136
f7b4a940
AL
1137#define SNAPSHOT_LIST 1
1138#define SNAPSHOT_CREATE 2
1139#define SNAPSHOT_APPLY 3
1140#define SNAPSHOT_DELETE 4
1141
153859be 1142static int img_snapshot(int argc, char **argv)
f7b4a940
AL
1143{
1144 BlockDriverState *bs;
1145 QEMUSnapshotInfo sn;
1146 char *filename, *snapshot_name = NULL;
c2abccec 1147 int c, ret = 0, bdrv_oflags;
f7b4a940
AL
1148 int action = 0;
1149 qemu_timeval tv;
1150
f5edb014 1151 bdrv_oflags = BDRV_O_RDWR;
f7b4a940
AL
1152 /* Parse commandline parameters */
1153 for(;;) {
1154 c = getopt(argc, argv, "la:c:d:h");
1155 if (c == -1)
1156 break;
1157 switch(c) {
1158 case 'h':
1159 help();
153859be 1160 return 0;
f7b4a940
AL
1161 case 'l':
1162 if (action) {
1163 help();
153859be 1164 return 0;
f7b4a940
AL
1165 }
1166 action = SNAPSHOT_LIST;
f5edb014 1167 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
f7b4a940
AL
1168 break;
1169 case 'a':
1170 if (action) {
1171 help();
153859be 1172 return 0;
f7b4a940
AL
1173 }
1174 action = SNAPSHOT_APPLY;
1175 snapshot_name = optarg;
1176 break;
1177 case 'c':
1178 if (action) {
1179 help();
153859be 1180 return 0;
f7b4a940
AL
1181 }
1182 action = SNAPSHOT_CREATE;
1183 snapshot_name = optarg;
1184 break;
1185 case 'd':
1186 if (action) {
1187 help();
153859be 1188 return 0;
f7b4a940
AL
1189 }
1190 action = SNAPSHOT_DELETE;
1191 snapshot_name = optarg;
1192 break;
1193 }
1194 }
1195
1196 if (optind >= argc)
1197 help();
1198 filename = argv[optind++];
1199
1200 /* Open the image */
f163d073 1201 bs = bdrv_new_open(filename, NULL, bdrv_oflags);
c2abccec
MK
1202 if (!bs) {
1203 return 1;
1204 }
f7b4a940
AL
1205
1206 /* Perform the requested action */
1207 switch(action) {
1208 case SNAPSHOT_LIST:
1209 dump_snapshots(bs);
1210 break;
1211
1212 case SNAPSHOT_CREATE:
1213 memset(&sn, 0, sizeof(sn));
1214 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1215
1216 qemu_gettimeofday(&tv);
1217 sn.date_sec = tv.tv_sec;
1218 sn.date_nsec = tv.tv_usec * 1000;
1219
1220 ret = bdrv_snapshot_create(bs, &sn);
1221 if (ret)
1222 error("Could not create snapshot '%s': %d (%s)",
1223 snapshot_name, ret, strerror(-ret));
1224 break;
1225
1226 case SNAPSHOT_APPLY:
1227 ret = bdrv_snapshot_goto(bs, snapshot_name);
1228 if (ret)
1229 error("Could not apply snapshot '%s': %d (%s)",
1230 snapshot_name, ret, strerror(-ret));
1231 break;
1232
1233 case SNAPSHOT_DELETE:
1234 ret = bdrv_snapshot_delete(bs, snapshot_name);
1235 if (ret)
1236 error("Could not delete snapshot '%s': %d (%s)",
1237 snapshot_name, ret, strerror(-ret));
1238 break;
1239 }
1240
1241 /* Cleanup */
1242 bdrv_delete(bs);
c2abccec
MK
1243 if (ret) {
1244 return 1;
1245 }
153859be 1246 return 0;
f7b4a940
AL
1247}
1248
3e85c6fd
KW
1249static int img_rebase(int argc, char **argv)
1250{
c2abccec 1251 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
f163d073 1252 BlockDriver *old_backing_drv, *new_backing_drv;
3e85c6fd 1253 char *filename;
e53dbee0 1254 const char *fmt, *out_basefmt, *out_baseimg;
3e85c6fd
KW
1255 int c, flags, ret;
1256 int unsafe = 0;
1257
1258 /* Parse commandline parameters */
e53dbee0 1259 fmt = NULL;
3e85c6fd
KW
1260 out_baseimg = NULL;
1261 out_basefmt = NULL;
1262
1263 for(;;) {
e53dbee0 1264 c = getopt(argc, argv, "uhf:F:b:");
3e85c6fd
KW
1265 if (c == -1)
1266 break;
1267 switch(c) {
1268 case 'h':
1269 help();
1270 return 0;
e53dbee0
KW
1271 case 'f':
1272 fmt = optarg;
1273 break;
3e85c6fd
KW
1274 case 'F':
1275 out_basefmt = optarg;
1276 break;
1277 case 'b':
1278 out_baseimg = optarg;
1279 break;
1280 case 'u':
1281 unsafe = 1;
1282 break;
1283 }
1284 }
1285
1286 if ((optind >= argc) || !out_baseimg)
1287 help();
1288 filename = argv[optind++];
1289
1290 /*
1291 * Open the images.
1292 *
1293 * Ignore the old backing file for unsafe rebase in case we want to correct
1294 * the reference to a renamed or moved backing file.
1295 */
adfe078e 1296 flags = BDRV_O_FLAGS | BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
f163d073 1297 bs = bdrv_new_open(filename, fmt, flags);
c2abccec
MK
1298 if (!bs) {
1299 return 1;
1300 }
3e85c6fd
KW
1301
1302 /* Find the right drivers for the backing files */
1303 old_backing_drv = NULL;
1304 new_backing_drv = NULL;
1305
1306 if (!unsafe && bs->backing_format[0] != '\0') {
1307 old_backing_drv = bdrv_find_format(bs->backing_format);
1308 if (old_backing_drv == NULL) {
1309 error("Invalid format name: '%s'", bs->backing_format);
c2abccec
MK
1310 ret = -1;
1311 goto out;
3e85c6fd
KW
1312 }
1313 }
1314
1315 if (out_basefmt != NULL) {
1316 new_backing_drv = bdrv_find_format(out_basefmt);
1317 if (new_backing_drv == NULL) {
1318 error("Invalid format name: '%s'", out_basefmt);
c2abccec
MK
1319 ret = -1;
1320 goto out;
3e85c6fd
KW
1321 }
1322 }
1323
1324 /* For safe rebasing we need to compare old and new backing file */
1325 if (unsafe) {
1326 /* Make the compiler happy */
1327 bs_old_backing = NULL;
1328 bs_new_backing = NULL;
1329 } else {
1330 char backing_name[1024];
1331
1332 bs_old_backing = bdrv_new("old_backing");
1333 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
c2abccec
MK
1334 ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS,
1335 old_backing_drv);
1336 if (ret) {
3e85c6fd 1337 error("Could not open old backing file '%s'", backing_name);
c2abccec 1338 goto out;
3e85c6fd
KW
1339 }
1340
1341 bs_new_backing = bdrv_new("new_backing");
cdbae851 1342 ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS,
c2abccec
MK
1343 new_backing_drv);
1344 if (ret) {
584771e6 1345 error("Could not open new backing file '%s'", out_baseimg);
c2abccec 1346 goto out;
3e85c6fd
KW
1347 }
1348 }
1349
1350 /*
1351 * Check each unallocated cluster in the COW file. If it is unallocated,
1352 * accesses go to the backing file. We must therefore compare this cluster
1353 * in the old and new backing file, and if they differ we need to copy it
1354 * from the old backing file into the COW file.
1355 *
1356 * If qemu-img crashes during this step, no harm is done. The content of
1357 * the image is the same as the original one at any time.
1358 */
1359 if (!unsafe) {
1360 uint64_t num_sectors;
1361 uint64_t sector;
cc60e327 1362 int n;
d6771bfa
T
1363 uint8_t * buf_old;
1364 uint8_t * buf_new;
1365
1366 buf_old = qemu_malloc(IO_BUF_SIZE);
1367 buf_new = qemu_malloc(IO_BUF_SIZE);
3e85c6fd
KW
1368
1369 bdrv_get_geometry(bs, &num_sectors);
1370
1371 for (sector = 0; sector < num_sectors; sector += n) {
1372
1373 /* How many sectors can we handle with the next read? */
1374 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
1375 n = (IO_BUF_SIZE / 512);
1376 } else {
1377 n = num_sectors - sector;
1378 }
1379
1380 /* If the cluster is allocated, we don't need to take action */
cc60e327
KW
1381 ret = bdrv_is_allocated(bs, sector, n, &n);
1382 if (ret) {
3e85c6fd
KW
1383 continue;
1384 }
1385
1386 /* Read old and new backing file */
c2abccec
MK
1387 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
1388 if (ret < 0) {
3e85c6fd 1389 error("error while reading from old backing file");
c2abccec 1390 goto out;
3e85c6fd 1391 }
c2abccec
MK
1392 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
1393 if (ret < 0) {
3e85c6fd 1394 error("error while reading from new backing file");
c2abccec 1395 goto out;
3e85c6fd
KW
1396 }
1397
1398 /* If they differ, we need to write to the COW file */
1399 uint64_t written = 0;
1400
1401 while (written < n) {
1402 int pnum;
1403
1404 if (compare_sectors(buf_old + written * 512,
60b1bd4f 1405 buf_new + written * 512, n - written, &pnum))
3e85c6fd
KW
1406 {
1407 ret = bdrv_write(bs, sector + written,
1408 buf_old + written * 512, pnum);
1409 if (ret < 0) {
1410 error("Error while writing to COW image: %s",
1411 strerror(-ret));
c2abccec 1412 goto out;
3e85c6fd
KW
1413 }
1414 }
1415
1416 written += pnum;
1417 }
1418 }
d6771bfa
T
1419
1420 qemu_free(buf_old);
1421 qemu_free(buf_new);
3e85c6fd
KW
1422 }
1423
1424 /*
1425 * Change the backing file. All clusters that are different from the old
1426 * backing file are overwritten in the COW file now, so the visible content
1427 * doesn't change when we switch the backing file.
1428 */
1429 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
1430 if (ret == -ENOSPC) {
1431 error("Could not change the backing file to '%s': No space left in "
1432 "the file header", out_baseimg);
1433 } else if (ret < 0) {
1434 error("Could not change the backing file to '%s': %s",
1435 out_baseimg, strerror(-ret));
1436 }
1437
1438 /*
1439 * TODO At this point it is possible to check if any clusters that are
1440 * allocated in the COW file are the same in the backing file. If so, they
1441 * could be dropped from the COW file. Don't do this before switching the
1442 * backing file, in case of a crash this would lead to corruption.
1443 */
c2abccec 1444out:
3e85c6fd
KW
1445 /* Cleanup */
1446 if (!unsafe) {
1447 bdrv_delete(bs_old_backing);
1448 bdrv_delete(bs_new_backing);
1449 }
1450
1451 bdrv_delete(bs);
c2abccec
MK
1452 if (ret) {
1453 return 1;
1454 }
3e85c6fd
KW
1455 return 0;
1456}
1457
ae6b0ed6
SH
1458static int img_resize(int argc, char **argv)
1459{
1460 int c, ret, relative;
1461 const char *filename, *fmt, *size;
1462 int64_t n, total_size;
1463 BlockDriverState *bs;
1464 QEMUOptionParameter *param;
1465 QEMUOptionParameter resize_options[] = {
1466 {
1467 .name = BLOCK_OPT_SIZE,
1468 .type = OPT_SIZE,
1469 .help = "Virtual disk size"
1470 },
1471 { NULL }
1472 };
1473
1474 fmt = NULL;
1475 for(;;) {
1476 c = getopt(argc, argv, "f:h");
1477 if (c == -1) {
1478 break;
1479 }
1480 switch(c) {
1481 case 'h':
1482 help();
1483 break;
1484 case 'f':
1485 fmt = optarg;
1486 break;
1487 }
1488 }
1489 if (optind + 1 >= argc) {
1490 help();
1491 }
1492 filename = argv[optind++];
1493 size = argv[optind++];
1494
1495 /* Choose grow, shrink, or absolute resize mode */
1496 switch (size[0]) {
1497 case '+':
1498 relative = 1;
1499 size++;
1500 break;
1501 case '-':
1502 relative = -1;
1503 size++;
1504 break;
1505 default:
1506 relative = 0;
1507 break;
1508 }
1509
1510 /* Parse size */
1511 param = parse_option_parameters("", resize_options, NULL);
1512 if (set_option_parameter(param, BLOCK_OPT_SIZE, size)) {
1513 /* Error message already printed when size parsing fails */
1514 exit(1);
1515 }
1516 n = get_option_parameter(param, BLOCK_OPT_SIZE)->value.n;
1517 free_option_parameters(param);
1518
1519 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR);
c2abccec
MK
1520 if (!bs) {
1521 return 1;
1522 }
ae6b0ed6
SH
1523
1524 if (relative) {
1525 total_size = bdrv_getlength(bs) + n * relative;
1526 } else {
1527 total_size = n;
1528 }
1529 if (total_size <= 0) {
1530 error("New image size must be positive");
c2abccec
MK
1531 ret = -1;
1532 goto out;
ae6b0ed6
SH
1533 }
1534
1535 ret = bdrv_truncate(bs, total_size);
1536 switch (ret) {
1537 case 0:
1538 printf("Image resized.\n");
1539 break;
1540 case -ENOTSUP:
1541 error("This image format does not support resize");
1542 break;
1543 case -EACCES:
1544 error("Image is read-only");
1545 break;
1546 default:
1547 error("Error resizing image (%d)", -ret);
1548 break;
1549 }
c2abccec 1550out:
ae6b0ed6 1551 bdrv_delete(bs);
c2abccec
MK
1552 if (ret) {
1553 return 1;
1554 }
ae6b0ed6
SH
1555 return 0;
1556}
1557
c227f099 1558static const img_cmd_t img_cmds[] = {
153859be
SB
1559#define DEF(option, callback, arg_string) \
1560 { option, callback },
1561#include "qemu-img-cmds.h"
1562#undef DEF
1563#undef GEN_DOCS
1564 { NULL, NULL, },
1565};
1566
ea2384d3
FB
1567int main(int argc, char **argv)
1568{
c227f099 1569 const img_cmd_t *cmd;
153859be 1570 const char *cmdname;
ea2384d3
FB
1571
1572 bdrv_init();
1573 if (argc < 2)
1574 help();
153859be 1575 cmdname = argv[1];
8f9b157e 1576 argc--; argv++;
153859be
SB
1577
1578 /* find the command */
1579 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
1580 if (!strcmp(cmdname, cmd->name)) {
1581 return cmd->handler(argc, argv);
1582 }
ea2384d3 1583 }
153859be
SB
1584
1585 /* not found */
1586 help();
ea2384d3
FB
1587 return 0;
1588}