Fixing a Virtual Box Hard Drive Image (VDI)

I can’t believe that I’ve managed it! I recently managed to corrupt one of the virtual hard drive images on a Virtual Box image. Everytime I started the image up, it complained of problems with the Headers in the VDI file. It appears that the image file (not the guest HDD partition) had become corrupted.

This created a complete disaster for me. Unfortunately, I had been following my mantra of “Backups are for whimps” and I had potentially lost a load of work. I did have a back-up of the file going back over a month, but that was too far in the past for me. Seeing as I was convinced it was a problem with the headers in the VDI file, and that the guest partition in the file was intact, I was sure that I could correct it.

So, having spoken to a VMWare Guru at work (thanks Chris!) who convinced me he had performed a similar trick on a VMWare image, I set about correcting the image.

Firstly, I went digging. Google throws up a few instances of this problem, but most people saying it is not possible to repair the file. Fortunately, I came across this forum post which details the format of the VDI file.

Secondly, and importantly, I took a copy of the corrupted file for safe-keeping.

Thirdly, I restored my month old backup, and opened it and the corrupted file in a hex-editor. Comparing the reference material on the forum post, I noticed a few small differences, but mostly it seemed to line up after a few dozen bytes.

So, comparing the reference data, and the restored backup, I worked on the corrupted file to aline the data to what looked correct.

I saved the file, and opened up Virtual Box.

Huzzah! It worked. Restarting the VM worked well – there was a quite blip of the Linux guest complaining about an EXT4 block.

So, now it’s time for me to take another backup and then to run fsck on the guest partition!

Sometimes detaching the boot volume image, attaching it to another machine (try as master or slave) and then attaching it back to the original virtual machine helps too and the information in the header may get fixed. I had a situation when it told me the boot medium is damaged (blue screen), but I was still able to fix it this way.
I find the settings and copy/clone tools in Virtual Box a bit confusing. It’s not difficult once it’s clear how things work but if making backups/copies/clones was easier and from one single place in Virtual Box, it doing backups would be just fun to do. Or perhaps a matter of scheduling the backup to run every 10 days or 10 times I open the virtual machine or something like that.