Skip to content

Instantly share code, notes, and snippets.

@gboudreau
Last active February 29, 2016 15:24
Show Gist options
  • Save gboudreau/219d90e30acad5131947 to your computer and use it in GitHub Desktop.
Save gboudreau/219d90e30acad5131947 to your computer and use it in GitHub Desktop.
Calculate the MD5 checksum of a file over and over, clearing the disk cache between each read, to try to identify read errors that are happening on a hard drive. Ref: https://www.pommepause.com/2016/02/the-case-of-the-dying-hard-drive-that-flipped-bits/
<?php
$file = '/mnt/hdd5/persistent-app-data/downloading/vvgoor1586DSFGKL.part10.rar';
while (TRUE) {
exec("sync ; sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'");
$data1 = file_get_contents($file);
exec("sync ; sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'");
$data2 = file_get_contents($file);
$md5_data1 = md5($data1);
$md5_data2 = md5($data2);
if ($md5_data1 != $md5_data2) {
echo "First read MD5: $md5_data1\n";
echo "Second read MD5: $md5_data2\n";
for ($i=0; $i<strlen($data1); $i++) {
if ($data1[$i] != $data2[$i]) {
echo "Wrong byte at pos $i: 0x" . dechex(ord($data1[$i])) . " vs 0x" . dechex(ord($data2[$i])) . "\n";
}
}
} else {
echo "OK\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment