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.

I have an ISO file, which I burned to a CD. Now how can I check if the CD is correctly created? I would like a command that calculate the hash sum that I can use to check with the hash sum I calculate on the ISO file. Ideally the command should:

  • Work regardless of the ISO file: that is, I don't want to keep a list of hash sum for each file in the disc, or remember the number of blocks whatever
  • Be relatively short: a one-line command is great, a chain of commands which is two line long is OK, a script that span one page is not
  • Be fairly efficient: for example, dd the disc back to a file then run md5sum on the file is unacceptable

If there is no answer that cannot satisfy all I will appreciate the nearest match too. Even better if you can tell me why it is not so straight-forward.

share|improve this question
add comment

1 Answer

up vote 12 down vote accepted

The basic problem is that we want to take the md5sum of the exact same information that was on the ISO originally. When you write the ISO to a CD, there is likely blank space on the end of the disk, which inevitably changes the md5sum. Thus, the the very shortest way:

md5sum /dev/cdrom

doesn't work. What does work (and is common in online documentation) is only reading the exact number of bytes from the device and then doing the md5sum. If you know the number of bytes you can do something like:

dd if=/dev/cdrom bs=1 count=xxxxxxxx | md5sum

where 'xxxxx' is the size of the iso in bytes.

If you don't know the number of bytes off hand, but have the iso on your disk still, you can get them using ls by doing the something like the following (taken from here):

dd if=/dev/cdrom | head -c `stat --format=%s file.iso` | md5sum

There are many other one-line constructions that should work. Notice that in each case we are using dd to read the bytes from the disk, but we aren't piping these to a file, rather, we are handing them to md5sum straight away.

Possible speed improvements can be made by doing some calculations to use a bigger block size (the bs= in the dd command).

share|improve this answer
    
You can simplify dd if=/dev/cdrom | to </dev/cdrom. There's nothing magical about dd and block devices, it's just a stream manipulation command with a funny syntax. –  Gilles Nov 5 '10 at 18:26
    
By the way, you (phunehehe) should have the iso size together with the MD5 checksum. This is particularly important for MD5 which is partially broken, but it's a good idea for both security and usability no matter what checksum you use. –  Gilles Nov 5 '10 at 18:34
    
@Gilles: actually, using dd with bs of the device cache size can speedup data transfer as opposed to plain input redirection. –  alex Nov 6 '10 at 4:51
    
@Gilles: what do you mean by "MD5 which is partially broken"? And why should I keep the file size, when if something is wrong the checksum should tell me right away? –  phunehehe Nov 6 '10 at 10:15
    
@phunehehe: There are known cryptographic weaknesses with MD5 (it is not (publicly) known how to generate a collision for a given file, but it could come). Knowing the file size gives you a little more confidence (though not much more). It also gives you immediate feedback for common accidental errors such as truncated downloads. –  Gilles Nov 6 '10 at 11:56
show 2 more comments

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.