Here I will go through a number of ways that you can possibly repair the dreaded corrupt Hyper V VHD if you have not got the option of restoring from backup or you do not actually backup ( if you don’t have a product I recommend Unitrends Hyper V Backup ), symptoms may include the Hyper V virtual machine will not boot and when you try and attach the VHD you receive the error below.
Failed to open attachment Error: 'The file or directory is corrupted and unreadable
How Can I Fix A Corrupt VHD
Checking The Integrity Of A VHD
Sometimes the most simple of fixes are the best, we can run a script in powershell that allows us to mount the VHD in a the root folder this will enable us to run a CHKDSK on it. Run the power shell commands below changing your details
#First we choose what VHD we want mounted $VHDName = "V:\serverx.vhd" #Get the MSVM_ImageManagementService $VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "." #Now we mount the VHD $Result = $VHDService.Mount($VHDName)
We can now run a CHKDSK on this VHD.
Compacting The Corrupt VHD
Another method is to create a new VM and add the corrupt VHD but we then compact the VHD as below.
First create the new Virtual Server and add a new hard disk, then select settings and the hard disk you have created, then browse to the VHD that will not load. Select the Virtual Disk Wizard, then select compact and make sure you select Convert it to a fixed-size virtual hard disk, if you have a VHD that was 50gig it will compact the drive to the size the the amount of data that is on the VHD, so if you have 10 gig of files then the VHD will be compacted to 10 gig. You will get the option to save the new VHD as a new one so make sure you give it a different name in case you need to go back to the corrupt VHD.
Once compacted copy the VHD back to the original server and try and mount it.
Use Microsoft’s VHD Tool
This VHD Tool from Microsoft has the option to repair a VHD, first download the vhdtool here direct from Techieshelp then run the following command to attempt to repair the VHD.
(as always make sure you have a backup of the VHD before starting this process.
VhdTool.exe /repair <BaseVhdFileName> <FirstSnapshotAVhdFileName> [/quiet]
If you have an other methods then please comment below.