Program.ListCommand.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using DiskAccessLibrary.LogicalDiskManager;
  6. using DiskAccessLibrary;
  7. using Utilities;
  8. namespace ISCSIConsole
  9. {
  10. partial class Program
  11. {
  12. public static void ListCommand(string[] args)
  13. {
  14. if (args.Length >= 2)
  15. {
  16. switch (args[1].ToLower())
  17. {
  18. case "disk":
  19. Console.WriteLine();
  20. ListPhysicalDisks();
  21. break;
  22. case "partition":
  23. Console.WriteLine();
  24. ListPartitions();
  25. break;
  26. case "volume":
  27. Console.WriteLine();
  28. ListVolumes();
  29. break;
  30. case "extent":
  31. Console.WriteLine();
  32. ListExtents();
  33. break;
  34. default:
  35. Console.WriteLine();
  36. Console.WriteLine("Invalid parameter.");
  37. HelpList();
  38. break;
  39. }
  40. }
  41. else
  42. {
  43. HelpList();
  44. }
  45. }
  46. public static void HelpList()
  47. {
  48. Console.WriteLine();
  49. Console.WriteLine("LIST DISK - Print a list of physical disks.");
  50. Console.WriteLine("LIST PARTITION - Print a list of partitions on the selected disk.");
  51. Console.WriteLine("LIST VOLUME - Print a list of supported volumes.");
  52. Console.WriteLine("LIST EXTENT - Print a list of extents of the selected volume.");
  53. }
  54. public static void ListPhysicalDisks()
  55. {
  56. List<PhysicalDisk> disks = PhysicalDiskHelper.GetPhysicalDisks();
  57. Console.WriteLine("Disk ## Size GPT Dyn DiskID Disk Group Name ");
  58. Console.WriteLine("------- ------- --- --- ------ ------------------");
  59. foreach (PhysicalDisk disk in disks)
  60. {
  61. int index = disk.PhysicalDiskIndex;
  62. string diskNumber = index.ToString().PadLeft(2);
  63. MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(disk);
  64. string isGPTStr = (mbr != null && mbr.IsGPTBasedDisk) ? " * " : " ";
  65. string isDynStr = DynamicDisk.IsDynamicDisk(disk) ? " * " : " ";
  66. string diskID = String.Empty;
  67. string diskGroupName = String.Empty;
  68. VolumeManagerDatabase database = VolumeManagerDatabase.ReadFromDisk(disk);
  69. if (database != null)
  70. {
  71. PrivateHeader privateHeader = PrivateHeader.ReadFromDisk(disk);
  72. DiskRecord diskRecord = database.FindDiskByDiskGuid(privateHeader.DiskGuid);
  73. diskID = diskRecord.DiskId.ToString();
  74. diskGroupName = database.DiskGroupName;
  75. }
  76. diskID = diskID.PadLeft(6);
  77. Console.WriteLine("Disk {0} {1} {2} {3} {4} {5}", diskNumber, FormattingHelper.GetStandardSizeString(disk.Size), isGPTStr, isDynStr, diskID, diskGroupName);
  78. }
  79. }
  80. public static void ListPartitions()
  81. {
  82. if (m_selectedDisk != null)
  83. {
  84. List<Partition> partitions = BasicDiskHelper.GetPartitions(m_selectedDisk);
  85. Console.WriteLine("Partition # Type Size Offset Start Sector");
  86. Console.WriteLine("----------- ---------------- ------- ------- ------------");
  87. for (int index = 0; index < partitions.Count; index++)
  88. {
  89. Partition partition = partitions[index];
  90. long offset = partition.FirstSector * m_selectedDisk.BytesPerSector;
  91. long size = partition.Size;
  92. string partitionType;
  93. if (partition is GPTPartition)
  94. {
  95. partitionType = ((GPTPartition)partition).PartitionTypeName;
  96. }
  97. else // partition is MBRPartition
  98. {
  99. partitionType = ((MBRPartition)partition).PartitionTypeName.ToString();
  100. }
  101. partitionType = partitionType.PadRight(16);
  102. string startSector = partition.FirstSector.ToString().PadLeft(12);
  103. Console.WriteLine("Partition {0} {1} {2} {3} {4}", index.ToString(), partitionType, FormattingHelper.GetStandardSizeString(size), FormattingHelper.GetStandardSizeString(offset), startSector);
  104. }
  105. }
  106. else
  107. {
  108. Console.WriteLine("No disk has been selected");
  109. }
  110. }
  111. public static void ListVolumes()
  112. {
  113. List<Volume> volumes = WindowsVolumeHelper.GetVolumes();
  114. Console.WriteLine("Volume ## ID Name Type Size Status ");
  115. Console.WriteLine("--------- ---- ------------ --------- ------- ---------");
  116. for (int index = 0; index < volumes.Count; index++)
  117. {
  118. Volume volume = volumes[index];
  119. string type = VolumeHelper.GetVolumeTypeString(volume);
  120. string status = VolumeHelper.GetVolumeStatusString(volume);
  121. ulong volumeID = 0;
  122. string name = String.Empty;
  123. if (volume is DynamicVolume)
  124. {
  125. volumeID = ((DynamicVolume)volume).VolumeID;
  126. name = ((DynamicVolume)volume).Name;
  127. }
  128. string volumeNumber = index.ToString().PadLeft(2);
  129. type = type.ToString().PadRight(9);
  130. name = name.ToString().PadRight(12);
  131. status = status.ToString().PadRight(9);
  132. string volumeIDString = String.Empty;
  133. if (volumeID != 0)
  134. {
  135. volumeIDString = volumeID.ToString();
  136. }
  137. volumeIDString = volumeIDString.PadRight(4);
  138. Console.WriteLine("Volume {0} {1} {2} {3} {4} {5}", volumeNumber, volumeIDString, name, type, FormattingHelper.GetStandardSizeString(volume.Size), status);
  139. }
  140. }
  141. public static void ListExtents()
  142. {
  143. if (m_selectedVolume != null)
  144. {
  145. Console.WriteLine("Extent ## ID Name Size DiskID Offset Start Sector");
  146. Console.WriteLine("--------- ---- --------- ------- ------ ------- ------------");
  147. for (int index = 0; index < m_selectedVolume.Extents.Count; index++)
  148. {
  149. DiskExtent extent = m_selectedVolume.Extents[index];
  150. string extentNumber = index.ToString().PadLeft(2);
  151. ulong extentID = 0;
  152. ulong diskID = 0;
  153. string name = String.Empty;
  154. if (extent is DynamicDiskExtent)
  155. {
  156. extentID = ((DynamicDiskExtent)extent).ExtentID;
  157. name = ((DynamicDiskExtent)extent).Name;
  158. if (extent.Disk != null)
  159. {
  160. VolumeManagerDatabase database = VolumeManagerDatabase.ReadFromDisk(extent.Disk);
  161. if (database != null)
  162. {
  163. ExtentRecord extentRecord = database.FindExtentByExtentID(extentID);
  164. diskID = extentRecord.DiskId;
  165. }
  166. }
  167. }
  168. string offsetString;
  169. if (extent.Disk != null)
  170. {
  171. long offset = extent.FirstSector * extent.Disk.BytesPerSector;
  172. offsetString = FormattingHelper.GetStandardSizeString(offset);
  173. }
  174. else
  175. {
  176. offsetString = " N/A";
  177. }
  178. long size = extent.Size;
  179. name = name.ToString().PadRight(9);
  180. string extentIDString = String.Empty;
  181. if (extentID != 0)
  182. {
  183. extentIDString = extentID.ToString();
  184. }
  185. extentIDString = extentIDString.PadLeft(4);
  186. string diskIDString = String.Empty;
  187. if (diskID != 0)
  188. {
  189. diskIDString = diskID.ToString();
  190. }
  191. diskIDString = diskIDString.PadLeft(6);
  192. string startSector = extent.FirstSector.ToString().PadLeft(12);
  193. Console.WriteLine("Extent {0} {1} {2} {3} {4} {5} {6}", extentNumber, extentIDString, name, FormattingHelper.GetStandardSizeString(size), diskIDString, offsetString, startSector);
  194. }
  195. }
  196. else
  197. {
  198. Console.WriteLine("No volume has been selected");
  199. }
  200. }
  201. }
  202. }