A Cautionary Tale
Here’s a cautionary tale for all you folks.
So I was going along, happily minding my own business, debianizing/defenestrating a few new computers, when I needed to make root/rescue disks. I went to my laptop, got the dd images and typed:
dd if=root.bin of=/dev/hda bs=1024 conv=sync
Boom. That was fast. And the floppy didn’t even spin. Hm…
Now wait a minute—that’s /dev/hda, not /dev/fd0! That’s ME. But I wasn’t root???
~> ls -l /dev/hda brw-rw---- 1 root disk 3, 0 Mar 14 2002 /dev/hda ~> groups ...disk...
Uh oh. I suddenly have the feeling of someone you see in the movies where the torso has been severed but they don’t feel it yet. I just overwrote the first 1024K of my hard drive with root.bin.
But everything was still working fine, for the moment. I had overwritten my partition table and part of my hibernation partition, but none of my actual linux drive.
So I foundered about for a bit, desperately not wanting to have to back up my whole drive and start over. I called my friend Dylan, woke him up (you never know with mathematicians!), and he gave me some very good ideas.
It turns out my partition table was still in memory, in /proc/partitions.
cfdisk /dev/hda failed (fatal error), but it turns out I could still run fdisk /dev/hda.
So I manually recreated the partition table with fdisk from /proc/partitions (set the partition types), wrote it to disk, reinstalled grub, rebooted and crossed my fingers.
Back to normal!
Try recovering from such a disaster under Windows, and I’ll see you next year.
A few lessons to be learned:
- Don’t put yourself in group disk! This might be obvious to some of you, but I had added myself in order to burn CDs (I should have created a different group, didn’t think about it at the time).
- Save a copy of your partition table! I was lucky based on what I did that the partition table was still in /proc, but googling suggested that this is sometimes an invaluable recovery tool. Put your partition table somewhere else. There is a tool gpart that tries to guess your partition table based on the data, but having the information just makes it so much easier.
- Don’t dd over your hard drive.
I guess that’s it. I hope my averted disaster is helpful to some of you.