Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a utility (or some shell magic) that allows me to compare two block devices?

Details: I have one large (0.5 TB) RAID device that I've backed up to a slightly larger SATA device using dd. The device has several partitions on it, however I copy from the 'parent' device and not the partitioned device nodes (e.g., I copy /dev/hda and not /dev/hda1 for example).

I would like to verify that backup is good/correct.

I have thought about running md5sum across each device, however that won't give me an accurate result as the second device is slightly larger than the first and thus the extra bytes will change the hash.

share|improve this question

4 Answers 4

up vote 3 down vote accepted

I would use the command

cmp /dev/hda /dev/hdb

it will stop at the first differing byte, writing out its offset, or on EOF on the shorter one.

share|improve this answer
    
I don't see in the manpage anything about stopping at EOF, is that documented somewhere? –  Kevin Jan 24 '12 at 18:54
    
In fact, the exit status says: "1: The files are different; this includes the case where one file is identical to the first part of the other." This seems to imply that it does not report them as identical if one is shorter. –  Kevin Jan 24 '12 at 18:56
    
@Kevin: from info cmp: "The 'cmp' command compares two files, and if they differ, tells the first byte and line number where they differ." –  enzotib Jan 24 '12 at 20:47
    
You can use the --bytes=LIMIT option, setting the LIMIT value to the bytesize of the smaller device to stop at the end of the smaller drive (or partition boundary) without having cmp exit with non-zero status. –  rozcietrzewiacz Oct 30 '12 at 9:26

Depending on how full the drives actually are, it may be fastest to mount the file systems and do

diff -qr /mnt/root1 /mnt/root2

Otherwise, I would say something like this:

diff -q <(dd bs=1M count=500K if=/dev/sda) <(dd bs=1M count=500K if=/dev/sdb)

Obviously figure out just how big you need to make bs and count to stop at (or just before if it's much more convenient) the end of the smaller drive.

Or, thanks to enzotib's comment, it looks like you could use cmp, but you'll need the -n flag to limit the number of bytes to the shorter of the two drives.

cmp /dev/sda /dev/sdb -n 500GB
share|improve this answer
    
Unfortunately I'm unable to mount the device as its an NTFS volume, and I have a LiveCD without the right drivers on it. For the second command the subshells you use don't try to suck the whole device into memory before feeding it into diff? –  alanxz Jan 24 '12 at 18:42
1  
@alanxz No, the diff subshells will not read the entire device before writing it. dd writes bs bytes immediately after reading it (or processing if specified, not here), and they write to fifos, which block until they're read. –  Kevin Jan 24 '12 at 18:52
    
I was thinking more at the shell level (or whatever implements the FIFO). But that answers my question! –  alanxz Jan 24 '12 at 18:55

You are looking for binary diff software

rdiff
is designed for backup and network transmition, look at rdiff-backup and duplicity for backup propuse

xdelta3
is an open-source binary diff, differential compression tools, VCDIFF (RFC 3284) delta compression.

bsdiff
is too!! memory hungry, is not suitable for block devices.

share|improve this answer

There is an utility called bsdiff, which does exactly this kind of thing. It does it really fast, and it's able to generate a binary patch, if you need.

See this post for some explanation around it.

share|improve this answer
    
bsdiff's job is more complex than simply compare two files, so I doubt it can be faster than the simpler cmp. –  enzotib Jan 24 '12 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.