Program.AttachCommand.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using DiskAccessLibrary;
  5. using DiskAccessLibrary.LogicalDiskManager;
  6. using ISCSI;
  7. using Utilities;
  8. namespace ISCSIConsole
  9. {
  10. partial class Program
  11. {
  12. public const string DefaultTargetIQN = "iqn.1991-05.com.microsoft";
  13. public static void AttachCommand(string[] args)
  14. {
  15. if (m_server != null)
  16. {
  17. Console.WriteLine("Server is already running");
  18. return;
  19. }
  20. if (args.Length >= 2)
  21. {
  22. KeyValuePairList<string, string> parameters = ParseParameters(args, 2);
  23. if (!VerifyParameters(parameters, "vdisk", "disk", "volume", "readonly", "target"))
  24. {
  25. Console.WriteLine();
  26. Console.WriteLine("Invalid parameter");
  27. HelpAttach();
  28. return;
  29. }
  30. switch (args[1].ToLower())
  31. {
  32. case "vdisk":
  33. {
  34. if (m_selectedDisk == null)
  35. {
  36. Console.WriteLine("No disk has been selected");
  37. break;
  38. }
  39. if (!(m_selectedDisk is DiskImage))
  40. {
  41. Console.WriteLine("Selected disk is not a disk image");
  42. break;
  43. }
  44. DiskImage disk = (DiskImage)m_selectedDisk;
  45. string defaultStorageTargetName = Path.GetFileNameWithoutExtension(disk.Path);
  46. string defaultTargetName = DefaultTargetIQN + ":" + defaultStorageTargetName.Replace(" ", ""); // spaces are not allowed
  47. bool success = AttachISCSIDisk(disk, defaultTargetName, parameters);
  48. if (!success)
  49. {
  50. Console.WriteLine();
  51. Console.WriteLine("Invalid parameter.");
  52. HelpAttach();
  53. }
  54. break;
  55. }
  56. case "disk":
  57. {
  58. if (m_selectedDisk == null)
  59. {
  60. Console.WriteLine("Error: No disk has been selected.");
  61. break;
  62. }
  63. if (!(m_selectedDisk is PhysicalDisk))
  64. {
  65. Console.WriteLine("Error: The selected disk is not a physical disk.");
  66. break;
  67. }
  68. bool isAttachmentReadOnly = parameters.ContainsKey("readonly");
  69. PhysicalDisk disk = (PhysicalDisk)m_selectedDisk;
  70. if (!isAttachmentReadOnly)
  71. {
  72. if (Environment.OSVersion.Version.Major >= 6)
  73. {
  74. bool isDiskReadOnly;
  75. bool isOnline = disk.GetOnlineStatus(out isDiskReadOnly);
  76. if (isOnline)
  77. {
  78. Console.WriteLine();
  79. Console.WriteLine("Error: The selected disk must be taken offline.");
  80. break;
  81. }
  82. if (!isAttachmentReadOnly && isDiskReadOnly)
  83. {
  84. Console.WriteLine();
  85. Console.WriteLine("Error: The selected disk is set to readonly!");
  86. break;
  87. }
  88. }
  89. else
  90. {
  91. Console.WriteLine();
  92. // Locking mechanism is not implemented
  93. Console.Write("Warning: if a volume on this disk is mounted locally, data corruption may occur!");
  94. }
  95. }
  96. string defaultStorageTargetName = string.Format("disk{0}", disk.PhysicalDiskIndex);
  97. string defaultTargetName = DefaultTargetIQN + ":" + defaultStorageTargetName;
  98. bool success = AttachISCSIDisk(disk, defaultTargetName, parameters);
  99. if (!success)
  100. {
  101. Console.WriteLine();
  102. Console.WriteLine("Invalid parameter.");
  103. HelpAttach();
  104. }
  105. break;
  106. }
  107. case "volume":
  108. {
  109. if (m_selectedVolume == null)
  110. {
  111. Console.WriteLine("No volume has been selected.");
  112. break;
  113. }
  114. VolumeDisk virtualDisk = new VolumeDisk(m_selectedVolume);
  115. string defaultTargetName = DefaultTargetIQN + ":Volume";
  116. bool isAttachmentReadOnly = parameters.ContainsKey("readonly");
  117. if (!isAttachmentReadOnly)
  118. {
  119. if (Environment.OSVersion.Version.Major >= 6)
  120. {
  121. if (m_selectedVolume is DynamicVolume)
  122. {
  123. foreach(DiskExtent extent in ((DynamicVolume)m_selectedVolume).Extents)
  124. {
  125. if (extent.Disk is PhysicalDisk)
  126. {
  127. bool isDiskReadOnly;
  128. bool isOnline = ((PhysicalDisk)extent.Disk).GetOnlineStatus(out isDiskReadOnly);
  129. if (isOnline)
  130. {
  131. Console.WriteLine("Error: All disks containing the volume must be taken offline.");
  132. return;
  133. }
  134. if (isDiskReadOnly)
  135. {
  136. Console.WriteLine("Error: A disk containing the volume is set to readonly.");
  137. return;
  138. }
  139. }
  140. }
  141. }
  142. else if (m_selectedVolume is Partition)
  143. {
  144. Disk disk = ((Partition)m_selectedVolume).Disk;
  145. if (disk is PhysicalDisk)
  146. {
  147. bool isDiskReadOnly;
  148. bool isOnline = ((PhysicalDisk)disk).GetOnlineStatus(out isDiskReadOnly);
  149. if (isOnline)
  150. {
  151. Console.WriteLine("Error: The disk containing the volume must be taken offline.");
  152. return;
  153. }
  154. if (isDiskReadOnly)
  155. {
  156. Console.WriteLine("Error: The disk containing the volume is set to readonly.");
  157. return;
  158. }
  159. }
  160. }
  161. }
  162. else
  163. {
  164. Console.WriteLine();
  165. // Locking mechanism is not implemented
  166. Console.WriteLine("Warning: if this volume is mounted locally, data corruption may occur!");
  167. }
  168. }
  169. bool success = AttachISCSIDisk(virtualDisk, defaultTargetName, parameters);
  170. if (!success)
  171. {
  172. Console.WriteLine();
  173. Console.WriteLine("Invalid parameter.");
  174. HelpAttach();
  175. }
  176. break;
  177. }
  178. default:
  179. {
  180. Console.WriteLine();
  181. Console.WriteLine("Invalid argument.");
  182. HelpAttach();
  183. break;
  184. }
  185. }
  186. }
  187. else
  188. {
  189. HelpAttach();
  190. }
  191. }
  192. public static void HelpAttach()
  193. {
  194. Console.WriteLine();
  195. Console.WriteLine("ATTACH VDISK [READONLY] [TARGET=<NAME>] - Attach virtual hard disk file");
  196. Console.WriteLine("ATTACH DISK [READONLY] [TARGET=<NAME>] - Attach selected physical disk");
  197. Console.WriteLine("ATTACH VOLUME [READONLY] [TARGET=<NAME>] - Attach selected volume");
  198. }
  199. public static bool AttachISCSIDisk(Disk disk, string defaultTargetName, KeyValuePairList<string, string> parameters)
  200. {
  201. if (VerifyParameters(parameters, "readonly", "target"))
  202. {
  203. bool isReadOnly = parameters.ContainsKey("readonly");
  204. disk.IsReadOnly |= isReadOnly;
  205. ISCSITarget target = null;
  206. string targetName = defaultTargetName;
  207. if (parameters.ContainsKey("target"))
  208. {
  209. string name = parameters.ValueOf("target");
  210. if (IsValidISCSIName(name))
  211. {
  212. targetName = name;
  213. }
  214. else if (IsValidStorageTargetName(name))
  215. {
  216. targetName = DefaultTargetIQN + ":" + name;
  217. }
  218. else
  219. {
  220. // The caller will display invalid parameter error
  221. return false;
  222. }
  223. }
  224. target = FindTarget(targetName);
  225. if (target == null)
  226. {
  227. target = AddTarget(targetName);
  228. }
  229. target.Disks.Add(disk);
  230. Console.WriteLine("Disk added to target: {0}", target.TargetName);
  231. return true;
  232. }
  233. else
  234. {
  235. return false;
  236. }
  237. }
  238. public static ISCSITarget AddTarget(string targetName)
  239. {
  240. List<Disk> disks = new List<Disk>();
  241. ISCSITarget target = new ISCSITarget(targetName, disks);
  242. m_targets.Add(target);
  243. return target;
  244. }
  245. public static ISCSITarget FindTarget(string targetName)
  246. {
  247. foreach (ISCSITarget target in m_targets)
  248. {
  249. // iSCSI names are not case sensitive
  250. if (target.TargetName.ToLower() == targetName.ToLower())
  251. {
  252. return target;
  253. }
  254. }
  255. return null;
  256. }
  257. /// <summary>
  258. /// Check if a name is a valid initiator or target name (a.k.a. iSCSI name)
  259. /// </summary>
  260. public static bool IsValidISCSIName(string name)
  261. {
  262. if (name.ToLower().StartsWith("iqn."))
  263. {
  264. return IsValidIQN(name);
  265. }
  266. else
  267. {
  268. return IsValidEUI(name);
  269. }
  270. }
  271. public static bool IsValidIQN(string name)
  272. {
  273. if (name.ToLower().StartsWith("iqn."))
  274. {
  275. if (name.Length > 12 && name[8] == '-' && name[11] == '.')
  276. {
  277. int year = Conversion.ToInt32(name.Substring(4, 4), -1);
  278. int month = Conversion.ToInt32(name.Substring(9, 2), -1);
  279. if (year != -1 && (month >= 1 && month <= 12))
  280. {
  281. string reversedDomain;
  282. string storageTargetName = String.Empty;
  283. int index = name.IndexOf(":");
  284. if (index >= 12) // index cannot be < 12
  285. {
  286. reversedDomain = name.Substring(12, index - 12);
  287. storageTargetName = name.Substring(index + 1);
  288. return IsValidReversedDomainName(reversedDomain) && IsValidStorageTargetName(storageTargetName);
  289. }
  290. else
  291. {
  292. reversedDomain = name.Substring(12);
  293. return IsValidReversedDomainName(reversedDomain);
  294. }
  295. }
  296. }
  297. }
  298. return false;
  299. }
  300. public static bool IsValidReversedDomainName(string name)
  301. {
  302. string[] components = name.Split('.');
  303. if (components.Length < 1)
  304. {
  305. return false;
  306. }
  307. foreach (string component in components)
  308. {
  309. if (component.Length == 0 || component.StartsWith("-") || component.EndsWith("-"))
  310. {
  311. return false;
  312. }
  313. for (int index = 0; index < component.Length; index++)
  314. {
  315. bool isValid = (component[index] >= '0' && component[index] <= '9') ||
  316. (component[index] >= 'a' && component[index] <= 'z') ||
  317. (component[index] >= 'A' && component[index] <= 'Z') ||
  318. (component[index] == '-');
  319. if (!isValid)
  320. {
  321. return false;
  322. }
  323. }
  324. }
  325. return true;
  326. }
  327. public static bool IsValidStorageTargetName(string name)
  328. {
  329. // With the exception of the colon prefix, the owner of the domain name can assign everything after the reversed domain name as desired
  330. // No whitespace characters are used in iSCSI names
  331. // Note: String.Empty is a valid storage target name
  332. if (name.StartsWith(":") || name.Contains(" "))
  333. {
  334. return false;
  335. }
  336. return true;
  337. }
  338. public static bool IsValidEUI(string name)
  339. {
  340. if (name.ToLower().StartsWith("eui.") && name.Length == 20)
  341. {
  342. string identifier = name.Substring(5);
  343. return OnlyHexChars(identifier);
  344. }
  345. return false;
  346. }
  347. public static bool OnlyHexChars(string value)
  348. {
  349. for (int index = 0; index < value.Length; index++)
  350. {
  351. bool isValid = (value[index] >= '0' && value[index] <= '9') ||
  352. (value[index] >= 'a' && value[index] <= 'f') ||
  353. (value[index] >= 'A' && value[index] <= 'F');
  354. if (!isValid)
  355. {
  356. return false;
  357. }
  358. }
  359. return true;
  360. }
  361. }
  362. }