Advertisement
ASMProgrammer

Lua Hyper-V (applied to OpenComputers)

Jul 5th, 2024
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.17 KB | Software | 0 0
  1. local hyper_v = {}
  2. -- Определение интерфейса для диска, который можно подключить к SCSI-контроллеру
  3. hyper_v.IScsiDrive = {}
  4.  
  5. -- Defines the current state of a virtual machine.
  6. hyper_v.VirtualMachineState = {
  7.     -- <summary>The state of the virtual machine could not be determined.</summary>
  8.     Unknown = 0,
  9.     -- <summary>The virtual machine is in an other state.</summary>
  10.     Other = 1,
  11.     -- <summary>The virtual machine is running.</summary>
  12.     Running = 2,
  13.     -- <summary>The virtual machine is turned off.</summary>
  14.     Off = 3,
  15.     -- <summary>The virtual machine is in the process of turning off.</summary>
  16.     ShuttingDown = 4,
  17.     -- <summary>The virtual machine does not support being started or turned off.</summary>
  18.     NotApplicable = 5,
  19.     -- <summary>The virtual machine might be completing commands, and it will drop any new requests.</summary>
  20.     EnabledButOffline = 6,
  21.     -- <summary>The virtual machine is in a test state.</summary>
  22.     InTest = 7,
  23.     -- <summary>The virtual machine might be completing commands, but it will queue any new requests.</summary>
  24.     Deferred = 8,
  25.     -- <summary>The virtual machine is running but in a restricted mode. The behavior of the virtual machine is similar to the Running state, but it processes only a restricted set of commands. All other requests are queued.</summary>
  26.     Quiesce = 9,
  27.     -- <summary>The virtual machine is in the process of starting. New requests are queued.</summary>
  28.     Starting = 10
  29. }
  30.  
  31. hyper_v.VirtualHardDrive = {}
  32. hyper_v.VirtualHardDrive.__index = hyper_v.VirtualHardDrive
  33.  
  34. -- The file format of the virtual hard disk.
  35. hyper_v.VirtualHardDiskFormat = {
  36.     -- <summary>Generation 1 virtual hard disk format.</summary>
  37.     Vhd = 2,
  38.     -- <summary>Generation 2 virtual hard disk format.</summary>
  39.     Vhdx = 3
  40. }
  41.  
  42. -- Defines the type of virtual hard disk.
  43. hyper_v.VirtualHardType = {
  44.     -- <summary>Space for fixed virtual hard disks is first allocated when the file is created.</summary>
  45.     FixedSize = 2,
  46.     -- <summary>Space for dynamically expanding virtual hard disks is allocated on demand.</summary>
  47.     DynamicallyExpanding = 3
  48.     -- Differencing disks are not supported by this library.
  49.     -- Differencing = 4
  50. }
  51.  
  52. hyper_v.VirtualHardDisk = {}
  53. hyper_v.VirtualHardDisk.__index = hyper_v.VirtualHardDisk
  54.  
  55. -- Define the ScsiController class
  56. hyper_v.ScsiController = {}
  57. hyper_v.ScsiController.__index = hyper_v.ScsiController
  58. hyper_v.ScsiController.Drives = {}
  59.  
  60. -- Defines virtual DVD media
  61. hyper_v.VirtualDvdDisk = {}
  62. hyper_v.VirtualDvdDisk.__index = hyper_v.VirtualDvdDisk
  63.  
  64. -- Defines a virtual DVD drive
  65. hyper_v.VirtualDvdDrive = {}
  66. hyper_v.VirtualDvdDrive.__index = hyper_v.VirtualDvdDrive
  67.  
  68. hyper_v.WmiHelpers = {}
  69.  
  70. hyper_v.SecureBootTemplate = {}
  71. hyper_v.SecureBootTemplate.RsOpenServer = "{1734C6E8-3154-4DDA-BA5F-A874CC483422}"
  72. hyper_v.SecureBootTemplate.RsUEFICertificateAuthority = "{272E7447-90A4-4563-A4B9-8E4AB00526CE}"
  73. hyper_v.SecureBootTemplate.OpenSourceShieldedVM = "{4292AE2B-EE2C-42B5-A969-DD8F8689F6F3}"
  74.  
  75. -- Define the MacAddress class
  76. hyper_v.MacAddress = {}
  77. hyper_v.MacAddress.__index = hyper_v.MacAddress
  78.  
  79. -- Define the NetworkAdapter class
  80. hyper_v.NetworkAdapter = {}
  81. hyper_v.NetworkAdapter.__index = hyper_v.NetworkAdapter
  82.  
  83. -- Defines the automatic start action.
  84. hyper_v.AutomaticStartAction = {
  85.     -- Do not start automatically.
  86.     Nothing = 2,
  87.     -- Automatically start if the virtual machine was running when the service stopped.
  88.     StartIfRunning = 3,
  89.     -- Always start the virtual machine automatically.
  90.     AlwaysStart = 4
  91. }
  92.  
  93. -- Defines the automatic stop action.
  94. hyper_v.AutomaticStopAction = {
  95.     -- Save the virtual machine state.
  96.     Save = 3,
  97.     -- Turn off the virtual machine.
  98.     TurnOff = 2,
  99.     -- Shutdown the guest operating system.
  100.     Shutdown = 4
  101. }
  102.  
  103. -- Defines the port mirroring mode for a network adapter.
  104. hyper_v.PortMirroringMode = {
  105.     -- The network adapter will not participate in mirroring.
  106.     None = nil,
  107.  
  108.     -- The network adapter will accept mirrored data from source ports.
  109.     Destination = nil,
  110.  
  111.     -- The network adapter will send mirrored data to detination ports.
  112.     Source = nil
  113. }
  114.  
  115. -- Defines the priority when balancing memory availability compared to other virtual machines.
  116. hyper_v.MemoryWeight = {
  117.     -- The lowest priority possible.
  118.     Lowest = 0,
  119.  
  120.     -- The priority is lower than low.
  121.     Lower = 1250,
  122.  
  123.     -- The priority is lower than balanced.
  124.     Low = 2500,
  125.  
  126.     -- The priority is lower than balanced.
  127.     BalancedLow = 3750,
  128.  
  129.     -- The priority is balanced.
  130.     Balanced = 5000,
  131.  
  132.     -- The priority is higher than balanced.
  133.     BalancedHigh = 6250,
  134.  
  135.     -- The priority is high.
  136.     High = 7500,
  137.  
  138.     -- The priority is higher than high.
  139.     Higher = 8750,
  140.  
  141.     -- The highest priority possible.
  142.     Higest = 10000
  143. }
  144.  
  145. function hyper_v.IScsiDrive.new()
  146.   local obj = {}
  147.   setmetatable(obj, hyper_v.IScsiDrive)
  148.   hyper_v.IScsiDrive.__index = hyper_v.IScsiDrive
  149.   return obj
  150. end
  151.  
  152. -- Constructor function for MacAddress class
  153. function hyper_v.MacAddress.new(mac)
  154.     local self = setmetatable({}, hyper_v.MacAddress)
  155.     self.mac = mac
  156.     return self
  157. end
  158.  
  159. -- Method to validate the MAC address format and check if it is a unicast MAC address
  160. function hyper_v.MacAddress:validate()
  161.     local pattern = "^([0-9A-Fa-f][0-9A-Fa-f](-|%|:)){5}[0-9A-Fa-f][0-9A-Fa-f]$"
  162.     if string.match(self.mac, pattern) then
  163.         return true
  164.     else
  165.         return false
  166.     end
  167. end
  168.  
  169. -- Method to convert the MacAddress object to its string representation
  170. function hyper_v.MacAddress:toString(lowercase)
  171.     if lowercase then
  172.         return string.lower(self.mac)
  173.     else
  174.         return self.mac
  175.     end
  176. end
  177.  
  178. -- Method to convert the MacAddress object to its string representation using a separator
  179. function hyper_v.MacAddress:toStringWithSeparator(separator, lowercase)
  180.     local formattedMac = string.gsub(self.mac, "[-%:]", separator)
  181.     if lowercase then
  182.         return string.lower(formattedMac)
  183.     else
  184.         return formattedMac
  185.     end
  186. end
  187.  
  188. -- Constructor function for NetworkAdapter class
  189. function hyper_v.NetworkAdapter.new(virtualSwitch)
  190.     local self = setmetatable({}, hyper_v.NetworkAdapter)
  191.     self.IpsecOffloading = true
  192.     self.IpsecSecurityAssociations = 512
  193.     self.PortMirroringMode = "None"
  194.     self.ProtectedNetwork = true
  195.     self.Vmq = true
  196.     self.VirtualSwitch = virtualSwitch
  197.     return self
  198. end
  199.  
  200. -- Method to set VLAN ID for NetworkAdapter
  201. function hyper_v.NetworkAdapter:setVlanId(value)
  202.     if value < 1 or value > 4094 then
  203.         error("VlanId must be between 1 and 4094.")
  204.     end
  205.     self.Vlan = true
  206.     self.vlanId = value
  207. end
  208.  
  209. -- Method to set Maximum Bandwidth for NetworkAdapter
  210. function hyper_v.NetworkAdapter:setMaximumBandwidth(value)
  211.     if value > 999999999 then
  212.         error("MaximumBandwidth must be between 0 and 999999999.")
  213.     end
  214.     self.maximumBandwidth = value
  215. end
  216.  
  217. -- Method to set Minimum Bandwidth for NetworkAdapter
  218. function hyper_v.NetworkAdapter:setMinimumBandwidth(value)
  219.     if value > 999999999 then
  220.         error("MinimumBandwidth must be between 0 and 999999999.")
  221.     end
  222.     self.minimumBandwidth = value
  223. end
  224.  
  225. function hyper_v.ScsiController.new()
  226.     local self = setmetatable({}, hyper_v.ScsiController)
  227.     self.Drives = {}
  228.     return self
  229. end
  230.  
  231. function hyper_v.ScsiController:init()
  232.     for i = 1, 64 do
  233.         table.insert(self.Drives, i)
  234.     end
  235. end
  236.  
  237. function hyper_v.WmiHelpers.Dispose(array)
  238.     for _, managementObject in ipairs(array) do
  239.         managementObject:Dispose()
  240.     end
  241. end
  242.  
  243. function hyper_v.WmiHelpers.First(collection)
  244.     for _, managementObject in ipairs(collection) do
  245.         return managementObject
  246.     end
  247.     return nil
  248. end
  249.  
  250. function hyper_v.WmiHelpers.ToObjectArray(managementStrings)
  251.     local managementObjects = {}
  252.     for index = 1, #managementStrings do
  253.         managementObjects[index] = managementStrings[index]
  254.     end
  255.     return managementObjects
  256. end
  257.  
  258. function hyper_v.VirtualDvdDrive:new()
  259.     local self = setmetatable({}, hyper_v.VirtualDvdDrive)
  260.     self.VirtualDvdDisk = nil
  261.     return self
  262. end
  263.  
  264. function hyper_v.VirtualDvdDrive:newWithDisk(virtualDvdDisk)
  265.     local self = setmetatable({}, hyper_v.VirtualDvdDrive)
  266.     self.VirtualDvdDisk = virtualDvdDisk
  267.     return self
  268. end
  269.  
  270. function hyper_v.VirtualDvdDisk:new(path)
  271.     local self = setmetatable({}, hyper_v.VirtualDvdDisk)
  272.     self.Path = path
  273.     return self
  274. end
  275.  
  276. function hyper_v.VirtualHardDisk:new()
  277.     local vhd = {}
  278.     setmetatable(vhd, hyper_v.VirtualHardDisk)
  279.     vhd.Format = nil
  280.     vhd.Path = nil
  281.     vhd.size = 0
  282.     return vhd
  283. end
  284.  
  285. function hyper_v.VirtualHardDisk:SetSize(value)
  286.     if self.Format == "Vhd" then
  287.         if value < 1 or value > 2040 then
  288.             error("Size must be between 1 and 2040.")
  289.         end
  290.     elseif self.Format == "Vhdx" then
  291.         if value < 1 or value > 65536 then
  292.             error("Size must be between 1 and 65536.")
  293.         end
  294.     end
  295.     self.size = value
  296. end
  297.  
  298. function hyper_v.VirtualHardDrive:new()
  299.     local vhd = {}
  300.     setmetatable(vhd, hyper_v.VirtualHardDrive)
  301.     vhd.MaximumIOPS = 0
  302.     vhd.MinimumIOPS = 0
  303.     vhd.VirtualHardDisk = nil
  304.     return vhd
  305. end
  306.  
  307. function hyper_v.VirtualHardDrive:SetMaximumIOPS(value)
  308.     if value < 0 or value > 1000000000 then
  309.         error("MaximumIOPS must be between 0 and 1000000000.")
  310.     end
  311.     self.MaximumIOPS = value
  312. end
  313.  
  314. function hyper_v.VirtualHardDrive:SetMinimumIOPS(value)
  315.     if value < 0 or value > 1000000000 then
  316.         error("MinimumIOPS must be between 0 and 1000000000.")
  317.     end
  318.     self.MinimumIOPS = value
  319. end
  320.  
  321. function hyper_v.VirtualHardDrive:AttachVirtualHardDisk(virtualHardDisk)
  322.     self.VirtualHardDisk = virtualHardDisk
  323. end
  324.  
  325. function hyper_v.VirtualHardDrive:__call(virtualHardDisk, minimumIOPS, maximumIOPS)
  326.     local vhd = {}
  327.     setmetatable(vhd, hyper_v.VirtualHardDrive)
  328.     vhd.VirtualHardDisk = virtualHardDisk
  329.     vhd.MinimumIOPS = minimumIOPS
  330.     vhd.MaximumIOPS = maximumIOPS
  331.     return vhd
  332. end
  333.  
  334. return hyper_v
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement