Module: VirtualBox

Defined in:
lib/virtual_box.rb,
lib/virtual_box/vm.rb,
lib/virtual_box/cli.rb,
lib/virtual_box/net.rb,
lib/virtual_box/error.rb,
lib/virtual_box/vm/nic.rb,
lib/virtual_box/version.rb,
lib/virtual_box/vm/disk.rb,
lib/virtual_box/net/dhcp.rb,
lib/virtual_box/vm/board.rb,
lib/virtual_box/vm/io_bus.rb

Overview

VirtualBox version detection.

Defined Under Namespace

Classes: Error, Net, Vm

Class Method Summary collapse

Class Method Details

.ose?Boolean

True if the installed VirtualBox is the open-source edition.

The open-source edition of VirtualBox has some limitations, such as no support for RDP and USB devices.

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/virtual_box/version.rb', line 10

def self.ose?
  unless version[:edition]
    raise 'VirtualBox is not installed on this machine.'
  end
  version[:edition] == 'OSE'
end

.reset_version_info!NilObject

Removes the cached information on the VirtualBox package version.

Returns:

  • (NilObject)

    nil



56
57
58
# File 'lib/virtual_box/version.rb', line 56

def self.reset_version_info!
  @version_info = nil
end

.run_command(args) ⇒ Hash<Symbol, String>

Runs a command in a sub-shell, waiting until the command completes.

Parameters:

  • args (Array<String>)

    the name and arguments for the command to be run

Returns:

  • (Hash<Symbol, String>)

    hash with the following keys:

    :status

    the command’s exit status

    :output

    a string with the command’s output



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/virtual_box/cli.rb', line 14

def self.run_command(args)
  begin
    output = Kernel.`(Shellwords.shelljoin(args) + ' 2>&1')
    { :status => $CHILD_STATUS.exitstatus, :output => output }

    # TODO(pwnall): this should work, but it makes VirtualBox slow as hell
    # child = POSIX::Spawn::Child.new(*args)
    # Hashie::Mash.new :status => child.status.exitstatus, :output => child.out
  rescue SystemCallError
    { :status => -1, :output => '' }
  end
end

.run_command!(args) ⇒ String

Runs a command in a sub-shell, waiting until the command completes.

Parameters:

  • args (Array<String>)

    the name and arguments for the command to be run

Returns:

  • (String)

    the command’s output

Raises:



32
33
34
35
36
# File 'lib/virtual_box/cli.rb', line 32

def self.run_command!(args)
  result = run_command args
  raise VirtualBox::Error, result unless result[:status] == 0
  result[:output]
end

.versionHash<Symbol, Object>, Boolean

Version information about the VirtualBox package installed on this machine.

Returns:

  • (Hash<Symbol, Object>, Boolean)

    false if VirtualBox is not installed; otherwise, a hash with the following keys:

    :svn

    (number) the SVN revision that VirtualBox is built off of

    :edition

    the VirtualBox edition (” for the personal edition, ‘OSE’ for the open-source edition)

    :release

    the public release number (e.g. ‘3.0.4’)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/virtual_box/version.rb', line 25

def self.version
  return @version_info unless @version_info.nil?
  
  cmd_result = run_command ['VBoxManage', '--version']
  if cmd_result[:status] != 0
    @version_info = {}
    return @version_info
  end
  
  output = cmd_result[:output].strip

  if revision_offset = output.rindex('r')
    revision = output[revision_offset + 1, output.length].to_i
    output.slice! revision_offset..-1
  else
    revision = nil
  end
  
  if edition_offset = output.rindex('_')
    edition = output[edition_offset + 1, output.length]
    output.slice! edition_offset..-1
  else
    edition = ''
  end
  
  @version_info = { :release => output, :svn => revision,
                    :edition => edition }
end