I am creating a dynamic vhd for a disk having multiple partitions.
The steps involved are
First i create a VHD by creating header and footer and calculating bat(Block Allocation Table) size.
Then i get disk size and partition details using win32 API
I take snapshots of volumes
I read mbr of parent disk and overwrite sector offsets then I write the mbr with multiple partitions to VHD.
Now for each shadow copy i read snapshot and write to VHD file
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.
Now i copy this vhd to hyper V machine and then create a new vm and add this vhd to that machine.
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