Graphical differences between two disk images

I was investigating possible disk corruption when copying a disk image between servers, but needed a way to visualise what might be happening. The disk image is tens of gigabytes, so looking at it in hexdump wasn’t a lot of fun. A little Python to the rescue instead:

#!/usr/bin/python3

from PIL import Image, ImageColor

import nbd
h1 = nbd.NBD()
h2 = nbd.NBD()

original = "original.qcow2";
copied = "copied.qcow2";

blksize = 4096
zero_block = bytearray(blksize)

width = 4096
output = "output.png"
red = ImageColor.getrgb("red")
green = ImageColor.getrgb("green")
blue = ImageColor.getrgb("blue")
white = ImageColor.getrgb("white")
black = ImageColor.getrgb("black")

# Open original disk image.
h1.connect_systemd_socket_activation(["qemu-nbd", "-f", "qcow2", original])

# Open copied disk image.
h2.connect_systemd_socket_activation(["qemu-nbd", "-f", "qcow2", copied])

assert(h1.get_size() <= h2.get_size())

# Open the output visualisation.
height = int(h1.get_size() / blksize / width) + 1
image = Image.new("RGB", (width, height), white)

# Iterate over the blocks of each.
x = 0
y = 0
for i in range(0, h1.get_size(), blksize):
    b1 = h1.pread(blksize, i)
    b2 = h2.pread(blksize, i)
    if b1 == b2:
        # Same
        image.putpixel((x, y), green)
    elif b1 != zero_block and b2 == zero_block:
        # Zeroed
        image.putpixel((x, y), black)
    else:
        # Different but not zeroed
        image.putpixel((x, y), red)
    x = x+1
    if x == width:
        x = 0
        y = y+1
        print("%d/%d\r" % (y, height), end='')

print()
image.save(output)
print("Saved to %s" % output)

h1.close()
h2.close()

The resulting image showed that stretches of the disk were getting zeroed out during the copy (which was definitely not supposed to happen).

5 Comments

Filed under Uncategorized

5 responses to “Graphical differences between two disk images

  1. Lakshmipathi.G's avatar Lakshmipathi.G

    Nice, but I was looking for a screenshot of that image too 🙂

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.