Class: VirtualBox::Vm::Nic

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

Overview

Configuration for a network card.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Nic

Creates a NIC with the given attributes.

Parameters:

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

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



78
79
80
# File 'lib/virtual_box/vm/nic.rb', line 78

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

Instance Attribute Details

#chipSymbol

The NIC controller chip.

Can be one of the following values:

:amd

AMD PCNet FAST III (good default)

:intel

Intel PRO/1000 MT Server (for newer Windows systems)

:intel_xp

Intel PRO/1000 MT Server (for Windows XP)

:virtio

fake card optimized for virtualization (custom drivers needed)

Returns:

  • (Symbol)


27
28
29
# File 'lib/virtual_box/vm/nic.rb', line 27

def chip
  @chip
end

#macString

MAC address for the network card, as a hexadecimal string.

The format for specifying MACs is ‘0123456789AB’. A random MAC will be generated if one is not assigned.

Returns:

  • (String)


44
45
46
# File 'lib/virtual_box/vm/nic.rb', line 44

def mac
  @mac
end

#modeSymbol

The kind of network emulation implemented on this card.

Can be one of the following values:

:nat

uses VirtualBox internal NAT engine to hide under host OS

:bridged

bypasses host OS, connects directly to a network interface

:host

virtual network connecting guest to host

:virtual

virtual network connecting multiple guests

Returns:

  • (Symbol)


17
18
19
# File 'lib/virtual_box/vm/nic.rb', line 17

def mode
  @mode
end

#net_nameSymbol

Name of the virtual network that the NIC is connected to.

The identifier differs depending on the networking mode:

:nat

not applicable

:bridged

name of the bridge network interface on the host

:host

name of the host-only network interface

:virtual

virtual network name

Returns:

  • (Symbol)


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

def net_name
  @net_name
end

#trace_fileString

Path to a file that logs a network trace for the VM.

Can be null to disable tracing.

Returns:

  • (String)


50
51
52
# File 'lib/virtual_box/vm/nic.rb', line 50

def trace_file
  @trace_file
end

Instance Method Details

#from_params(params, nic_id) ⇒ VirtualBox::Vm::Nic

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

Parameters:

  • params (Hash<String, String>)

    the “VBoxManage showvminfo” output, parsed by Vm.parse_machine_readble

  • nic_id (Integer)

    the NIC’s number in the VM

Returns:



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
166
167
168
169
170
# File 'lib/virtual_box/vm/nic.rb', line 134

def from_params(params, nic_id)
  case params["nic#{nic_id}"]
  when 'nat'
    self.mode = :nat
  when 'bridged'
    self.mode = :bridged
    self.net_name = params["bridgeadapter#{nic_id}"]
  when 'intnet'
    self.mode = :virtual
    self.net_name = params["intnet#{nic_id}"]
  when 'hostonly'
    self.mode = :host
    self.net_name = params["hostonlyadapter#{nic_id}"]    
  end
  
  self.chip = case params["nictype#{nic_id}"]
  when 'Am79C970A', 'Am79C973',
    :amd
  when '82543GC'
    :intel_xp
  when '82540OEM', '82545EM'
    :intel
  when 'virtio'
    :virtual
  else
    (self.mode == :virtual) ? :virtual : :amd
  end
  
  self.mac = params["macaddress#{nic_id}"]
  if params["nictrace#{nic_id}"] == 'on'
    self.trace_file = params["nictracefile#{nic_id}"]
  else
    self.trace_file = nil
  end
 
  self
end

#to_hashHash<Symbol, Object>

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

Returns:

  • (Hash<Symbol, Object>)

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



176
177
178
179
# File 'lib/virtual_box/vm/nic.rb', line 176

def to_hash
  { :mode => mode, :chip => chip, :net_name => net_name, :mac => mac,
    :trace_file => trace_file }
end

#to_params(nic_id) ⇒ Array<String>

Arguments to “VBoxManage modifyvm” describing the NIC.

Parameters:

  • nic_id (Number)

    the number of the card (1-4) connected to the host

Returns:

  • (Array<String>)

    arguments that can be concatenated to a “VBoxManage modifyvm” command to express this NIC specification



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/virtual_box/vm/nic.rb', line 87

def to_params(nic_id)
  params = []
      
  params.push "--nic#{nic_id}"
  case mode
  when :nat
    params.push 'nat'
  when :bridged
    params.push 'bridged', "--bridgeadapter#{nic_id}", net_name
  when :virtual
    params.push 'intnet', "--intnet#{nic_id}", net_name
  when :host
    params.push 'hostonly', "--hostonlyadapter#{nic_id}", net_name
  else
    params.push 'null'
  end
  
  params.push "--nictype#{nic_id}", case chip
  when :amd
    'Am79C973'
  when :intel
    '82545EM'
  when :intel_xp
    '82543GC'
  when :virtual
    'virtio'
  end      
  
  params.push "--cableconnected#{nic_id}", 'on'    
  params.push "--macaddress#{nic_id}", mac if mac
  
  params.push "--nictrace#{nic_id}"
  if trace_file
    params.push 'on', "--nictracefile#{nic_id}", trace_file
  else
    params.push 'off'
  end
  
  params
end