< Previous | Contents | Next >
Extra Credit
It’s often useful to verify the integrity of an iso image that we have downloaded. In most cases, a distributor of an iso image will also supply a checksum file. A checksum is the re- sult of an exotic mathematical calculation resulting in a number that represents the con- tent of the target file. If the contents of the file change by even one bit, the resulting checksum will be much different. The most common method of checksum generation uses the md5sum program. When you use md5sum, it produces a unique hexadecimal number:
md5sum image.iso
34e354760f9bb7fbf85c96f6a3f94ece image.iso
md5sum image.iso
34e354760f9bb7fbf85c96f6a3f94ece image.iso
After you download an image, you should run md5sum against it and compare the results with the md5sum value supplied by the publisher.
In addition to checking the integrity of a downloaded file, we can use md5sum to verify newly written optical media. To do this, we first calculate the checksum of the image file and then calculate a checksum for the media. The trick to verifying the media is to limit the calculation to only the portion of the optical media that contains the image. We do this by determining the number of 2048 byte blocks the image contains (optical media is al- ways written in 2048 byte blocks) and reading that many blocks from the media. On some types of media, this is not required. A CD-R written in disc-at-once mode can be checked this way:
md5sum /dev/cdrom
34e354760f9bb7fbf85c96f6a3f94ece /dev/cdrom
md5sum /dev/cdrom
34e354760f9bb7fbf85c96f6a3f94ece /dev/cdrom
Many types of media, such as DVDs, require a precise calculation of the number of
Extra Credit
blocks. In the example below, we check the integrity of the image file dvd-image.iso
and the disc in the DVD reader /dev/dvd. Can you figure out how this works?
md5sum dvd-image.iso; dd if=/dev/dvd bs=2048 count=$(( $(stat -c "%s" dvd-image.iso) / 2048 )) | md5sum
md5sum dvd-image.iso; dd if=/dev/dvd bs=2048 count=$(( $(stat -c "%s" dvd-image.iso) / 2048 )) | md5sum