Exception initializing new VHD's partitions

Feb 6, 2013 at 8:49 PM
Edited Feb 7, 2013 at 7:17 PM
I'm receiving an exception when initializing vhds that I created.

The exception being thrown is an "Index out of bounds" in the following line of DynamicStream.cs.
bool readFromParent = (_blockBitmaps[block][sectorInBlock / 8] & mask) == 0;
The code I am using that produces this error is:
using (var f = new FileStream("filename.vhd", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
using ( var d = DiscUtils.Vhd.Disk.InitializeDynamic( f, Ownership.None, ( 4 * 1024 * 1024 * 1024), 2048))
DiscUtils.Partitions.BiosPartitionTable.Initialize( d ); // dies performing this line
In trying to hunt down the issue, I noticed that the DynamicStream constructor sets _blockBitmapSize in what looks to me like a very odd manner.
The line in question presently looks like this:
_blockBirmapSize = (int)Utilities.RoundUp((_dynamicHeader.BlockSize / Utilities.SectorSize) / 8, Utilities. SectorSize);
The sector size of the disk files I've been generating is always 512 bytes, so it looks to me that unless the block size of the vhd is set to at least 4096 (512 * 8), the first parameter to RoundUp() will always be 0.

If this is the desired behaviour, any ideas where else I might be going wrong?

Edit: For reference, I am using code current as of commit 14fd51607559.
Feb 10, 2013 at 2:21 PM
I agree, the code looks suspect - but that's a pretty unusual block size to be so small. From the VHD spec:
Note: Although the format supports varying block sizes, Microsoft Virtual PC 2004 and Virtual Server 2005 have only been tested with 512K and 2 MB block sizes.
Also seems that Windows 7 at least won't mount VHDs with a blocksize that isn't either 512KB or 2MB.

Anyhow, I'll take a look at tweaking that code.