public static VhdFooter CreateFixedDiskFooter(long virtualSize) { var helper = new AttributeHelper <VhdFooter>(); var footer = new VhdFooter(); var reservedAttribute = helper.GetAttribute(() => footer.Reserved); footer.Cookie = VhdCookie.CreateFooterCookie(); footer.Features = VhdFeature.Reserved; footer.FileFormatVersion = VhdFileFormatVersion.DefaultFileFormatVersion; footer.HeaderOffset = VhdConstants.VHD_NO_DATA_LONG; footer.TimeStamp = DateTime.UtcNow; footer.CreatorApplication = WindowsAzureCreatorApplicationName; footer.CreatorVersion = VhdCreatorVersion.CSUP2011; footer.CreatorHostOsType = HostOsType.Windows; footer.PhsyicalSize = virtualSize; footer.VirtualSize = virtualSize; footer.DiskGeometry = DiskGeometry.CreateFromVirtualSize(virtualSize); footer.DiskType = DiskType.Fixed; footer.UniqueId = Guid.NewGuid(); footer.SavedState = false; footer.Reserved = new byte[reservedAttribute.Size]; var footerSerializer = new VhdFooterSerializer(footer); var byteArray = footerSerializer.ToByteArray(); using (var memoryStream = new MemoryStream(byteArray)) { var binaryReader = new BinaryReader(memoryStream); var vhdDataReader = new VhdDataReader(binaryReader); var footerFactory = new VhdFooterFactory(vhdDataReader); var vhdFooter = footerFactory.CreateFooter(); return(vhdFooter); } }
public static void CreateFixedVhdFile(Stream destination, long virtualSize) { var footer = VhdFooterFactory.CreateFixedDiskFooter(virtualSize); var serializer = new VhdFooterSerializer(footer); var buffer = serializer.ToByteArray(); destination.SetLength(virtualSize + VhdConstants.VHD_FOOTER_SIZE); destination.Seek(-VhdConstants.VHD_FOOTER_SIZE, SeekOrigin.End); destination.Write(buffer, 0, buffer.Length); destination.Flush(); }
private static IEnumerable <CompletionPort> CreateFixedVhdFileAtAsync(AsyncMachine machine, Stream destination, long virtualSize) { var footer = VhdFooterFactory.CreateFixedDiskFooter(virtualSize); var serializer = new VhdFooterSerializer(footer); var buffer = serializer.ToByteArray(); destination.SetLength(virtualSize + VhdConstants.VHD_FOOTER_SIZE); destination.Seek(-VhdConstants.VHD_FOOTER_SIZE, SeekOrigin.End); destination.BeginWrite(buffer, 0, buffer.Length, machine.CompletionCallback, null); yield return(CompletionPort.SingleOperation); destination.EndWrite(machine.CompletionResult); destination.Flush(); }
private VhdFile Create(StreamSource streamSource) { var disposer = new Action(() => { if (streamSource.DisposeOnException) { streamSource.Stream.Dispose(); } }); bool throwing = false; try { var reader = new BinaryReader(streamSource.Stream, Encoding.Unicode); var dataReader = new VhdDataReader(reader); var footer = new VhdFooterFactory(dataReader).CreateFooter(); VhdHeader header = null; BlockAllocationTable blockAllocationTable = null; VhdFile parent = null; if (footer.DiskType != DiskType.Fixed) { header = new VhdHeaderFactory(dataReader, footer).CreateHeader(); blockAllocationTable = new BlockAllocationTableFactory(dataReader, header).Create(); if (footer.DiskType == DiskType.Differencing) { string parentPath = GetParentPath(streamSource.VhdDirectory, header); parent = Create(parentPath); } } return(new VhdFile(footer, header, blockAllocationTable, parent, streamSource.Stream)); } catch (Exception e) { throwing = true; throw new VhdParsingException("unsupported format", e); } finally { if (throwing) { disposer(); } } }