Program.ListCommand.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = String.Empty;
  120. string status = String.Empty;
  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. if (((DynamicVolume)volume).IsHealthy)
  128. {
  129. status = "Healthy";
  130. }
  131. else
  132. {
  133. status = "Failed";
  134. }
  135. }
  136. if (volume is SimpleVolume)
  137. {
  138. type = "Simple";
  139. }
  140. else if (volume is SpannedVolume)
  141. {
  142. type = "Spanned";
  143. }
  144. else if (volume is StripedVolume)
  145. {
  146. type = "Striped";
  147. }
  148. else if (volume is MirroredVolume)
  149. {
  150. type = "Mirrored";
  151. if (!((MirroredVolume)volume).IsHealthy && ((MirroredVolume)volume).IsOperational)
  152. {
  153. status = "Failed Rd";
  154. }
  155. }
  156. else if (volume is Raid5Volume)
  157. {
  158. type = "RAID-5";
  159. if (!((Raid5Volume)volume).IsHealthy && ((Raid5Volume)volume).IsOperational)
  160. {
  161. status = "Failed Rd";
  162. }
  163. }
  164. else if (volume is Partition)
  165. {
  166. type = "Partition";
  167. status = "Healthy";
  168. }
  169. string volumeNumber = index.ToString().PadLeft(2);
  170. type = type.ToString().PadRight(9);
  171. name = name.ToString().PadRight(12);
  172. status = status.ToString().PadRight(9);
  173. string volumeIDString = String.Empty;
  174. if (volumeID != 0)
  175. {
  176. volumeIDString = volumeID.ToString();
  177. }
  178. volumeIDString = volumeIDString.PadRight(4);
  179. Console.WriteLine("Volume {0} {1} {2} {3} {4} {5}", volumeNumber, volumeIDString, name, type, FormattingHelper.GetStandardSizeString(volume.Size), status);
  180. }
  181. }
  182. public static void ListExtents()
  183. {
  184. if (m_selectedVolume != null)
  185. {
  186. Console.WriteLine("Extent ## ID Name Size DiskID Offset Start Sector");
  187. Console.WriteLine("--------- ---- --------- ------- ------ ------- ------------");
  188. for (int index = 0; index < m_selectedVolume.Extents.Count; index++)
  189. {
  190. DiskExtent extent = m_selectedVolume.Extents[index];
  191. string extentNumber = index.ToString().PadLeft(2);
  192. ulong extentID = 0;
  193. ulong diskID = 0;
  194. string name = String.Empty;
  195. if (extent is DynamicDiskExtent)
  196. {
  197. extentID = ((DynamicDiskExtent)extent).ExtentID;
  198. name = ((DynamicDiskExtent)extent).Name;
  199. if (extent.Disk != null)
  200. {
  201. VolumeManagerDatabase database = VolumeManagerDatabase.ReadFromDisk(extent.Disk);
  202. if (database != null)
  203. {
  204. ExtentRecord extentRecord = database.FindExtentByExtentID(extentID);
  205. diskID = extentRecord.DiskId;
  206. }
  207. }
  208. }
  209. string offsetString;
  210. if (extent.Disk != null)
  211. {
  212. long offset = extent.FirstSector * extent.Disk.BytesPerSector;
  213. offsetString = FormattingHelper.GetStandardSizeString(offset);
  214. }
  215. else
  216. {
  217. offsetString = " N/A";
  218. }
  219. long size = extent.Size;
  220. name = name.ToString().PadRight(9);
  221. string extentIDString = String.Empty;
  222. if (extentID != 0)
  223. {
  224. extentIDString = extentID.ToString();
  225. }
  226. extentIDString = extentIDString.PadLeft(4);
  227. string diskIDString = String.Empty;
  228. if (diskID != 0)
  229. {
  230. diskIDString = diskID.ToString();
  231. }
  232. diskIDString = diskIDString.PadLeft(6);
  233. string startSector = extent.FirstSector.ToString().PadLeft(12);
  234. Console.WriteLine("Extent {0} {1} {2} {3} {4} {5} {6}", extentNumber, extentIDString, name, FormattingHelper.GetStandardSizeString(size), diskIDString, offsetString, startSector);
  235. }
  236. }
  237. else
  238. {
  239. Console.WriteLine("No volume has been selected");
  240. }
  241. }
  242. }
  243. }