/// <summary> /// Initiates the build process. /// </summary> /// <param name="baseName">The base name for the VMDK, for example 'foo' to create 'foo.vmdk'.</param> /// <returns>A set of one or more logical files that constitute the VMDK. The first file is /// the 'primary' file that is normally attached to VMs.</returns> public override DiskImageFileSpecification[] Build(string baseName) { if (string.IsNullOrEmpty(baseName)) { throw new ArgumentException("Invalid base file name", "baseName"); } if (Content == null) { throw new InvalidOperationException("No content stream specified"); } if (_diskType != DiskCreateType.Vmfs && _diskType != DiskCreateType.VmfsSparse) { throw new NotImplementedException("Only Vmfs and VmfsSparse disks implemented"); } List <DiskImageFileSpecification> fileSpecs = new List <DiskImageFileSpecification>(); Geometry geometry = Geometry ?? DiskImageFile.DefaultGeometry(Content.Length); Geometry biosGeometry = BiosGeometry ?? Geometry.LbaAssistedBiosGeometry(Content.Length); DescriptorFile baseDescriptor = DiskImageFile.CreateSimpleDiskDescriptor(geometry, biosGeometry, _diskType, _adapterType); if (_diskType == DiskCreateType.Vmfs) { ExtentDescriptor extent = new ExtentDescriptor(ExtentAccess.ReadWrite, Content.Length / 512, ExtentType.Vmfs, baseName + "-flat.vmdk", 0); baseDescriptor.Extents.Add(extent); MemoryStream ms = new MemoryStream(); baseDescriptor.Write(ms); fileSpecs.Add(new DiskImageFileSpecification(baseName + ".vmdk", new PassthroughStreamBuilder(ms))); fileSpecs.Add(new DiskImageFileSpecification(baseName + "-flat.vmdk", new PassthroughStreamBuilder(Content))); } else if (_diskType == DiskCreateType.VmfsSparse) { ExtentDescriptor extent = new ExtentDescriptor(ExtentAccess.ReadWrite, Content.Length / 512, ExtentType.VmfsSparse, baseName + "-sparse.vmdk", 0); baseDescriptor.Extents.Add(extent); MemoryStream ms = new MemoryStream(); baseDescriptor.Write(ms); fileSpecs.Add(new DiskImageFileSpecification(baseName + ".vmdk", new PassthroughStreamBuilder(ms))); fileSpecs.Add(new DiskImageFileSpecification(baseName + "-sparse.vmdk", new VmfsSparseExtentBuilder(Content))); } return(fileSpecs.ToArray()); }
private void LoadDescriptor(Stream s) { s.Position = 0; byte[] header = Utilities.ReadFully(s, (int)Math.Min(Sizes.Sector, s.Length)); if (header.Length < Sizes.Sector || Utilities.ToUInt32LittleEndian(header, 0) != HostedSparseExtentHeader.VmdkMagicNumber) { s.Position = 0; _descriptor = new DescriptorFile(s); if (_access != FileAccess.Read) { _descriptor.ContentId = (uint)_rng.Next(); s.Position = 0; _descriptor.Write(s); s.SetLength(s.Position); } } else { // This is a sparse disk extent, hopefully with embedded descriptor... HostedSparseExtentHeader hdr = HostedSparseExtentHeader.Read(header, 0); if (hdr.DescriptorOffset != 0) { Stream descriptorStream = new SubStream(s, hdr.DescriptorOffset * Sizes.Sector, hdr.DescriptorSize * Sizes.Sector); _descriptor = new DescriptorFile(descriptorStream); if (_access != FileAccess.Read) { _descriptor.ContentId = (uint)_rng.Next(); descriptorStream.Position = 0; _descriptor.Write(descriptorStream); byte[] blank = new byte[descriptorStream.Length - descriptorStream.Position]; descriptorStream.Write(blank, 0, blank.Length); } } } }
private static DiskImageFile DoInitialize(FileLocator fileLocator, string file, long capacity, DiskCreateType type, DescriptorFile baseDescriptor) { if (type == DiskCreateType.MonolithicSparse) { // MonolithicSparse is a special case, the descriptor is embedded in the file itself... using (Stream fs = fileLocator.Open(file, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { long descriptorStart; CreateExtent(fs, capacity, ExtentType.Sparse, 10 * Sizes.OneKiB, out descriptorStart); ExtentDescriptor extent = new ExtentDescriptor(ExtentAccess.ReadWrite, capacity / Sizes.Sector, ExtentType.Sparse, file, 0); fs.Position = descriptorStart * Sizes.Sector; baseDescriptor.Extents.Add(extent); baseDescriptor.Write(fs); } } else { ExtentType extentType = CreateTypeToExtentType(type); long totalSize = 0; List <ExtentDescriptor> extents = new List <ExtentDescriptor>(); if (type == DiskCreateType.MonolithicFlat || type == DiskCreateType.VmfsSparse || type == DiskCreateType.Vmfs) { string adornment = "flat"; if (type == DiskCreateType.VmfsSparse) { adornment = string.IsNullOrEmpty(baseDescriptor.ParentFileNameHint) ? "sparse" : "delta"; } string fileName = AdornFileName(file, adornment); using (Stream fs = fileLocator.Open(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { CreateExtent(fs, capacity, extentType); extents.Add(new ExtentDescriptor(ExtentAccess.ReadWrite, capacity / Sizes.Sector, extentType, fileName, 0)); totalSize = capacity; } } else if (type == DiskCreateType.TwoGbMaxExtentFlat || type == DiskCreateType.TwoGbMaxExtentSparse) { int i = 1; while (totalSize < capacity) { string adornment; if (type == DiskCreateType.TwoGbMaxExtentSparse) { adornment = string.Format(CultureInfo.InvariantCulture, "s{0:x3}", i); } else { adornment = string.Format(CultureInfo.InvariantCulture, "{0:x6}", i); } string fileName = AdornFileName(file, adornment); using (Stream fs = fileLocator.Open(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { long extentSize = Math.Min((2 * Sizes.OneGiB) - Sizes.OneMiB, capacity - totalSize); CreateExtent(fs, extentSize, extentType); extents.Add(new ExtentDescriptor(ExtentAccess.ReadWrite, extentSize / Sizes.Sector, extentType, fileName, 0)); totalSize += extentSize; } ++i; } } else { throw new NotSupportedException("Creating disks of this type is not supported"); } using (Stream fs = fileLocator.Open(file, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { baseDescriptor.Extents.AddRange(extents); baseDescriptor.Write(fs); } } return(new DiskImageFile(fileLocator, file, FileAccess.ReadWrite)); }