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(); var reader = new VhdDataReader(new BinaryReader(new MemoryStream(byteArray))); var footerFactory = new VhdFooterFactory(reader); var vhdFooter = footerFactory.CreateFooter(); return(vhdFooter); }
private VhdFile Create(Stream stream, string vhdDirectory) { var reader = new BinaryReader(stream, Encoding.Unicode); try { 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) { var parentPath = vhdDirectory == null ? header.ParentPath : Path.Combine(vhdDirectory, header.GetRelativeParentPath()); parent = Create(parentPath); } } return(new VhdFile(footer, header, blockAllocationTable, parent, stream)); } catch (EndOfStreamException) { throw new VhdParsingException("unsupported format"); } }
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) { var parentPath = streamSource.VhdDirectory == null ? header.ParentPath : Path.Combine(streamSource.VhdDirectory, header.GetRelativeParentPath()); 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(); } } }
private IEnumerable <CompletionPort> CreateAsync(AsyncMachine <VhdFile> machine, StreamSource streamSource) { var reader = new BinaryReader(streamSource.Stream, Encoding.Unicode); var dataReader = new VhdDataReader(reader); var footerFactory = new VhdFooterFactory(dataReader); footerFactory.BeginCreateFooter(machine.CompletionCallback, null); yield return(CompletionPort.SingleOperation); var footer = TryCatch <VhdFooter>(footerFactory.EndCreateFooter, machine.CompletionResult); VhdHeader header = null; BlockAllocationTable blockAllocationTable = null; VhdFile parent = null; if (footer.DiskType != DiskType.Fixed) { var headerFactory = new VhdHeaderFactory(dataReader, footer); headerFactory.BeginCreateHeader(machine.CompletionCallback, null); yield return(CompletionPort.SingleOperation); header = TryCatch <VhdHeader>(headerFactory.EndCreateHeader, machine.CompletionResult); var tableFactory = new BlockAllocationTableFactory(dataReader, header); tableFactory.BeginCreate(machine.CompletionCallback, null); yield return(CompletionPort.SingleOperation); blockAllocationTable = TryCatch <BlockAllocationTable>(tableFactory.EndCreate, machine.CompletionResult); if (footer.DiskType == DiskType.Differencing) { var parentPath = streamSource.VhdDirectory == null ? header.ParentPath : Path.Combine(streamSource.VhdDirectory, header.GetRelativeParentPath()); BeginCreate(parentPath, machine.CompletionCallback, null); yield return(CompletionPort.SingleOperation); parent = EndCreate(machine.CompletionResult); } } machine.ParameterValue = new VhdFile(footer, header, blockAllocationTable, parent, streamSource.Stream); }