Shit happens! Still today, hard disks are the Achille’s heel of modern computers. My corporate laptop hard disk started to give some bad signs of tiredness a few days ago. After a brief analyze, only one file was affected by two bad sector! But it was a VirtualBox disk image of 32GB! I performed a last full backup of my laptop before starting a re-install on a new disk and the backup failed on the VM image.
I tried via the simple tools ‘cp‘: ‘Input/Output error’. I tried via ‘tar‘: it fulfilled the rest of the file with ‘zero’! I tried via ‘dd‘, it skipped the bad blocks but concatenated the end of the file to the previous part (wtf!). The backup image crashed logically the guest operating system every time (with a nice BSOD). Then I tried a fork of ‘dd’ (suggested by a colleague, thanks Bart! ;-)) called ‘dd_rescue‘. Like the classic ‘dd’, it performs copies of data files or block devices but interesting options were added by the author:
- It will abort only after x errors (and x can be specified via the command line)
- It will truncate the destination file only if specified (by default, no)
- It can replace by data blocks with “zero”
- and more…
To backup my VM to a external disk, I used the following command:
# dd_rescue -v -A /data/virtualbox/HardDisks/disk.vdi /mnt/backup/disk.vdi
Then, I successfully booted my virtual machine! Honestly, I was lucky: The zero’ed blocks did not belong to critical files. Once started, I used another nice tool called MyDefrag (previously know as JkDefrag). This is a freeware disk defragmentation tool which proposes different types of operations.Â As the bad blocks affected my disk image around the 31GB (on a total of 32), I used the one which moves all the data at the beginning of the disk. The operation was successful.
If you have a corrupted file system or disk image, give a try to dd_rescue which should be part of all system administrator’s emergency toolbox! Of course, this will never replace a good backup policy! I had a copy of all my data but I preferred to try a recovery from the original disk images (it’s so boring to reinstall all the stuff!). If you urgently need to rebuild your system, a restore will always be the fastest solution.