Class: VirtualBox::Vm::IoBus

Inherits:
Object
  • Object
show all
Defined in:
lib/virtual_box/vm/io_bus.rb

Overview

Specification for a IO controller attached to a virtual machine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ IoBus

Creates a new IO controller specification based on the given attributes.

Parameters:

  • options (Hash<Symbol, Object>) (defaults to: {})

    ActiveRecord-style initial values for attributes; can be used together with IoBus#to_hash to save and restore



232
233
234
235
# File 'lib/virtual_box/vm/io_bus.rb', line 232

def initialize(options = {})
  @disks = {}
  options.each { |k, v| self.send :"#{k}=", v }
end

Instance Attribute Details

#bootableBoolean

True if the VM BIOS considers this controller bootable.

Returns:

  • (Boolean)


33
34
35
# File 'lib/virtual_box/vm/io_bus.rb', line 33

def bootable
  @bootable
end

#busSymbol

The kind of bus used by this controller.

The following bus types are recognized: :ide, :sata, :sas, :scsi, :floppy.

Returns:

  • (Symbol)


18
19
20
# File 'lib/virtual_box/vm/io_bus.rb', line 18

def bus
  @bus
end

#chipSymbol

The chipset simulated by the IO controller

The following chipsets are recogniezd:

IDE

:piix3, :piix4, and :ich6

SATA

:ahci (Intel AHCI)

SCSI

:lsi_logic and :bus_logic

SAS

lsi_logic_sas

Floppy

:i82078

Returns:

  • (Symbol)


29
30
31
# File 'lib/virtual_box/vm/io_bus.rb', line 29

def chip
  @chip
end

#disksHash<Array<Integer>, VirtualBox::Vm::Disk>

The disks connected to this controller’s bus.

Returns:



45
46
47
# File 'lib/virtual_box/vm/io_bus.rb', line 45

def disks
  @disks
end

#max_portsInteger

The maximum number of I/O devices supported by this controller.

Returns:

  • (Integer)


41
42
43
# File 'lib/virtual_box/vm/io_bus.rb', line 41

def max_ports
  @max_ports
end

#nameString

A user-friendly name for the I/O controller.

I/O controller names must be unique within the scope of a virtual machine. VirtualBox uses “IDE Controller” and “SATA Controller” as default names.

Returns:

  • (String)


12
13
14
# File 'lib/virtual_box/vm/io_bus.rb', line 12

def name
  @name
end

#no_cacheBoolean

True if the controller’s I/O bypasses the host OS cache.

Returns:

  • (Boolean)


37
38
39
# File 'lib/virtual_box/vm/io_bus.rb', line 37

def no_cache
  @no_cache
end

Instance Method Details

#add_bus_to(vm) ⇒ VirtualBox::Vm::IoBus

Adds this IO bus to a virtual machine.

Parameters:

  • vm (VirtualBox::Vm)

    the virtual machine that this IO bus will be added to

Returns:



222
223
224
225
226
# File 'lib/virtual_box/vm/io_bus.rb', line 222

def add_bus_to(vm)
  VirtualBox.run_command! ['VBoxManage', '--nologo', 'storagectl',
                           vm.uid].concat(to_params)
  self
end

#add_to(vm) ⇒ VirtualBox::Vm::IoBus

Adds this IO bus and all its disks to a virtual machine.

Parameters:

  • vm (VirtualBox::Vm)

    the virtual machine that this IO controller will be added to

Returns:



198
199
200
201
202
203
204
# File 'lib/virtual_box/vm/io_bus.rb', line 198

def add_to(vm)
  add_bus_to vm
  disks.each do |port_device, disk|
    disk.add_to vm, self, port_device.first, port_device.last
  end
  self
end

#first_free_portInteger

Finds an unused port on this IO controller’s bus.

Returns:

  • (Integer)

    a port number that a new disk can be attached to



252
253
254
# File 'lib/virtual_box/vm/io_bus.rb', line 252

def first_free_port
  disks.empty? ? 0 : disks.keys.min.first + 1
end

#from_params(params, bus_id) ⇒ VirtualBox::Vm::IoBus

Parses “VBoxManage showvminfo –machinereadable” output into this instance.

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/virtual_box/vm/io_bus.rb', line 132

def from_params(params, bus_id)
  self.name = params["storagecontrollername#{bus_id}"]
  self.bootable = params["storagecontrollerbootable#{bus_id}"] == 'on'
  self.max_ports = params["storagecontrollermaxportcount#{bus_id}"].to_i
  case params["storagecontrollertype#{bus_id}"]
  when 'PIIX3'
    self.chip = :piix3
  when 'PIIX4'
    self.chip = :piix4
  when 'ICH6'
    self.chip = :ich6      
  when 'IntelAhci'
    self.chip = :ahci
  when 'LsiLogic'
    self.chip = :lsi_logic
  when 'BusLogic'
    self.chip = :bus_logic
  when 'LSILogicSAS'
    self.chip = :lsi_logic_sas
  when 'I82078'
    self.chip = :i82078
  end
  self.no_cache = nil
  
  image_re = /\A#{name}-(\d+)-(\d+)\Z/
  @disks = {}
  params.each do |key, value|
    next unless match = image_re.match(key)
    next if value == 'none'
    port, device = match[1].to_i, match[2].to_i
    @disks[[port, device]] = VirtualBox::Vm::Disk.new :file => value
  end
  self
end

#remove_from(vm) ⇒ VirtualBox::Vm::IoBus

Removes this IO bus from a virtual machine.

Parameters:

  • vm (VirtualBox::Vm)

    the virtual machine that this IO controller will be removed from

Returns:



211
212
213
214
215
# File 'lib/virtual_box/vm/io_bus.rb', line 211

def remove_from(vm)
  VirtualBox.run_command! ['VBoxManage', '--nologo', 'storagectl', vm.uuid,
                           '--name', name, '--remove']
  self
end

#to_hashHash<Symbol, Object>

Hash capturing this specification. Can be passed to IoBus#new.

Returns:

  • (Hash<Symbol, Object>)

    Ruby-friendly Hash that can be used to re-create this IO controller specification



241
242
243
244
245
246
247
248
# File 'lib/virtual_box/vm/io_bus.rb', line 241

def to_hash
  disk_hashes = disks.map do |port_device, disk|
    disk.to_hash.merge! :port => port_device.first,
                        :device => port_device.last
  end
  { :name => name, :bus => bus, :chip => chip, :bootable => bootable,
    :no_cache => no_cache, :max_ports => max_ports, :disks => disk_hashes }
end

#to_paramsArray<String>

Parameters for “VBoxManage storagectl” to add this IO bus to a VM.

Returns:

  • (Array<String>)

    the parameters for a “VBoxManage storagectl” command that get this IO bus added to a VM.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/virtual_box/vm/io_bus.rb', line 171

def to_params
  params = []
  params.push '--name', name
  params.push '--add', bus.to_s
  params.push '--controller', case chip
  when :piix3, :piix4, :ich6, :i82078
    chip.to_s.upcase
  when :ahci
    'IntelAhci'
  when :lsi_logic
    'LsiLogic'
  when :bus_logic
    'BusLogic'
  when :lsi_logic_sas
    'LSILogicSAS'
  end
  params.push '--sataportcount', max_ports.to_s
  params.push '--hostiocache', (no_cache ? 'off' : 'on')
  params.push '--bootable', (bootable ? 'on' : 'off')
end