Wednesday, August 22, 2012

How to check media integrity in Linux

It's a good idea check the integrity of media made from ISO images. It's possible to do this using only two simple tools: dd and sha1sum. If you want, you can use another hash algorithm, like md5sum, sha256sum or sha512sum.

The examples in this article were made in a Linux environment.

First, you need to know the hash of the ISO image. In the following example, I used the sha1sum tool to get the sha1sum hash of the ISO image openSUSE-12.1-GNOME-LiveCD-x86_64.iso:

rafael@rafael:~/rafael/tmp> sha1sum openSUSE-12.1-GNOME-LiveCD-x86_64.iso
4922fc8d380fd688825954026f72655fc42bd233  openSUSE-12.1-GNOME-LiveCD-x86_64.iso

Now you know the hash of the ISO image: 4922fc8d380fd688825954026f72655fc42bd233. When I downloaded the image from the openSUSE's official torrent, I checked in the openSUSE's official site if its hash is equal to the downloaded ISO image hash in my computer. And I confirmmed that it is (the hash is in the file http://download.opensuse.org/distribution/12.1/iso/openSUSE-12.1-GNOME-LiveCD-x86_64.iso.sha1).

And now, you need to know the size in bytes of the ISO image. You can see this through the ls -l command:

rafael@rafael:~/rafael/tmp> ls -l
total 691204
-rw-r--r-- 1 rafael users 707788800 Aug 21 18:23 openSUSE-12.1-GNOME-LiveCD-x86_64.iso

Now you know that the ISO image size is 707788800 bytes. To get the hash of the media part that contains the ISO image, you need to run the following command putting the ISO image size value after the count argument:

rafael@rafael:~/rafael/tmp> dd if=/dev/cdrom bs=1 count=707788800 | sha1sum
707788800+0 records in
707788800+0 records out
707788800 bytes (708 MB) copied, 724.078 s, 978 kB/s
4922fc8d380fd688825954026f72655fc42bd233  -

You could speed this up by using a larger block size (bs) and dividing count by the new block size. Since all iso images are multiples of 2048, that is an appropriate block size.

And there is the gold, in the last line: the hash of the ISO image on the media. If this hash is equal to the hash generated by the hash tool on the ISO image, the media should be OK to be used. If it's not, it's recommended record again the media, until both hashes be equal.

It's necessary hash only the size of the ISO image in the media because a hash of the entire media will return a different value because it will contain the empty space at the end of the disk.

No comments:

Post a Comment