Program.CreateCommand.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using DiskAccessLibrary;
  6. using DiskAccessLibrary.LogicalDiskManager;
  7. using DiskAccessLibrary.VHD;
  8. using Utilities;
  9. namespace ISCSIConsole
  10. {
  11. public partial class Program
  12. {
  13. public static void CreateCommand(string[] args)
  14. {
  15. if (args.Length >= 2)
  16. {
  17. if (args[1].ToLower() == "vdisk")
  18. {
  19. KeyValuePairList<string, string> parameters = ParseParameters(args, 2);
  20. CreateVDisk(parameters);
  21. }
  22. else
  23. {
  24. Console.WriteLine("Invalid argument.");
  25. HelpCreate();
  26. }
  27. }
  28. else
  29. {
  30. HelpCreate();
  31. }
  32. }
  33. public static void CreateVDisk(KeyValuePairList<string, string> parameters)
  34. {
  35. if (!VerifyParameters(parameters, "file", "size"))
  36. {
  37. Console.WriteLine();
  38. Console.WriteLine("Invalid parameter.");
  39. HelpCreate();
  40. return;
  41. }
  42. long sizeInBytes;
  43. if (parameters.ContainsKey("size"))
  44. {
  45. long requestedSizeInMB = Conversion.ToInt64(parameters.ValueOf("size"), 0);
  46. sizeInBytes = requestedSizeInMB * 1024 * 1024;
  47. if (requestedSizeInMB <= 0)
  48. {
  49. Console.WriteLine("Invalid size (must be specified in MB).");
  50. return;
  51. }
  52. }
  53. else
  54. {
  55. Console.WriteLine("The SIZE parameter must be specified.");
  56. return;
  57. }
  58. if (parameters.ContainsKey("file"))
  59. {
  60. string path = parameters.ValueOf("file");
  61. if (new FileInfo(path).Exists)
  62. {
  63. Console.WriteLine("Error: file already exists.");
  64. return;
  65. }
  66. try
  67. {
  68. m_selectedDisk = VirtualHardDisk.Create(path, sizeInBytes);
  69. Console.WriteLine("The virtual disk file was created successfully.");
  70. }
  71. catch (IOException ex)
  72. {
  73. Console.WriteLine("Error: Could not write the virtual disk file. {0}", ex.Message);
  74. return;
  75. }
  76. catch (UnauthorizedAccessException)
  77. {
  78. Console.WriteLine("Error: Access Denied, Could not write the virtual disk file.");
  79. return;
  80. }
  81. }
  82. else
  83. {
  84. Console.WriteLine("The FILE parameter was not specified.");
  85. }
  86. }
  87. public static void HelpCreate()
  88. {
  89. Console.WriteLine();
  90. Console.WriteLine("CREATE VDISK FILE=<path> SIZE=<N>");
  91. Console.WriteLine();
  92. Console.WriteLine("Note:");
  93. Console.WriteLine("-----");
  94. Console.WriteLine("1. SIZE must be specified in MB, and without any suffixes.");
  95. }
  96. }
  97. }