VirtualMachineDisk.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using Utilities;
  12. using DiskAccessLibrary.VMDK;
  13. namespace DiskAccessLibrary
  14. {
  15. public partial class VirtualMachineDisk : DiskImage, IDiskGeometry
  16. {
  17. // VMDK sector size is set to 512 bytes.
  18. public const int BytesPerDiskSector = 512;
  19. private const uint BaseDiskParentCID = 0xffffffff;
  20. private string m_descriptorPath;
  21. private VirtualMachineDiskDescriptor m_descriptor;
  22. private DiskImage m_extent;
  23. /// <exception cref="System.IO.IOException"></exception>
  24. /// <exception cref="System.IO.InvalidDataException"></exception>
  25. /// <exception cref="System.NotImplementedException"></exception>
  26. /// <exception cref="System.UnauthorizedAccessException"></exception>
  27. public VirtualMachineDisk(string descriptorPath) : base(descriptorPath)
  28. {
  29. m_descriptorPath = descriptorPath;
  30. m_descriptor = VirtualMachineDiskDescriptor.ReadFromFile(m_descriptorPath);
  31. bool isDescriptorEmbedded = false;
  32. if (m_descriptor == null)
  33. {
  34. SparseExtent sparse = new SparseExtent(m_descriptorPath);
  35. if (sparse.Descriptor != null)
  36. {
  37. isDescriptorEmbedded = true;
  38. m_descriptor = sparse.Descriptor;
  39. m_extent = sparse;
  40. }
  41. else
  42. {
  43. throw new InvalidDataException("Missing VMDK descriptor");
  44. }
  45. }
  46. if (m_descriptor.Version != 1)
  47. {
  48. throw new NotImplementedException("Unsupported VMDK descriptor version");
  49. }
  50. if (m_descriptor.ParentContentID != BaseDiskParentCID)
  51. {
  52. throw new InvalidDataException("VMDK descriptor ParentContentID does not match BaseDiskParentCID");
  53. }
  54. if (!isDescriptorEmbedded && m_descriptor.DiskType != VirtualMachineDiskType.MonolithicFlat)
  55. {
  56. throw new NotImplementedException("Unsupported VMDK disk type");
  57. }
  58. if (isDescriptorEmbedded && m_descriptor.DiskType != VirtualMachineDiskType.MonolithicSparse)
  59. {
  60. throw new NotImplementedException("Unsupported VMDK disk type");
  61. }
  62. foreach (VirtualMachineDiskExtentEntry extentEntry in m_descriptor.ExtentEntries)
  63. {
  64. if (!isDescriptorEmbedded && extentEntry.ExtentType != ExtentType.Flat)
  65. {
  66. throw new NotImplementedException("Unsupported VMDK extent type");
  67. }
  68. if (isDescriptorEmbedded && extentEntry.ExtentType != ExtentType.Sparse)
  69. {
  70. throw new NotImplementedException("Unsupported VMDK extent type");
  71. }
  72. }
  73. if (m_descriptor.ExtentEntries.Count != 1)
  74. {
  75. throw new NotImplementedException("Unsupported number of VMDK extents");
  76. }
  77. if (m_descriptor.DiskType == VirtualMachineDiskType.MonolithicFlat)
  78. {
  79. VirtualMachineDiskExtentEntry entry = m_descriptor.ExtentEntries[0];
  80. string directory = System.IO.Path.GetDirectoryName(descriptorPath);
  81. DiskImage extent = new RawDiskImage(directory + @"\" + entry.FileName, BytesPerDiskSector);
  82. m_extent = extent;
  83. }
  84. }
  85. public override bool ExclusiveLock()
  86. {
  87. return m_extent.ExclusiveLock();
  88. }
  89. public override bool ReleaseLock()
  90. {
  91. return m_extent.ReleaseLock();
  92. }
  93. public override byte[] ReadSectors(long sectorIndex, int sectorCount)
  94. {
  95. return m_extent.ReadSectors(sectorIndex, sectorCount);
  96. }
  97. public override void WriteSectors(long sectorIndex, byte[] data)
  98. {
  99. if (IsReadOnly)
  100. {
  101. throw new UnauthorizedAccessException("Attempted to perform write on a readonly disk");
  102. }
  103. m_extent.WriteSectors(sectorIndex, data);
  104. }
  105. public override void Extend(long numberOfAdditionalBytes)
  106. {
  107. if (m_descriptor.DiskType == VirtualMachineDiskType.MonolithicFlat)
  108. {
  109. // Add updated extent entries
  110. List<string> lines = VirtualMachineDiskDescriptor.ReadASCIITextLines(m_descriptorPath);
  111. m_descriptor.ExtentEntries[0].SizeInSectors += numberOfAdditionalBytes / this.BytesPerSector;
  112. m_descriptor.UpdateExtentEntries(lines);
  113. File.WriteAllLines(m_descriptorPath, lines.ToArray(), Encoding.ASCII);
  114. ((DiskImage)m_extent).Extend(numberOfAdditionalBytes);
  115. }
  116. else
  117. {
  118. throw new NotImplementedException("Extending a monolithic sparse is not supported");
  119. }
  120. }
  121. public override int BytesPerSector
  122. {
  123. get
  124. {
  125. return BytesPerDiskSector;
  126. }
  127. }
  128. public override long Size
  129. {
  130. get
  131. {
  132. return m_extent.Size;
  133. }
  134. }
  135. public long Cylinders
  136. {
  137. get
  138. {
  139. return m_descriptor.Cylinders;
  140. }
  141. }
  142. public int TracksPerCylinder
  143. {
  144. get
  145. {
  146. return m_descriptor.TracksPerCylinder;
  147. }
  148. }
  149. public int SectorsPerTrack
  150. {
  151. get
  152. {
  153. return m_descriptor.SectorsPerTrack;
  154. }
  155. }
  156. }
  157. }