Thursday, May 26, 2011

VHD Corruption - Not cool, UGH.

Quick description of my setup... I do my dev work in VMs, in this particular case I have a Win7 host with my VM file on a Bitlocker partition.  I'm using Oracle VirtualBox to access a Win7 guest, that has like ~140GB dynamic drive, ~actual size 40GB.

While restoring a DB in SSMS 2008, the VM drive grows with the restore and fills the host drive.  Of course SSMS and the guest Win7 doesn't report being out of space, b/c technically the VM drive isn't out of space.  VirtualBox doesn't say it's out of space... why?  That's a good question, but if I could have saved a lot of grief if it threw up something like "Hey your VM is trying to write to the drive and it's out of space.  Your VM has been paused.  Retry?  Abort?"

Anyhow, VirtualBox along with everything else on my box is halting "This process is not responding..."

Not cool.  UGH.  More digging, I kill the VirtualBox task.  Restart VirtualBox.  Error with drive:

Failed to open the hard disk D:\abcde.vhd.
Could not get teh storage format of the medium 'D:\abcde.vhd' (VERR_NOT_SUPPORTED).
Result Code:
 VBOX_E_IPRT_ERROR (0x80BB0005)

Component:
 Medium

Interface:
 IMedium {1d578f43-5ef1-4415-b556-7592d3ccdc8f}

Callee:
 IVirtualBox {3f36e024-7fed-4f20-a02c-9158a82b44e6}



Immediately copy the broken VHD to an external drive.

Not cool.  UGH.  More digging, I try mounting the VHD in Win7...Start, Computer Management, Disk Management, Action, Attach VHD, and I get this:

The file or directory is corrupted and unreadable.

Not cool.  UGH.  More digging, I found a post about using WinImage (http://www.winimage.com/) to read the VHD.  So I open it in WinImage and woohoo it opened... except half my data is missing.  The C:\Users folder isn't even listed.

Not cool.  UGH.  More digging, I came across a bunch of other posts and disregarded them, then later wanted to go back to it but couldn't find them.  The gist of it was that a person fixed their corrupted header on their dynamic VHD by using a utility to convert it to a fixed size VHD.  The result should be that my 120GB dynamic VHD would result in a 40GB fixed size VHD.  They had a link to VHDTool (http://archive.msdn.microsoft.com/vhdtool) so I figured I'd give it a whirl.  Data should be intact and just have a new header.

Run the utility to convert to fixed size, seems ok, open in WinImage, files still there.  Mount in Win7, error.  VHD looks like one big blob of unformatted space.  Try VirtualBox... the VHD mounts, but it won't boot.  Makes sense.  It's now a fixed size VHD with 0 free bytes.  Mount to a diff VM as a secondary drive.  Boot, OS says the secondary drive needs to be formatted.

Not cool.  UGH.  More digging, but nothing new to try yet.  So I figure the VHD has a new header now, and I can see my data in WinImage... So I'll just try converting back to a dynamic drive again and see if I can then boot.

Guess what I learned.   WinImage has menus!  And under those menus are various little neat tricks... like.... Disk, Convert VHD image...

I convert the new fixed size VHD, to a dynamic VHD, WinImage shows the new data, but still missing C:\Users.  I try mounting in Win7, drive mounts!  VirtualBox media manager opens the VHD!  Boot the Win7 OS..... SUCCESS!!!! :)

Looking back, WinImage was able to see the contents of the original corrupted VHD, so maybe I could have used it to open the corrupt VHD and then immediately re-write it back out, without having to use the Microsoft tool.  If the VHD corrupts a 2nd time, then I'll try that ;)

Every blog needs a first post.

As a programmer, I *know* I should be contributing back to the community, but it's always been on the back burner.  I also *know* that I, like many other programmers, do not enjoy writing documentation.  Anyhow, I thought my most recent situation is worth documenting because wow I was in quite a pinch.  So... I guess it's time to start a blog!