#include "qemu/osdep.h"
#include "sysemu/sysemu.h"
#include "block/block_int.h"
+#include "block/qapi.h"
#include <getopt.h>
#include <stdio.h>
#include <stdarg.h>
" options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
" 'directsync' and 'unsafe' (default for convert)\n"
" 'size' is the disk image size in bytes. Optional suffixes\n"
- " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
- " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
+ " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
+ " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
+ " supported. 'b' is ignored.\n"
" 'output_filename' is the destination disk image filename\n"
" 'output_fmt' is the destination format\n"
" 'options' is a comma separated list of format specific options in a\n"
" '-a' applies a snapshot (revert disk to saved state)\n"
" '-c' creates a snapshot\n"
" '-d' deletes a snapshot\n"
- " '-l' lists all snapshots in the given image\n";
+ " '-l' lists all snapshots in the given image\n"
+ "\n"
+ "Parameters to compare subcommand:\n"
+ " '-f' first image format\n"
+ " '-F' second image format\n"
+ " '-s' run in Strict mode - fail on different image size or sector allocation\n";
printf("%s\nSupported formats:", help_msg);
bdrv_iterate_format(format_print, NULL);
exit(1);
}
-static int qprintf(bool quiet, const char *fmt, ...)
+static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
{
int ret = 0;
if (!quiet) {
return 1;
}
- proto_drv = bdrv_find_protocol(filename);
+ proto_drv = bdrv_find_protocol(filename, true);
if (!proto_drv) {
error_report("Unknown protocol '%s'", filename);
return 1;
drv = NULL;
}
- ret = bdrv_open(bs, filename, flags, drv);
+ ret = bdrv_open(bs, filename, NULL, flags, drv);
if (ret < 0) {
error_report("Could not open '%s': %s", filename, strerror(-ret));
goto fail;
error_report("Image size must be less than 8 EiB!");
} else {
error_report("Invalid image size specified! You may use k, M, "
- "G or T suffixes for ");
- error_report("kilobytes, megabytes, gigabytes and terabytes.");
+ "G, T, P or E suffixes for ");
+ error_report("kilobytes, megabytes, gigabytes, terabytes, "
+ "petabytes and exabytes.");
}
return 1;
}
#define IO_BUF_SIZE (2 * 1024 * 1024)
+static int64_t sectors_to_bytes(int64_t sectors)
+{
+ return sectors << BDRV_SECTOR_BITS;
+}
+
+static int64_t sectors_to_process(int64_t total, int64_t from)
+{
+ return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
+}
+
+/*
+ * Check if passed sectors are empty (not allocated or contain only 0 bytes)
+ *
+ * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
+ * data and negative value on error.
+ *
+ * @param bs: Driver used for accessing file
+ * @param sect_num: Number of first sector to check
+ * @param sect_count: Number of sectors to check
+ * @param filename: Name of disk file we are checking (logging purpose)
+ * @param buffer: Allocated buffer for storing read data
+ * @param quiet: Flag for quiet mode
+ */
+static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
+ int sect_count, const char *filename,
+ uint8_t *buffer, bool quiet)
+{
+ int pnum, ret = 0;
+ ret = bdrv_read(bs, sect_num, buffer, sect_count);
+ if (ret < 0) {
+ error_report("Error while reading offset %" PRId64 " of %s: %s",
+ sectors_to_bytes(sect_num), filename, strerror(-ret));
+ return ret;
+ }
+ ret = is_allocated_sectors(buffer, sect_count, &pnum);
+ if (ret || pnum != sect_count) {
+ qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
+ sectors_to_bytes(ret ? sect_num : sect_num + pnum));
+ return 1;
+ }
+
+ return 0;
+}
+
+/*
+ * Compares two images. Exit codes:
+ *
+ * 0 - Images are identical
+ * 1 - Images differ
+ * >1 - Error occurred
+ */
+static int img_compare(int argc, char **argv)
+{
+ const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
+ BlockDriverState *bs1, *bs2;
+ int64_t total_sectors1, total_sectors2;
+ uint8_t *buf1 = NULL, *buf2 = NULL;
+ int pnum1, pnum2;
+ int allocated1, allocated2;
+ int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
+ bool progress = false, quiet = false, strict = false;
+ int64_t total_sectors;
+ int64_t sector_num = 0;
+ int64_t nb_sectors;
+ int c, pnum;
+ uint64_t bs_sectors;
+ uint64_t progress_base;
+
+ for (;;) {
+ c = getopt(argc, argv, "hpf:F:sq");
+ if (c == -1) {
+ break;
+ }
+ switch (c) {
+ case '?':
+ case 'h':
+ help();
+ break;
+ case 'f':
+ fmt1 = optarg;
+ break;
+ case 'F':
+ fmt2 = optarg;
+ break;
+ case 'p':
+ progress = true;
+ break;
+ case 'q':
+ quiet = true;
+ break;
+ case 's':
+ strict = true;
+ break;
+ }
+ }
+
+ /* Progress is not shown in Quiet mode */
+ if (quiet) {
+ progress = false;
+ }
+
+
+ if (optind > argc - 2) {
+ help();
+ }
+ filename1 = argv[optind++];
+ filename2 = argv[optind++];
+
+ /* Initialize before goto out */
+ qemu_progress_init(progress, 2.0);
+
+ bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet);
+ if (!bs1) {
+ error_report("Can't open file %s", filename1);
+ ret = 2;
+ goto out3;
+ }
+
+ bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet);
+ if (!bs2) {
+ error_report("Can't open file %s", filename2);
+ ret = 2;
+ goto out2;
+ }
+
+ buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
+ buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
+ bdrv_get_geometry(bs1, &bs_sectors);
+ total_sectors1 = bs_sectors;
+ bdrv_get_geometry(bs2, &bs_sectors);
+ total_sectors2 = bs_sectors;
+ total_sectors = MIN(total_sectors1, total_sectors2);
+ progress_base = MAX(total_sectors1, total_sectors2);
+
+ qemu_progress_print(0, 100);
+
+ if (strict && total_sectors1 != total_sectors2) {
+ ret = 1;
+ qprintf(quiet, "Strict mode: Image size mismatch!\n");
+ goto out;
+ }
+
+ for (;;) {
+ nb_sectors = sectors_to_process(total_sectors, sector_num);
+ if (nb_sectors <= 0) {
+ break;
+ }
+ allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
+ &pnum1);
+ if (allocated1 < 0) {
+ ret = 3;
+ error_report("Sector allocation test failed for %s", filename1);
+ goto out;
+ }
+
+ allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
+ &pnum2);
+ if (allocated2 < 0) {
+ ret = 3;
+ error_report("Sector allocation test failed for %s", filename2);
+ goto out;
+ }
+ nb_sectors = MIN(pnum1, pnum2);
+
+ if (allocated1 == allocated2) {
+ if (allocated1) {
+ ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
+ if (ret < 0) {
+ error_report("Error while reading offset %" PRId64 " of %s:"
+ " %s", sectors_to_bytes(sector_num), filename1,
+ strerror(-ret));
+ ret = 4;
+ goto out;
+ }
+ ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
+ if (ret < 0) {
+ error_report("Error while reading offset %" PRId64
+ " of %s: %s", sectors_to_bytes(sector_num),
+ filename2, strerror(-ret));
+ ret = 4;
+ goto out;
+ }
+ ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
+ if (ret || pnum != nb_sectors) {
+ ret = 1;
+ qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
+ sectors_to_bytes(
+ ret ? sector_num : sector_num + pnum));
+ goto out;
+ }
+ }
+ } else {
+ if (strict) {
+ ret = 1;
+ qprintf(quiet, "Strict mode: Offset %" PRId64
+ " allocation mismatch!\n",
+ sectors_to_bytes(sector_num));
+ goto out;
+ }
+
+ if (allocated1) {
+ ret = check_empty_sectors(bs1, sector_num, nb_sectors,
+ filename1, buf1, quiet);
+ } else {
+ ret = check_empty_sectors(bs2, sector_num, nb_sectors,
+ filename2, buf1, quiet);
+ }
+ if (ret) {
+ if (ret < 0) {
+ ret = 4;
+ error_report("Error while reading offset %" PRId64 ": %s",
+ sectors_to_bytes(sector_num), strerror(-ret));
+ }
+ goto out;
+ }
+ }
+ sector_num += nb_sectors;
+ qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
+ }
+
+ if (total_sectors1 != total_sectors2) {
+ BlockDriverState *bs_over;
+ int64_t total_sectors_over;
+ const char *filename_over;
+
+ qprintf(quiet, "Warning: Image size mismatch!\n");
+ if (total_sectors1 > total_sectors2) {
+ total_sectors_over = total_sectors1;
+ bs_over = bs1;
+ filename_over = filename1;
+ } else {
+ total_sectors_over = total_sectors2;
+ bs_over = bs2;
+ filename_over = filename2;
+ }
+
+ for (;;) {
+ nb_sectors = sectors_to_process(total_sectors_over, sector_num);
+ if (nb_sectors <= 0) {
+ break;
+ }
+ ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
+ nb_sectors, &pnum);
+ if (ret < 0) {
+ ret = 3;
+ error_report("Sector allocation test failed for %s",
+ filename_over);
+ goto out;
+
+ }
+ nb_sectors = pnum;
+ if (ret) {
+ ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
+ filename_over, buf1, quiet);
+ if (ret) {
+ if (ret < 0) {
+ ret = 4;
+ error_report("Error while reading offset %" PRId64
+ " of %s: %s", sectors_to_bytes(sector_num),
+ filename_over, strerror(-ret));
+ }
+ goto out;
+ }
+ }
+ sector_num += nb_sectors;
+ qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
+ }
+ }
+
+ qprintf(quiet, "Images are identical.\n");
+ ret = 0;
+
+out:
+ bdrv_delete(bs2);
+ qemu_vfree(buf1);
+ qemu_vfree(buf2);
+out2:
+ bdrv_delete(bs1);
+out3:
+ qemu_progress_end();
+ return ret;
+}
+
static int img_convert(int argc, char **argv)
{
int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
goto out;
}
- proto_drv = bdrv_find_protocol(out_filename);
+ proto_drv = bdrv_find_protocol(out_filename, true);
if (!proto_drv) {
error_report("Unknown protocol '%s'", out_filename);
ret = -1;
}
assert (remainder == 0);
- if (n < cluster_sectors) {
- memset(buf + n * 512, 0, cluster_size - n * 512);
- }
- if (!buffer_is_zero(buf, cluster_size)) {
- ret = bdrv_write_compressed(out_bs, sector_num, buf,
- cluster_sectors);
+ if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
+ ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
if (ret != 0) {
error_report("error while compressing sector %" PRId64
": %s", sector_num, strerror(-ret));
{
QEMUSnapshotInfo *sn_tab, *sn;
int nb_sns, i;
- char buf[256];
nb_sns = bdrv_snapshot_list(bs, &sn_tab);
if (nb_sns <= 0)
return;
printf("Snapshot list:\n");
- printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+ bdrv_snapshot_dump(fprintf, stdout, NULL);
+ printf("\n");
for(i = 0; i < nb_sns; i++) {
sn = &sn_tab[i];
- printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+ bdrv_snapshot_dump(fprintf, stdout, sn);
+ printf("\n");
}
g_free(sn_tab);
}
QDECREF(str);
}
-static void collect_snapshots(BlockDriverState *bs , ImageInfo *info)
-{
- int i, sn_count;
- QEMUSnapshotInfo *sn_tab = NULL;
- SnapshotInfoList *info_list, *cur_item = NULL;
- sn_count = bdrv_snapshot_list(bs, &sn_tab);
-
- for (i = 0; i < sn_count; i++) {
- info->has_snapshots = true;
- info_list = g_new0(SnapshotInfoList, 1);
-
- info_list->value = g_new0(SnapshotInfo, 1);
- info_list->value->id = g_strdup(sn_tab[i].id_str);
- info_list->value->name = g_strdup(sn_tab[i].name);
- info_list->value->vm_state_size = sn_tab[i].vm_state_size;
- info_list->value->date_sec = sn_tab[i].date_sec;
- info_list->value->date_nsec = sn_tab[i].date_nsec;
- info_list->value->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
- info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
-
- /* XXX: waiting for the qapi to support qemu-queue.h types */
- if (!cur_item) {
- info->snapshots = cur_item = info_list;
- } else {
- cur_item->next = info_list;
- cur_item = info_list;
- }
-
- }
-
- g_free(sn_tab);
-}
-
static void dump_json_image_info(ImageInfo *info)
{
Error *errp = NULL;
QDECREF(str);
}
-static void collect_image_info(BlockDriverState *bs,
- ImageInfo *info,
- const char *filename,
- const char *fmt)
-{
- uint64_t total_sectors;
- char backing_filename[1024];
- char backing_filename2[1024];
- BlockDriverInfo bdi;
-
- bdrv_get_geometry(bs, &total_sectors);
-
- info->filename = g_strdup(filename);
- info->format = g_strdup(bdrv_get_format_name(bs));
- info->virtual_size = total_sectors * 512;
- info->actual_size = bdrv_get_allocated_file_size(bs);
- info->has_actual_size = info->actual_size >= 0;
- if (bdrv_is_encrypted(bs)) {
- info->encrypted = true;
- info->has_encrypted = true;
- }
- if (bdrv_get_info(bs, &bdi) >= 0) {
- if (bdi.cluster_size != 0) {
- info->cluster_size = bdi.cluster_size;
- info->has_cluster_size = true;
- }
- info->dirty_flag = bdi.is_dirty;
- info->has_dirty_flag = true;
- }
- bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
- if (backing_filename[0] != '\0') {
- info->backing_filename = g_strdup(backing_filename);
- info->has_backing_filename = true;
- bdrv_get_full_backing_filename(bs, backing_filename2,
- sizeof(backing_filename2));
-
- if (strcmp(backing_filename, backing_filename2) != 0) {
- info->full_backing_filename =
- g_strdup(backing_filename2);
- info->has_full_backing_filename = true;
- }
-
- if (bs->backing_format[0]) {
- info->backing_filename_format = g_strdup(bs->backing_format);
- info->has_backing_filename_format = true;
- }
- }
-}
-
-static void dump_human_image_info(ImageInfo *info)
-{
- char size_buf[128], dsize_buf[128];
- if (!info->has_actual_size) {
- snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
- } else {
- get_human_readable_size(dsize_buf, sizeof(dsize_buf),
- info->actual_size);
- }
- get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
- printf("image: %s\n"
- "file format: %s\n"
- "virtual size: %s (%" PRId64 " bytes)\n"
- "disk size: %s\n",
- info->filename, info->format, size_buf,
- info->virtual_size,
- dsize_buf);
-
- if (info->has_encrypted && info->encrypted) {
- printf("encrypted: yes\n");
- }
-
- if (info->has_cluster_size) {
- printf("cluster_size: %" PRId64 "\n", info->cluster_size);
- }
-
- if (info->has_dirty_flag && info->dirty_flag) {
- printf("cleanly shut down: no\n");
- }
-
- if (info->has_backing_filename) {
- printf("backing file: %s", info->backing_filename);
- if (info->has_full_backing_filename) {
- printf(" (actual path: %s)", info->full_backing_filename);
- }
- putchar('\n');
- if (info->has_backing_filename_format) {
- printf("backing file format: %s\n", info->backing_filename_format);
- }
- }
-
- if (info->has_snapshots) {
- SnapshotInfoList *elem;
- char buf[256];
-
- printf("Snapshot list:\n");
- printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
-
- /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
- * we convert to the block layer's native QEMUSnapshotInfo for now.
- */
- for (elem = info->snapshots; elem; elem = elem->next) {
- QEMUSnapshotInfo sn = {
- .vm_state_size = elem->value->vm_state_size,
- .date_sec = elem->value->date_sec,
- .date_nsec = elem->value->date_nsec,
- .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
- elem->value->vm_clock_nsec,
- };
-
- pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
- pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
- printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
- }
- }
-}
-
static void dump_human_image_info_list(ImageInfoList *list)
{
ImageInfoList *elem;
}
delim = true;
- dump_human_image_info(elem->value);
+ bdrv_image_info_dump(fprintf, stdout, elem->value);
}
}
ImageInfoList *head = NULL;
ImageInfoList **last = &head;
GHashTable *filenames;
+ Error *err = NULL;
filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
goto err;
}
- info = g_new0(ImageInfo, 1);
- collect_image_info(bs, info, filename, fmt);
- collect_snapshots(bs, info);
+ bdrv_query_image_info(bs, &info, &err);
+ if (error_is_set(&err)) {
+ error_report("%s", error_get_pretty(err));
+ error_free(err);
+ goto err;
+ }
elem = g_new0(ImageInfoList, 1);
elem->value = info;
bs_old_backing = bdrv_new("old_backing");
bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
- ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS,
+ ret = bdrv_open(bs_old_backing, backing_name, NULL, BDRV_O_FLAGS,
old_backing_drv);
if (ret) {
error_report("Could not open old backing file '%s'", backing_name);
}
if (out_baseimg[0]) {
bs_new_backing = bdrv_new("new_backing");
- ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS,
+ ret = bdrv_open(bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS,
new_backing_drv);
if (ret) {
error_report("Could not open new backing file '%s'",