0

I am creating a dynamic vhd for a disk having multiple partitions.

The steps involved are

  1. First i create a VHD by creating header and footer and calculating bat(Block Allocation Table) size.

  2. Then i get disk size and partition details using win32 API

  3. I take snapshots of volumes

  4. I read mbr of parent disk and overwrite sector offsets then I write the mbr with multiple partitions to VHD.

  5. Now for each shadow copy i read snapshot and write to VHD file

  6. This process completes successfully and then i open the vhd in diskmgmt.msc .It shows both partitions and i can see the data in those partitions.

  7. Now i copy this vhd to hyper V machine and then create a new vm and add this vhd to that machine.

  8. When it starts it gives me an error "Disk read error" but when i add this vhd to an existing OS and add boot entry in the boot loader it boots fine.

I dont know what is going wrong.If any one came give me some pointers.

Here is the code to write mbr

/* Stream out the first data block which contains only the MBR */

   BYTE zerosector[512] = {0};

  zerosector[0] = 0x80;

  assert(0 != WriteFile(hFile, zerosector, 512, &nWritten, NULL));  /* here goes the  first block's block bitmap */

  zerosector[0] = 0x00;


 unint16 mcount = 0;
 unint64 indexOffset = 0;

 memcpy(&mbr,mbrbuff,512);
 for(vector<PartOffset*>::iterator pTemp = pPart.begin();pTemp!=pPart.end();pTemp++)
 {

   mbr.partition[mcount].type          = 0x07;
   mbr.partition[mcount].start_sector  = 4096 + indexOffset ;
   mbr.signature                       = 0xAA55;
   mbr.partition[mcount].total_sectors =  (((*pTemp)->total_sectors)) + 1 ; 
   indexOffset +=  ((((*pTemp)->total_sectors))) + 1;
   //if(mcount==0)
     //  indexOffset +=  ((((*pTemp)->start_sector)/512) );
   mcount++;

 }

Thanks

singh
  • 439
  • 1
  • 4
  • 20

1 Answers1

0

DiscUtils provides a reference for accessing and writing .VHD files compatible with Hyper-V

Have a look at how it builds up a VHD starting with the DiskFactory

Donal Lafferty
  • 5,807
  • 7
  • 43
  • 60