2 * QEMU disk image utility
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qapi-visit.h"
25 #include "qapi/qmp-output-visitor.h"
26 #include "qapi/qmp/qjson.h"
27 #include "qemu-common.h"
28 #include "qemu/option.h"
29 #include "qemu/error-report.h"
30 #include "qemu/osdep.h"
31 #include "sysemu/sysemu.h"
32 #include "block/block_int.h"
33 #include "block/qapi.h"
36 typedef struct img_cmd_t {
38 int (*handler)(int argc, char **argv);
43 OPTION_BACKING_CHAIN = 257,
46 typedef enum OutputFormat {
51 /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
52 #define BDRV_O_FLAGS BDRV_O_CACHE_WB
53 #define BDRV_DEFAULT_CACHE "writeback"
55 static void format_print(void *opaque, const char *name)
60 /* Please keep in synch with qemu-img.texi */
61 static void help(void)
63 const char *help_msg =
64 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
65 "usage: qemu-img command [command options]\n"
66 "QEMU disk image utility\n"
69 #define DEF(option, callback, arg_string) \
71 #include "qemu-img-cmds.h"
75 "Command parameters:\n"
76 " 'filename' is a disk image filename\n"
77 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
78 " 'cache' is the cache mode used to write the output disk image, the valid\n"
79 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
80 " 'directsync' and 'unsafe' (default for convert)\n"
81 " 'size' is the disk image size in bytes. Optional suffixes\n"
82 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
83 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
84 " supported. 'b' is ignored.\n"
85 " 'output_filename' is the destination disk image filename\n"
86 " 'output_fmt' is the destination format\n"
87 " 'options' is a comma separated list of format specific options in a\n"
88 " name=value format. Use -o ? for an overview of the options supported by the\n"
90 " 'snapshot_param' is param used for internal snapshot, format\n"
91 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
93 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
95 " '-c' indicates that target image must be compressed (qcow format only)\n"
96 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
97 " match exactly. The image doesn't need a working backing file before\n"
98 " rebasing in this case (useful for renaming the backing file)\n"
99 " '-h' with or without a command shows this help and lists the supported formats\n"
100 " '-p' show progress of command (only certain commands)\n"
101 " '-q' use Quiet mode - do not print any output (except errors)\n"
102 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
103 " contain only zeros for qemu-img to create a sparse image during\n"
104 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
105 " unallocated or zero sectors, and the destination image will always be\n"
107 " '--output' takes the format in which the output must be done (human or json)\n"
108 " '-n' skips the target volume creation (useful if the volume is created\n"
109 " prior to running qemu-img)\n"
111 "Parameters to check subcommand:\n"
112 " '-r' tries to repair any inconsistencies that are found during the check.\n"
113 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
114 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
115 " hiding corruption that has already occurred.\n"
117 "Parameters to snapshot subcommand:\n"
118 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
119 " '-a' applies a snapshot (revert disk to saved state)\n"
120 " '-c' creates a snapshot\n"
121 " '-d' deletes a snapshot\n"
122 " '-l' lists all snapshots in the given image\n"
124 "Parameters to compare subcommand:\n"
125 " '-f' first image format\n"
126 " '-F' second image format\n"
127 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
129 printf("%s\nSupported formats:", help_msg);
130 bdrv_iterate_format(format_print, NULL);
135 static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
141 ret = vprintf(fmt, args);
148 /* XXX: put correct support for win32 */
149 static int read_password(char *buf, int buf_size)
152 printf("Password: ");
159 if (i < (buf_size - 1))
170 static struct termios oldtty;
172 static void term_exit(void)
174 tcsetattr (0, TCSANOW, &oldtty);
177 static void term_init(void)
184 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
185 |INLCR|IGNCR|ICRNL|IXON);
186 tty.c_oflag |= OPOST;
187 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
188 tty.c_cflag &= ~(CSIZE|PARENB);
193 tcsetattr (0, TCSANOW, &tty);
198 static int read_password(char *buf, int buf_size)
203 printf("password: ");
208 ret = read(0, &ch, 1);
210 if (errno == EAGAIN || errno == EINTR) {
216 } else if (ret == 0) {
224 if (i < (buf_size - 1))
235 static int print_block_option_help(const char *filename, const char *fmt)
237 BlockDriver *drv, *proto_drv;
238 QEMUOptionParameter *create_options = NULL;
240 /* Find driver and parse its options */
241 drv = bdrv_find_format(fmt);
243 error_report("Unknown file format '%s'", fmt);
247 create_options = append_option_parameters(create_options,
248 drv->create_options);
251 proto_drv = bdrv_find_protocol(filename, true);
253 error_report("Unknown protocol '%s'", filename);
256 create_options = append_option_parameters(create_options,
257 proto_drv->create_options);
260 print_option_help(create_options);
261 free_option_parameters(create_options);
265 static BlockDriverState *bdrv_new_open(const char *filename,
271 BlockDriverState *bs;
274 Error *local_err = NULL;
277 bs = bdrv_new("image");
280 drv = bdrv_find_format(fmt);
282 error_report("Unknown file format '%s'", fmt);
289 ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
291 error_report("Could not open '%s': %s", filename,
292 error_get_pretty(local_err));
293 error_free(local_err);
297 if (bdrv_is_encrypted(bs) && require_io) {
298 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
299 if (read_password(password, sizeof(password)) < 0) {
300 error_report("No password given");
303 if (bdrv_set_key(bs, password) < 0) {
304 error_report("invalid password");
314 static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
315 const char *base_filename,
316 const char *base_fmt)
319 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
320 error_report("Backing file not supported for file format '%s'",
326 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
327 error_report("Backing file format not supported for file "
335 static int img_create(int argc, char **argv)
338 uint64_t img_size = -1;
339 const char *fmt = "raw";
340 const char *base_fmt = NULL;
341 const char *filename;
342 const char *base_filename = NULL;
343 char *options = NULL;
344 Error *local_err = NULL;
348 c = getopt(argc, argv, "F:b:f:he6o:q");
361 base_filename = optarg;
367 error_report("option -e is deprecated, please use \'-o "
368 "encryption\' instead!");
371 error_report("option -6 is deprecated, please use \'-o "
372 "compat6\' instead!");
375 if (!is_valid_option_list(optarg)) {
376 error_report("Invalid option list: %s", optarg);
380 options = g_strdup(optarg);
382 char *old_options = options;
383 options = g_strdup_printf("%s,%s", options, optarg);
393 /* Get the filename */
394 filename = (optind < argc) ? argv[optind] : NULL;
395 if (options && has_help_option(options)) {
397 return print_block_option_help(filename, fmt);
400 if (optind >= argc) {
405 /* Get image size, if specified */
409 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
410 if (sval < 0 || *end) {
411 if (sval == -ERANGE) {
412 error_report("Image size must be less than 8 EiB!");
414 error_report("Invalid image size specified! You may use k, M, "
415 "G, T, P or E suffixes for ");
416 error_report("kilobytes, megabytes, gigabytes, terabytes, "
417 "petabytes and exabytes.");
421 img_size = (uint64_t)sval;
423 if (optind != argc) {
427 bdrv_img_create(filename, fmt, base_filename, base_fmt,
428 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
430 error_report("%s: %s", filename, error_get_pretty(local_err));
431 error_free(local_err);
443 static void dump_json_image_check(ImageCheck *check, bool quiet)
447 QmpOutputVisitor *ov = qmp_output_visitor_new();
449 visit_type_ImageCheck(qmp_output_get_visitor(ov),
450 &check, NULL, &errp);
451 obj = qmp_output_get_qobject(ov);
452 str = qobject_to_json_pretty(obj);
454 qprintf(quiet, "%s\n", qstring_get_str(str));
456 qmp_output_visitor_cleanup(ov);
460 static void dump_human_image_check(ImageCheck *check, bool quiet)
462 if (!(check->corruptions || check->leaks || check->check_errors)) {
463 qprintf(quiet, "No errors were found on the image.\n");
465 if (check->corruptions) {
466 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
467 "Data may be corrupted, or further writes to the image "
474 "\n%" PRId64 " leaked clusters were found on the image.\n"
475 "This means waste of disk space, but no harm to data.\n",
479 if (check->check_errors) {
482 " internal errors have occurred during the check.\n",
483 check->check_errors);
487 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
488 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
489 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
490 check->allocated_clusters, check->total_clusters,
491 check->allocated_clusters * 100.0 / check->total_clusters,
492 check->fragmented_clusters * 100.0 / check->allocated_clusters,
493 check->compressed_clusters * 100.0 /
494 check->allocated_clusters);
497 if (check->image_end_offset) {
499 "Image end offset: %" PRId64 "\n", check->image_end_offset);
503 static int collect_image_check(BlockDriverState *bs,
505 const char *filename,
510 BdrvCheckResult result;
512 ret = bdrv_check(bs, &result, fix);
517 check->filename = g_strdup(filename);
518 check->format = g_strdup(bdrv_get_format_name(bs));
519 check->check_errors = result.check_errors;
520 check->corruptions = result.corruptions;
521 check->has_corruptions = result.corruptions != 0;
522 check->leaks = result.leaks;
523 check->has_leaks = result.leaks != 0;
524 check->corruptions_fixed = result.corruptions_fixed;
525 check->has_corruptions_fixed = result.corruptions != 0;
526 check->leaks_fixed = result.leaks_fixed;
527 check->has_leaks_fixed = result.leaks != 0;
528 check->image_end_offset = result.image_end_offset;
529 check->has_image_end_offset = result.image_end_offset != 0;
530 check->total_clusters = result.bfi.total_clusters;
531 check->has_total_clusters = result.bfi.total_clusters != 0;
532 check->allocated_clusters = result.bfi.allocated_clusters;
533 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
534 check->fragmented_clusters = result.bfi.fragmented_clusters;
535 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
536 check->compressed_clusters = result.bfi.compressed_clusters;
537 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
543 * Checks an image for consistency. Exit codes:
545 * 0 - Check completed, image is good
546 * 1 - Check not completed because of internal errors
547 * 2 - Check completed, image is corrupted
548 * 3 - Check completed, image has leaked clusters, but is good otherwise
550 static int img_check(int argc, char **argv)
553 OutputFormat output_format = OFORMAT_HUMAN;
554 const char *filename, *fmt, *output;
555 BlockDriverState *bs;
557 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
564 int option_index = 0;
565 static const struct option long_options[] = {
566 {"help", no_argument, 0, 'h'},
567 {"format", required_argument, 0, 'f'},
568 {"repair", no_argument, 0, 'r'},
569 {"output", required_argument, 0, OPTION_OUTPUT},
572 c = getopt_long(argc, argv, "f:hr:q",
573 long_options, &option_index);
586 flags |= BDRV_O_RDWR;
588 if (!strcmp(optarg, "leaks")) {
589 fix = BDRV_FIX_LEAKS;
590 } else if (!strcmp(optarg, "all")) {
591 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
604 if (optind != argc - 1) {
607 filename = argv[optind++];
609 if (output && !strcmp(output, "json")) {
610 output_format = OFORMAT_JSON;
611 } else if (output && !strcmp(output, "human")) {
612 output_format = OFORMAT_HUMAN;
614 error_report("--output must be used with human or json as argument.");
618 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
623 check = g_new0(ImageCheck, 1);
624 ret = collect_image_check(bs, check, filename, fmt, fix);
626 if (ret == -ENOTSUP) {
627 if (output_format == OFORMAT_HUMAN) {
628 error_report("This image format does not support checks");
634 if (check->corruptions_fixed || check->leaks_fixed) {
635 int corruptions_fixed, leaks_fixed;
637 leaks_fixed = check->leaks_fixed;
638 corruptions_fixed = check->corruptions_fixed;
640 if (output_format == OFORMAT_HUMAN) {
642 "The following inconsistencies were found and repaired:\n\n"
643 " %" PRId64 " leaked clusters\n"
644 " %" PRId64 " corruptions\n\n"
645 "Double checking the fixed image now...\n",
647 check->corruptions_fixed);
650 ret = collect_image_check(bs, check, filename, fmt, 0);
652 check->leaks_fixed = leaks_fixed;
653 check->corruptions_fixed = corruptions_fixed;
656 switch (output_format) {
658 dump_human_image_check(check, quiet);
661 dump_json_image_check(check, quiet);
665 if (ret || check->check_errors) {
670 if (check->corruptions) {
672 } else if (check->leaks) {
679 qapi_free_ImageCheck(check);
685 static int img_commit(int argc, char **argv)
688 const char *filename, *fmt, *cache;
689 BlockDriverState *bs;
693 cache = BDRV_DEFAULT_CACHE;
695 c = getopt(argc, argv, "f:ht:q");
715 if (optind != argc - 1) {
718 filename = argv[optind++];
721 ret = bdrv_parse_cache_flags(cache, &flags);
723 error_report("Invalid cache option: %s", cache);
727 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
731 ret = bdrv_commit(bs);
734 qprintf(quiet, "Image committed.\n");
737 error_report("No disk inserted");
740 error_report("Image is read-only");
743 error_report("Image is already committed");
746 error_report("Error while committing image");
758 * Returns true iff the first sector pointed to by 'buf' contains at least
761 * 'pnum' is set to the number of sectors (including and immediately following
762 * the first one) that are known to be in the same allocated/unallocated state.
764 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
773 is_zero = buffer_is_zero(buf, 512);
774 for(i = 1; i < n; i++) {
776 if (is_zero != buffer_is_zero(buf, 512)) {
785 * Like is_allocated_sectors, but if the buffer starts with a used sector,
786 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
787 * breaking up write requests for only small sparse areas.
789 static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
793 int num_checked, num_used;
799 ret = is_allocated_sectors(buf, n, pnum);
805 buf += BDRV_SECTOR_SIZE * *pnum;
807 num_checked = num_used;
810 ret = is_allocated_sectors(buf, n, pnum);
812 buf += BDRV_SECTOR_SIZE * *pnum;
814 num_checked += *pnum;
816 num_used = num_checked;
817 } else if (*pnum >= min) {
827 * Compares two buffers sector by sector. Returns 0 if the first sector of both
828 * buffers matches, non-zero otherwise.
830 * pnum is set to the number of sectors (including and immediately following
831 * the first one) that are known to have the same comparison result
833 static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
843 res = !!memcmp(buf1, buf2, 512);
844 for(i = 1; i < n; i++) {
848 if (!!memcmp(buf1, buf2, 512) != res) {
857 #define IO_BUF_SIZE (2 * 1024 * 1024)
859 static int64_t sectors_to_bytes(int64_t sectors)
861 return sectors << BDRV_SECTOR_BITS;
864 static int64_t sectors_to_process(int64_t total, int64_t from)
866 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
870 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
872 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
873 * data and negative value on error.
875 * @param bs: Driver used for accessing file
876 * @param sect_num: Number of first sector to check
877 * @param sect_count: Number of sectors to check
878 * @param filename: Name of disk file we are checking (logging purpose)
879 * @param buffer: Allocated buffer for storing read data
880 * @param quiet: Flag for quiet mode
882 static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
883 int sect_count, const char *filename,
884 uint8_t *buffer, bool quiet)
887 ret = bdrv_read(bs, sect_num, buffer, sect_count);
889 error_report("Error while reading offset %" PRId64 " of %s: %s",
890 sectors_to_bytes(sect_num), filename, strerror(-ret));
893 ret = is_allocated_sectors(buffer, sect_count, &pnum);
894 if (ret || pnum != sect_count) {
895 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
896 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
904 * Compares two images. Exit codes:
906 * 0 - Images are identical
908 * >1 - Error occurred
910 static int img_compare(int argc, char **argv)
912 const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
913 BlockDriverState *bs1, *bs2;
914 int64_t total_sectors1, total_sectors2;
915 uint8_t *buf1 = NULL, *buf2 = NULL;
917 int allocated1, allocated2;
918 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
919 bool progress = false, quiet = false, strict = false;
920 int64_t total_sectors;
921 int64_t sector_num = 0;
925 uint64_t progress_base;
928 c = getopt(argc, argv, "hpf:F:sq");
955 /* Progress is not shown in Quiet mode */
961 if (optind != argc - 2) {
964 filename1 = argv[optind++];
965 filename2 = argv[optind++];
967 /* Initialize before goto out */
968 qemu_progress_init(progress, 2.0);
970 bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet);
972 error_report("Can't open file %s", filename1);
977 bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet);
979 error_report("Can't open file %s", filename2);
984 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
985 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
986 bdrv_get_geometry(bs1, &bs_sectors);
987 total_sectors1 = bs_sectors;
988 bdrv_get_geometry(bs2, &bs_sectors);
989 total_sectors2 = bs_sectors;
990 total_sectors = MIN(total_sectors1, total_sectors2);
991 progress_base = MAX(total_sectors1, total_sectors2);
993 qemu_progress_print(0, 100);
995 if (strict && total_sectors1 != total_sectors2) {
997 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1002 nb_sectors = sectors_to_process(total_sectors, sector_num);
1003 if (nb_sectors <= 0) {
1006 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
1008 if (allocated1 < 0) {
1010 error_report("Sector allocation test failed for %s", filename1);
1014 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
1016 if (allocated2 < 0) {
1018 error_report("Sector allocation test failed for %s", filename2);
1021 nb_sectors = MIN(pnum1, pnum2);
1023 if (allocated1 == allocated2) {
1025 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
1027 error_report("Error while reading offset %" PRId64 " of %s:"
1028 " %s", sectors_to_bytes(sector_num), filename1,
1033 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1035 error_report("Error while reading offset %" PRId64
1036 " of %s: %s", sectors_to_bytes(sector_num),
1037 filename2, strerror(-ret));
1041 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1042 if (ret || pnum != nb_sectors) {
1043 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1045 ret ? sector_num : sector_num + pnum));
1053 qprintf(quiet, "Strict mode: Offset %" PRId64
1054 " allocation mismatch!\n",
1055 sectors_to_bytes(sector_num));
1060 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1061 filename1, buf1, quiet);
1063 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1064 filename2, buf1, quiet);
1068 error_report("Error while reading offset %" PRId64 ": %s",
1069 sectors_to_bytes(sector_num), strerror(-ret));
1075 sector_num += nb_sectors;
1076 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1079 if (total_sectors1 != total_sectors2) {
1080 BlockDriverState *bs_over;
1081 int64_t total_sectors_over;
1082 const char *filename_over;
1084 qprintf(quiet, "Warning: Image size mismatch!\n");
1085 if (total_sectors1 > total_sectors2) {
1086 total_sectors_over = total_sectors1;
1088 filename_over = filename1;
1090 total_sectors_over = total_sectors2;
1092 filename_over = filename2;
1096 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1097 if (nb_sectors <= 0) {
1100 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1104 error_report("Sector allocation test failed for %s",
1111 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1112 filename_over, buf1, quiet);
1115 error_report("Error while reading offset %" PRId64
1116 " of %s: %s", sectors_to_bytes(sector_num),
1117 filename_over, strerror(-ret));
1123 sector_num += nb_sectors;
1124 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1128 qprintf(quiet, "Images are identical.\n");
1138 qemu_progress_end();
1142 static int img_convert(int argc, char **argv)
1144 int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
1146 int progress = 0, flags;
1147 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
1148 BlockDriver *drv, *proto_drv;
1149 BlockDriverState **bs = NULL, *out_bs = NULL;
1150 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
1151 uint64_t bs_sectors;
1152 uint8_t * buf = NULL;
1153 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
1154 const uint8_t *buf1;
1155 BlockDriverInfo bdi;
1156 QEMUOptionParameter *param = NULL, *create_options = NULL;
1157 QEMUOptionParameter *out_baseimg_param;
1158 char *options = NULL;
1159 const char *snapshot_name = NULL;
1160 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
1162 Error *local_err = NULL;
1163 QemuOpts *sn_opts = NULL;
1172 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:");
1188 out_baseimg = optarg;
1194 error_report("option -e is deprecated, please use \'-o "
1195 "encryption\' instead!");
1199 error_report("option -6 is deprecated, please use \'-o "
1200 "compat6\' instead!");
1204 if (!is_valid_option_list(optarg)) {
1205 error_report("Invalid option list: %s", optarg);
1210 options = g_strdup(optarg);
1212 char *old_options = options;
1213 options = g_strdup_printf("%s,%s", options, optarg);
1214 g_free(old_options);
1218 snapshot_name = optarg;
1221 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1222 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
1224 error_report("Failed in parsing snapshot param '%s'",
1230 snapshot_name = optarg;
1237 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1238 if (sval < 0 || *end) {
1239 error_report("Invalid minimum zero buffer size for sparse output specified");
1244 min_sparse = sval / BDRV_SECTOR_SIZE;
1262 /* Initialize before goto out */
1266 qemu_progress_init(progress, 1.0);
1269 bs_n = argc - optind - 1;
1270 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
1272 if (options && has_help_option(options)) {
1273 ret = print_block_option_help(out_filename, out_fmt);
1282 if (bs_n > 1 && out_baseimg) {
1283 error_report("-B makes no sense when concatenating multiple input "
1289 qemu_progress_print(0, 100);
1291 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
1294 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1295 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true,
1298 error_report("Could not open '%s'", argv[optind + bs_i]);
1302 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1303 total_sectors += bs_sectors;
1307 ret = bdrv_snapshot_load_tmp(bs[0],
1308 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1309 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1311 } else if (snapshot_name != NULL) {
1313 error_report("No support for concatenating multiple snapshot");
1318 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
1321 error_report("Failed to load snapshot: %s",
1322 error_get_pretty(local_err));
1323 error_free(local_err);
1328 /* Find driver and parse its options */
1329 drv = bdrv_find_format(out_fmt);
1331 error_report("Unknown file format '%s'", out_fmt);
1336 proto_drv = bdrv_find_protocol(out_filename, true);
1338 error_report("Unknown protocol '%s'", out_filename);
1343 create_options = append_option_parameters(create_options,
1344 drv->create_options);
1345 create_options = append_option_parameters(create_options,
1346 proto_drv->create_options);
1349 param = parse_option_parameters(options, create_options, param);
1350 if (param == NULL) {
1351 error_report("Invalid options for file format '%s'.", out_fmt);
1356 param = parse_option_parameters("", create_options, param);
1359 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
1360 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
1365 /* Get backing file name if -o backing_file was used */
1366 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1367 if (out_baseimg_param) {
1368 out_baseimg = out_baseimg_param->value.s;
1371 /* Check if compression is supported */
1373 QEMUOptionParameter *encryption =
1374 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
1375 QEMUOptionParameter *preallocation =
1376 get_option_parameter(param, BLOCK_OPT_PREALLOC);
1378 if (!drv->bdrv_write_compressed) {
1379 error_report("Compression not supported for this file format");
1384 if (encryption && encryption->value.n) {
1385 error_report("Compression and encryption not supported at "
1391 if (preallocation && preallocation->value.s
1392 && strcmp(preallocation->value.s, "off"))
1394 error_report("Compression and preallocation not supported at "
1402 /* Create the new image */
1403 ret = bdrv_create(drv, out_filename, param, &local_err);
1405 error_report("%s: error while converting %s: %s",
1406 out_filename, out_fmt, error_get_pretty(local_err));
1407 error_free(local_err);
1412 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
1413 ret = bdrv_parse_cache_flags(cache, &flags);
1415 error_report("Invalid cache option: %s", cache);
1419 out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet);
1427 bdrv_get_geometry(bs[0], &bs_sectors);
1429 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1430 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1432 bufsectors = MIN(32768,
1433 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1434 out_bs->bl.discard_alignment))
1437 buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
1440 int64_t output_length = bdrv_getlength(out_bs);
1441 if (output_length < 0) {
1442 error_report("unable to get output image length: %s\n",
1443 strerror(-output_length));
1446 } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
1447 error_report("output file is smaller than input file");
1453 cluster_sectors = 0;
1454 ret = bdrv_get_info(out_bs, &bdi);
1457 error_report("could not get block driver info");
1461 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1465 if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
1466 error_report("invalid cluster size");
1472 nb_sectors = total_sectors;
1479 nb_sectors = total_sectors - sector_num;
1480 if (nb_sectors <= 0)
1482 if (nb_sectors >= cluster_sectors)
1483 n = cluster_sectors;
1487 bs_num = sector_num - bs_offset;
1488 assert (bs_num >= 0);
1491 while (remainder > 0) {
1493 while (bs_num == bs_sectors) {
1495 assert (bs_i < bs_n);
1496 bs_offset += bs_sectors;
1497 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1499 /* printf("changing part: sector_num=%" PRId64 ", "
1500 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1501 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
1503 assert (bs_num < bs_sectors);
1505 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1507 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1509 error_report("error while reading sector %" PRId64 ": %s",
1510 bs_num, strerror(-ret));
1519 assert (remainder == 0);
1521 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1522 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
1524 error_report("error while compressing sector %" PRId64
1525 ": %s", sector_num, strerror(-ret));
1530 qemu_progress_print(100.0 * sector_num / total_sectors, 0);
1532 /* signal EOF to align */
1533 bdrv_write_compressed(out_bs, 0, NULL, 0);
1535 int64_t sectors_to_read, sectors_read, sector_num_next_status;
1536 bool count_allocated_sectors;
1537 int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0;
1539 if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
1540 ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
1547 sectors_to_read = total_sectors;
1548 count_allocated_sectors = progress && (out_baseimg || has_zero_init);
1550 sector_num = 0; // total number of sectors converted so far
1552 sector_num_next_status = 0;
1555 nb_sectors = total_sectors - sector_num;
1556 if (nb_sectors <= 0) {
1557 if (count_allocated_sectors) {
1558 sectors_to_read = sectors_read;
1559 count_allocated_sectors = false;
1566 while (sector_num - bs_offset >= bs_sectors) {
1568 assert (bs_i < bs_n);
1569 bs_offset += bs_sectors;
1570 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1571 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1572 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
1573 sector_num, bs_i, bs_offset, bs_sectors); */
1576 if ((out_baseimg || has_zero_init) &&
1577 sector_num >= sector_num_next_status) {
1578 n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors;
1579 ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset,
1582 error_report("error while reading block status of sector %"
1583 PRId64 ": %s", sector_num - bs_offset,
1587 /* If the output image is zero initialized, we are not working
1588 * on a shared base and the input is zero we can skip the next
1590 if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) {
1594 /* If the output image is being created as a copy on write
1595 * image, assume that sectors which are unallocated in the
1596 * input image are present in both the output's and input's
1597 * base images (no need to copy them). */
1599 if (!(ret & BDRV_BLOCK_DATA)) {
1603 /* The next 'n1' sectors are allocated in the input image.
1604 * Copy only those as they may be followed by unallocated
1608 /* avoid redundant callouts to get_block_status */
1609 sector_num_next_status = sector_num + n1;
1612 n = MIN(nb_sectors, bufsectors);
1614 /* round down request length to an aligned sector, but
1615 * do not bother doing this on short requests. They happen
1616 * when we found an all-zero area, and the next sector to
1617 * write will not be sector_num + n. */
1618 if (cluster_sectors > 0 && n >= cluster_sectors) {
1619 int64_t next_aligned_sector = (sector_num + n);
1620 next_aligned_sector -= next_aligned_sector % cluster_sectors;
1621 if (sector_num + n > next_aligned_sector) {
1622 n = next_aligned_sector - sector_num;
1626 n = MIN(n, bs_sectors - (sector_num - bs_offset));
1629 if (count_allocated_sectors) {
1635 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1637 error_report("error while reading sector %" PRId64 ": %s",
1638 sector_num - bs_offset, strerror(-ret));
1641 /* NOTE: at the same time we convert, we do not write zero
1642 sectors to have a chance to compress the image. Ideally, we
1643 should add a specific call to have the info to go faster */
1646 if (!has_zero_init ||
1647 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
1648 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1650 error_report("error while writing sector %" PRId64
1651 ": %s", sector_num, strerror(-ret));
1659 qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
1664 qemu_progress_print(100, 0);
1666 qemu_progress_end();
1667 free_option_parameters(create_options);
1668 free_option_parameters(param);
1671 qemu_opts_del(sn_opts);
1677 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1679 bdrv_unref(bs[bs_i]);
1694 static void dump_snapshots(BlockDriverState *bs)
1696 QEMUSnapshotInfo *sn_tab, *sn;
1699 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1702 printf("Snapshot list:\n");
1703 bdrv_snapshot_dump(fprintf, stdout, NULL);
1705 for(i = 0; i < nb_sns; i++) {
1707 bdrv_snapshot_dump(fprintf, stdout, sn);
1713 static void dump_json_image_info_list(ImageInfoList *list)
1717 QmpOutputVisitor *ov = qmp_output_visitor_new();
1719 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1720 &list, NULL, &errp);
1721 obj = qmp_output_get_qobject(ov);
1722 str = qobject_to_json_pretty(obj);
1723 assert(str != NULL);
1724 printf("%s\n", qstring_get_str(str));
1725 qobject_decref(obj);
1726 qmp_output_visitor_cleanup(ov);
1730 static void dump_json_image_info(ImageInfo *info)
1734 QmpOutputVisitor *ov = qmp_output_visitor_new();
1736 visit_type_ImageInfo(qmp_output_get_visitor(ov),
1737 &info, NULL, &errp);
1738 obj = qmp_output_get_qobject(ov);
1739 str = qobject_to_json_pretty(obj);
1740 assert(str != NULL);
1741 printf("%s\n", qstring_get_str(str));
1742 qobject_decref(obj);
1743 qmp_output_visitor_cleanup(ov);
1747 static void dump_human_image_info_list(ImageInfoList *list)
1749 ImageInfoList *elem;
1752 for (elem = list; elem; elem = elem->next) {
1758 bdrv_image_info_dump(fprintf, stdout, elem->value);
1762 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1764 return strcmp(a, b) == 0;
1768 * Open an image file chain and return an ImageInfoList
1770 * @filename: topmost image filename
1771 * @fmt: topmost image format (may be NULL to autodetect)
1772 * @chain: true - enumerate entire backing file chain
1773 * false - only topmost image file
1775 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1776 * image file. If there was an error a message will have been printed to
1779 static ImageInfoList *collect_image_info_list(const char *filename,
1783 ImageInfoList *head = NULL;
1784 ImageInfoList **last = &head;
1785 GHashTable *filenames;
1788 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1791 BlockDriverState *bs;
1793 ImageInfoList *elem;
1795 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1796 error_report("Backing file '%s' creates an infinite loop.",
1800 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1802 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
1808 bdrv_query_image_info(bs, &info, &err);
1810 error_report("%s", error_get_pretty(err));
1815 elem = g_new0(ImageInfoList, 1);
1822 filename = fmt = NULL;
1824 if (info->has_full_backing_filename) {
1825 filename = info->full_backing_filename;
1826 } else if (info->has_backing_filename) {
1827 filename = info->backing_filename;
1829 if (info->has_backing_filename_format) {
1830 fmt = info->backing_filename_format;
1834 g_hash_table_destroy(filenames);
1838 qapi_free_ImageInfoList(head);
1839 g_hash_table_destroy(filenames);
1843 static int img_info(int argc, char **argv)
1846 OutputFormat output_format = OFORMAT_HUMAN;
1848 const char *filename, *fmt, *output;
1849 ImageInfoList *list;
1854 int option_index = 0;
1855 static const struct option long_options[] = {
1856 {"help", no_argument, 0, 'h'},
1857 {"format", required_argument, 0, 'f'},
1858 {"output", required_argument, 0, OPTION_OUTPUT},
1859 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
1862 c = getopt_long(argc, argv, "f:h",
1863 long_options, &option_index);
1878 case OPTION_BACKING_CHAIN:
1883 if (optind != argc - 1) {
1886 filename = argv[optind++];
1888 if (output && !strcmp(output, "json")) {
1889 output_format = OFORMAT_JSON;
1890 } else if (output && !strcmp(output, "human")) {
1891 output_format = OFORMAT_HUMAN;
1892 } else if (output) {
1893 error_report("--output must be used with human or json as argument.");
1897 list = collect_image_info_list(filename, fmt, chain);
1902 switch (output_format) {
1904 dump_human_image_info_list(list);
1908 dump_json_image_info_list(list);
1910 dump_json_image_info(list->value);
1915 qapi_free_ImageInfoList(list);
1920 typedef struct MapEntry {
1926 BlockDriverState *bs;
1929 static void dump_map_entry(OutputFormat output_format, MapEntry *e,
1932 switch (output_format) {
1934 if ((e->flags & BDRV_BLOCK_DATA) &&
1935 !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
1936 error_report("File contains external, encrypted or compressed clusters.");
1939 if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
1940 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
1941 e->start, e->length, e->offset, e->bs->filename);
1943 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
1944 * Modify the flags here to allow more coalescing.
1947 (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
1948 next->flags &= ~BDRV_BLOCK_DATA;
1949 next->flags |= BDRV_BLOCK_ZERO;
1953 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
1954 " \"zero\": %s, \"data\": %s",
1955 (e->start == 0 ? "[" : ",\n"),
1956 e->start, e->length, e->depth,
1957 (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
1958 (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
1959 if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
1960 printf(", \"offset\": %"PRId64"", e->offset);
1971 static int get_block_status(BlockDriverState *bs, int64_t sector_num,
1972 int nb_sectors, MapEntry *e)
1977 /* As an optimization, we could cache the current range of unallocated
1978 * clusters in each file of the chain, and avoid querying the same
1984 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
1989 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
1992 bs = bs->backing_hd;
2001 e->start = sector_num * BDRV_SECTOR_SIZE;
2002 e->length = nb_sectors * BDRV_SECTOR_SIZE;
2003 e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
2004 e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
2010 static int img_map(int argc, char **argv)
2013 OutputFormat output_format = OFORMAT_HUMAN;
2014 BlockDriverState *bs;
2015 const char *filename, *fmt, *output;
2017 MapEntry curr = { .length = 0 }, next;
2023 int option_index = 0;
2024 static const struct option long_options[] = {
2025 {"help", no_argument, 0, 'h'},
2026 {"format", required_argument, 0, 'f'},
2027 {"output", required_argument, 0, OPTION_OUTPUT},
2030 c = getopt_long(argc, argv, "f:h",
2031 long_options, &option_index);
2048 if (optind >= argc) {
2051 filename = argv[optind++];
2053 if (output && !strcmp(output, "json")) {
2054 output_format = OFORMAT_JSON;
2055 } else if (output && !strcmp(output, "human")) {
2056 output_format = OFORMAT_HUMAN;
2057 } else if (output) {
2058 error_report("--output must be used with human or json as argument.");
2062 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS, true, false);
2067 if (output_format == OFORMAT_HUMAN) {
2068 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2071 length = bdrv_getlength(bs);
2072 while (curr.start + curr.length < length) {
2073 int64_t nsectors_left;
2077 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2079 /* Probe up to 1 GiB at a time. */
2080 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2081 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2082 ret = get_block_status(bs, sector_num, n, &next);
2085 error_report("Could not read file metadata: %s", strerror(-ret));
2089 if (curr.length != 0 && curr.flags == next.flags &&
2090 curr.depth == next.depth &&
2091 ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
2092 curr.offset + curr.length == next.offset)) {
2093 curr.length += next.length;
2097 if (curr.length > 0) {
2098 dump_map_entry(output_format, &curr, &next);
2103 dump_map_entry(output_format, &curr, NULL);
2110 #define SNAPSHOT_LIST 1
2111 #define SNAPSHOT_CREATE 2
2112 #define SNAPSHOT_APPLY 3
2113 #define SNAPSHOT_DELETE 4
2115 static int img_snapshot(int argc, char **argv)
2117 BlockDriverState *bs;
2118 QEMUSnapshotInfo sn;
2119 char *filename, *snapshot_name = NULL;
2120 int c, ret = 0, bdrv_oflags;
2126 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
2127 /* Parse commandline parameters */
2129 c = getopt(argc, argv, "la:c:d:hq");
2143 action = SNAPSHOT_LIST;
2144 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
2151 action = SNAPSHOT_APPLY;
2152 snapshot_name = optarg;
2159 action = SNAPSHOT_CREATE;
2160 snapshot_name = optarg;
2167 action = SNAPSHOT_DELETE;
2168 snapshot_name = optarg;
2176 if (optind != argc - 1) {
2179 filename = argv[optind++];
2181 /* Open the image */
2182 bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet);
2187 /* Perform the requested action */
2193 case SNAPSHOT_CREATE:
2194 memset(&sn, 0, sizeof(sn));
2195 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2197 qemu_gettimeofday(&tv);
2198 sn.date_sec = tv.tv_sec;
2199 sn.date_nsec = tv.tv_usec * 1000;
2201 ret = bdrv_snapshot_create(bs, &sn);
2203 error_report("Could not create snapshot '%s': %d (%s)",
2204 snapshot_name, ret, strerror(-ret));
2208 case SNAPSHOT_APPLY:
2209 ret = bdrv_snapshot_goto(bs, snapshot_name);
2211 error_report("Could not apply snapshot '%s': %d (%s)",
2212 snapshot_name, ret, strerror(-ret));
2216 case SNAPSHOT_DELETE:
2217 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
2219 error_report("Could not delete snapshot '%s': (%s)",
2220 snapshot_name, error_get_pretty(err));
2235 static int img_rebase(int argc, char **argv)
2237 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
2238 BlockDriver *old_backing_drv, *new_backing_drv;
2240 const char *fmt, *cache, *out_basefmt, *out_baseimg;
2245 Error *local_err = NULL;
2247 /* Parse commandline parameters */
2249 cache = BDRV_DEFAULT_CACHE;
2253 c = getopt(argc, argv, "uhf:F:b:pt:q");
2266 out_basefmt = optarg;
2269 out_baseimg = optarg;
2290 if ((optind != argc - 1) || (!unsafe && !out_baseimg)) {
2293 filename = argv[optind++];
2295 qemu_progress_init(progress, 2.0);
2296 qemu_progress_print(0, 100);
2298 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
2299 ret = bdrv_parse_cache_flags(cache, &flags);
2301 error_report("Invalid cache option: %s", cache);
2308 * Ignore the old backing file for unsafe rebase in case we want to correct
2309 * the reference to a renamed or moved backing file.
2311 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
2316 /* Find the right drivers for the backing files */
2317 old_backing_drv = NULL;
2318 new_backing_drv = NULL;
2320 if (!unsafe && bs->backing_format[0] != '\0') {
2321 old_backing_drv = bdrv_find_format(bs->backing_format);
2322 if (old_backing_drv == NULL) {
2323 error_report("Invalid format name: '%s'", bs->backing_format);
2329 if (out_basefmt != NULL) {
2330 new_backing_drv = bdrv_find_format(out_basefmt);
2331 if (new_backing_drv == NULL) {
2332 error_report("Invalid format name: '%s'", out_basefmt);
2338 /* For safe rebasing we need to compare old and new backing file */
2340 /* Make the compiler happy */
2341 bs_old_backing = NULL;
2342 bs_new_backing = NULL;
2344 char backing_name[1024];
2346 bs_old_backing = bdrv_new("old_backing");
2347 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2348 ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, BDRV_O_FLAGS,
2349 old_backing_drv, &local_err);
2351 error_report("Could not open old backing file '%s': %s",
2352 backing_name, error_get_pretty(local_err));
2353 error_free(local_err);
2356 if (out_baseimg[0]) {
2357 bs_new_backing = bdrv_new("new_backing");
2358 ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL,
2359 BDRV_O_FLAGS, new_backing_drv, &local_err);
2361 error_report("Could not open new backing file '%s': %s",
2362 out_baseimg, error_get_pretty(local_err));
2363 error_free(local_err);
2370 * Check each unallocated cluster in the COW file. If it is unallocated,
2371 * accesses go to the backing file. We must therefore compare this cluster
2372 * in the old and new backing file, and if they differ we need to copy it
2373 * from the old backing file into the COW file.
2375 * If qemu-img crashes during this step, no harm is done. The content of
2376 * the image is the same as the original one at any time.
2379 uint64_t num_sectors;
2380 uint64_t old_backing_num_sectors;
2381 uint64_t new_backing_num_sectors = 0;
2386 float local_progress = 0;
2388 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2389 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
2391 bdrv_get_geometry(bs, &num_sectors);
2392 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
2393 if (bs_new_backing) {
2394 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2397 if (num_sectors != 0) {
2398 local_progress = (float)100 /
2399 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2402 for (sector = 0; sector < num_sectors; sector += n) {
2404 /* How many sectors can we handle with the next read? */
2405 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2406 n = (IO_BUF_SIZE / 512);
2408 n = num_sectors - sector;
2411 /* If the cluster is allocated, we don't need to take action */
2412 ret = bdrv_is_allocated(bs, sector, n, &n);
2414 error_report("error while reading image metadata: %s",
2423 * Read old and new backing file and take into consideration that
2424 * backing files may be smaller than the COW image.
2426 if (sector >= old_backing_num_sectors) {
2427 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2429 if (sector + n > old_backing_num_sectors) {
2430 n = old_backing_num_sectors - sector;
2433 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2435 error_report("error while reading from old backing file");
2440 if (sector >= new_backing_num_sectors || !bs_new_backing) {
2441 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2443 if (sector + n > new_backing_num_sectors) {
2444 n = new_backing_num_sectors - sector;
2447 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2449 error_report("error while reading from new backing file");
2454 /* If they differ, we need to write to the COW file */
2455 uint64_t written = 0;
2457 while (written < n) {
2460 if (compare_sectors(buf_old + written * 512,
2461 buf_new + written * 512, n - written, &pnum))
2463 ret = bdrv_write(bs, sector + written,
2464 buf_old + written * 512, pnum);
2466 error_report("Error while writing to COW image: %s",
2474 qemu_progress_print(local_progress, 100);
2477 qemu_vfree(buf_old);
2478 qemu_vfree(buf_new);
2482 * Change the backing file. All clusters that are different from the old
2483 * backing file are overwritten in the COW file now, so the visible content
2484 * doesn't change when we switch the backing file.
2486 if (out_baseimg && *out_baseimg) {
2487 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2489 ret = bdrv_change_backing_file(bs, NULL, NULL);
2492 if (ret == -ENOSPC) {
2493 error_report("Could not change the backing file to '%s': No "
2494 "space left in the file header", out_baseimg);
2495 } else if (ret < 0) {
2496 error_report("Could not change the backing file to '%s': %s",
2497 out_baseimg, strerror(-ret));
2500 qemu_progress_print(100, 0);
2502 * TODO At this point it is possible to check if any clusters that are
2503 * allocated in the COW file are the same in the backing file. If so, they
2504 * could be dropped from the COW file. Don't do this before switching the
2505 * backing file, in case of a crash this would lead to corruption.
2508 qemu_progress_end();
2511 if (bs_old_backing != NULL) {
2512 bdrv_unref(bs_old_backing);
2514 if (bs_new_backing != NULL) {
2515 bdrv_unref(bs_new_backing);
2526 static int img_resize(int argc, char **argv)
2528 int c, ret, relative;
2529 const char *filename, *fmt, *size;
2530 int64_t n, total_size;
2532 BlockDriverState *bs = NULL;
2534 static QemuOptsList resize_options = {
2535 .name = "resize_options",
2536 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2539 .name = BLOCK_OPT_SIZE,
2540 .type = QEMU_OPT_SIZE,
2541 .help = "Virtual disk size"
2548 /* Remove size from argv manually so that negative numbers are not treated
2549 * as options by getopt. */
2555 size = argv[--argc];
2557 /* Parse getopt arguments */
2560 c = getopt(argc, argv, "f:hq");
2577 if (optind != argc - 1) {
2580 filename = argv[optind++];
2582 /* Choose grow, shrink, or absolute resize mode */
2598 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
2599 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
2600 /* Error message already printed when size parsing fails */
2602 qemu_opts_del(param);
2605 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2606 qemu_opts_del(param);
2608 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
2615 total_size = bdrv_getlength(bs) + n * relative;
2619 if (total_size <= 0) {
2620 error_report("New image size must be positive");
2625 ret = bdrv_truncate(bs, total_size);
2628 qprintf(quiet, "Image resized.\n");
2631 error_report("This image does not support resize");
2634 error_report("Image is read-only");
2637 error_report("Error resizing image (%d)", -ret);
2650 static int img_amend(int argc, char **argv)
2653 char *options = NULL;
2654 QEMUOptionParameter *create_options = NULL, *options_param = NULL;
2655 const char *fmt = NULL, *filename;
2657 BlockDriverState *bs = NULL;
2660 c = getopt(argc, argv, "hqf:o:");
2671 if (!is_valid_option_list(optarg)) {
2672 error_report("Invalid option list: %s", optarg);
2677 options = g_strdup(optarg);
2679 char *old_options = options;
2680 options = g_strdup_printf("%s,%s", options, optarg);
2681 g_free(old_options);
2697 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
2698 if (fmt && has_help_option(options)) {
2699 /* If a format is explicitly specified (and possibly no filename is
2700 * given), print option help here */
2701 ret = print_block_option_help(filename, fmt);
2705 if (optind != argc - 1) {
2709 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
2711 error_report("Could not open image '%s'", filename);
2716 fmt = bs->drv->format_name;
2718 if (has_help_option(options)) {
2719 /* If the format was auto-detected, print option help here */
2720 ret = print_block_option_help(filename, fmt);
2724 create_options = append_option_parameters(create_options,
2725 bs->drv->create_options);
2726 options_param = parse_option_parameters(options, create_options,
2728 if (options_param == NULL) {
2729 error_report("Invalid options for file format '%s'", fmt);
2734 ret = bdrv_amend_options(bs, options_param);
2736 error_report("Error while amending options: %s", strerror(-ret));
2744 free_option_parameters(create_options);
2745 free_option_parameters(options_param);
2754 static const img_cmd_t img_cmds[] = {
2755 #define DEF(option, callback, arg_string) \
2756 { option, callback },
2757 #include "qemu-img-cmds.h"
2763 int main(int argc, char **argv)
2765 const img_cmd_t *cmd;
2766 const char *cmdname;
2769 signal(SIGPIPE, SIG_IGN);
2772 error_set_progname(argv[0]);
2773 qemu_init_exec_dir(argv[0]);
2775 qemu_init_main_loop();
2782 /* find the command */
2783 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2784 if (!strcmp(cmdname, cmd->name)) {
2785 return cmd->handler(argc, argv);