Program.AttachCommand.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (C) 2012-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 DiskAccessLibrary;
  11. using DiskAccessLibrary.LogicalDiskManager;
  12. using SCSI;
  13. using ISCSI.Server;
  14. using Utilities;
  15. namespace ISCSIConsole
  16. {
  17. partial class Program
  18. {
  19. public const string DefaultTargetIQN = "iqn.1991-05.com.microsoft";
  20. public static void AttachCommand(string[] args)
  21. {
  22. if (m_server != null)
  23. {
  24. Console.WriteLine("Server is already running");
  25. return;
  26. }
  27. if (args.Length >= 2)
  28. {
  29. KeyValuePairList<string, string> parameters = ParseParameters(args, 2);
  30. if (!VerifyParameters(parameters, "vdisk", "disk", "volume", "readonly", "target"))
  31. {
  32. Console.WriteLine();
  33. Console.WriteLine("Invalid parameter");
  34. HelpAttach();
  35. return;
  36. }
  37. switch (args[1].ToLower())
  38. {
  39. case "vdisk":
  40. {
  41. if (m_selectedDisk == null)
  42. {
  43. Console.WriteLine("No disk has been selected");
  44. break;
  45. }
  46. if (!(m_selectedDisk is DiskImage))
  47. {
  48. Console.WriteLine("Selected disk is not a disk image");
  49. break;
  50. }
  51. DiskImage disk = (DiskImage)m_selectedDisk;
  52. string defaultStorageTargetName = Path.GetFileNameWithoutExtension(disk.Path);
  53. string defaultTargetName = DefaultTargetIQN + ":" + defaultStorageTargetName.Replace(" ", ""); // spaces are not allowed
  54. AttachISCSIDisk(disk, defaultTargetName, parameters);
  55. break;
  56. }
  57. case "disk":
  58. {
  59. if (m_selectedDisk == null)
  60. {
  61. Console.WriteLine("Error: No disk has been selected.");
  62. break;
  63. }
  64. if (!(m_selectedDisk is PhysicalDisk))
  65. {
  66. Console.WriteLine("Error: The selected disk is not a physical disk.");
  67. break;
  68. }
  69. bool isAttachmentReadOnly = parameters.ContainsKey("readonly");
  70. PhysicalDisk disk = (PhysicalDisk)m_selectedDisk;
  71. if (!isAttachmentReadOnly)
  72. {
  73. if (Environment.OSVersion.Version.Major >= 6)
  74. {
  75. bool isDiskReadOnly;
  76. bool isOnline = disk.GetOnlineStatus(out isDiskReadOnly);
  77. if (isOnline)
  78. {
  79. Console.WriteLine();
  80. Console.WriteLine("Error: The selected disk must be taken offline.");
  81. break;
  82. }
  83. if (!isAttachmentReadOnly && isDiskReadOnly)
  84. {
  85. Console.WriteLine();
  86. Console.WriteLine("Error: The selected disk is set to readonly!");
  87. break;
  88. }
  89. }
  90. else
  91. {
  92. Console.WriteLine();
  93. // Locking mechanism is not implemented
  94. Console.Write("Warning: if a volume on this disk is mounted locally, data corruption may occur!");
  95. }
  96. }
  97. string defaultStorageTargetName = string.Format("disk{0}", disk.PhysicalDiskIndex);
  98. string defaultTargetName = DefaultTargetIQN + ":" + defaultStorageTargetName;
  99. AttachISCSIDisk(disk, defaultTargetName, parameters);
  100. break;
  101. }
  102. case "volume":
  103. {
  104. if (m_selectedVolume == null)
  105. {
  106. Console.WriteLine("No volume has been selected.");
  107. break;
  108. }
  109. VolumeDisk virtualDisk = new VolumeDisk(m_selectedVolume);
  110. string defaultTargetName = DefaultTargetIQN + ":Volume";
  111. bool isAttachmentReadOnly = parameters.ContainsKey("readonly");
  112. if (!isAttachmentReadOnly)
  113. {
  114. if (Environment.OSVersion.Version.Major >= 6)
  115. {
  116. if (m_selectedVolume is DynamicVolume)
  117. {
  118. foreach(DiskExtent extent in ((DynamicVolume)m_selectedVolume).Extents)
  119. {
  120. if (extent.Disk is PhysicalDisk)
  121. {
  122. bool isDiskReadOnly;
  123. bool isOnline = ((PhysicalDisk)extent.Disk).GetOnlineStatus(out isDiskReadOnly);
  124. if (isOnline)
  125. {
  126. Console.WriteLine("Error: All disks containing the volume must be taken offline.");
  127. return;
  128. }
  129. if (isDiskReadOnly)
  130. {
  131. Console.WriteLine("Error: A disk containing the volume is set to readonly.");
  132. return;
  133. }
  134. }
  135. }
  136. }
  137. else if (m_selectedVolume is Partition)
  138. {
  139. Disk disk = ((Partition)m_selectedVolume).Disk;
  140. if (disk is PhysicalDisk)
  141. {
  142. bool isDiskReadOnly;
  143. bool isOnline = ((PhysicalDisk)disk).GetOnlineStatus(out isDiskReadOnly);
  144. if (isOnline)
  145. {
  146. Console.WriteLine("Error: The disk containing the volume must be taken offline.");
  147. return;
  148. }
  149. if (isDiskReadOnly)
  150. {
  151. Console.WriteLine("Error: The disk containing the volume is set to readonly.");
  152. return;
  153. }
  154. }
  155. }
  156. }
  157. else
  158. {
  159. Console.WriteLine();
  160. // Locking mechanism is not implemented
  161. Console.WriteLine("Warning: if this volume is mounted locally, data corruption may occur!");
  162. }
  163. }
  164. AttachISCSIDisk(virtualDisk, defaultTargetName, parameters);
  165. break;
  166. }
  167. default:
  168. {
  169. Console.WriteLine();
  170. Console.WriteLine("Invalid argument.");
  171. HelpAttach();
  172. break;
  173. }
  174. }
  175. }
  176. else
  177. {
  178. HelpAttach();
  179. }
  180. }
  181. public static void HelpAttach()
  182. {
  183. Console.WriteLine();
  184. Console.WriteLine("ATTACH VDISK [READONLY] [TARGET=<NAME>] - Attach virtual hard disk file");
  185. Console.WriteLine("ATTACH DISK [READONLY] [TARGET=<NAME>] - Attach selected physical disk");
  186. Console.WriteLine("ATTACH VOLUME [READONLY] [TARGET=<NAME>] - Attach selected volume");
  187. }
  188. public static void AttachISCSIDisk(Disk disk, string defaultTargetName, KeyValuePairList<string, string> parameters)
  189. {
  190. if (VerifyParameters(parameters, "readonly", "target"))
  191. {
  192. bool isReadOnly = parameters.ContainsKey("readonly");
  193. disk.IsReadOnly |= isReadOnly;
  194. if (disk is DiskImage)
  195. {
  196. bool isLocked = ((DiskImage)disk).ExclusiveLock();
  197. if (!isLocked)
  198. {
  199. Console.WriteLine("Error: Cannot lock the disk image for exclusive access");
  200. return;
  201. }
  202. }
  203. ISCSITarget target = null;
  204. string targetName = defaultTargetName;
  205. if (parameters.ContainsKey("target"))
  206. {
  207. string name = parameters.ValueOf("target");
  208. if (ISCSINameHelper.IsValidISCSIName(name))
  209. {
  210. targetName = name;
  211. }
  212. else if (ISCSINameHelper.IsValidSubQualifier(name))
  213. {
  214. targetName = DefaultTargetIQN + ":" + name;
  215. }
  216. else
  217. {
  218. Console.WriteLine("Invalid parameter.");
  219. HelpAttach();
  220. }
  221. }
  222. target = FindTarget(targetName);
  223. if (target == null)
  224. {
  225. target = AddTarget(targetName);
  226. }
  227. ((VirtualSCSITarget)target.SCSITarget).Disks.Add(disk);
  228. Console.WriteLine("Disk added to target: {0}", target.TargetName);
  229. }
  230. else
  231. {
  232. HelpAttach();
  233. }
  234. }
  235. public static ISCSITarget AddTarget(string targetName)
  236. {
  237. List<Disk> disks = new List<Disk>();
  238. VirtualSCSITarget scsiTarget = new VirtualSCSITarget(disks);
  239. scsiTarget.OnLogEntry += new EventHandler<LogEntry>(OnLogEntry);
  240. ISCSITarget target = new ISCSITarget(targetName, scsiTarget);
  241. m_targets.Add(target);
  242. return target;
  243. }
  244. public static ISCSITarget FindTarget(string targetName)
  245. {
  246. foreach (ISCSITarget target in m_targets)
  247. {
  248. // iSCSI names are not case sensitive
  249. if (target.TargetName.ToLower() == targetName.ToLower())
  250. {
  251. return target;
  252. }
  253. }
  254. return null;
  255. }
  256. }
  257. }