Class: Virtuoso::VirtualBox::VM

Inherits:
API::VM
  • Object
show all
Defined in:
lib/virtuoso/virtualbox/vm.rb

Overview

VirtualBox VM.

Instance Attribute Summary

Attributes inherited from API::VM

#connection, #disk_image, #domain, #memory, #name

Instance Method Summary collapse

Methods inherited from API::VM

#initialize

Constructor Details

This class inherits a constructor from Virtuoso::API::VM

Instance Method Details

#destroyObject



56
57
58
59
60
# File 'lib/virtuoso/virtualbox/vm.rb', line 56

def destroy
  requires_existing_vm
  @domain.undefine
  @domain = nil
end

#reloadObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/virtuoso/virtualbox/vm.rb', line 72

def reload
  # Load the main disk image path. We assume this is the first "disk"
  # device, though this assumption is probably pretty weak.
  spec = domain.spec
  disk = spec.devices.find { |d| d.is_a?(Libvirt::Spec::Device::Disk) }
  self.disk_image = disk.source

  # Load the basic attributes
  self.name = spec.name
  self.memory = spec.memory
end

#saveObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
53
54
# File 'lib/virtuoso/virtualbox/vm.rb', line 9

def save
  # Setup the basic settings for the VM
  d = Libvirt::Spec::Domain.new
  d.hypervisor = :vbox
  d.name = name
  d.memory = memory
  d.vcpu = 1
  d.features = [:acpi, :pae]
  d.clock.offset = :localtime
  d.os.type = :hvm
  d.os.arch = :i386
  d.os.boot = [:cdrom, :hd]

  # Attach the main hard disk
  disk = Libvirt::Spec::Device.get(:disk).new
  disk.type = :file
  disk.device = :disk
  disk.source = disk_image
  disk.target_dev = :hda
  disk.target_bus = :ide
  d.devices << disk

  # Attach a basic NAT network interface
  nat = Libvirt::Spec::Device.get(:interface).new
  nat.type = :user
  nat.mac_address = "08:00:27:8f:7a:9f"
  nat.model_type = "82540EM"
  d.devices << nat

  # Attach video information
  video = Libvirt::Spec::Device.get(:video).new
  model = Libvirt::Spec::Device::VideoModel.new
  model.type = :vbox
  model.vram = 12
  model.heads = 1
  model.accel3d = false
  model.accel2d = false
  video.models << model
  d.devices << video

  # At this point, assuming the virtuoso settings are correct, we
  # should have a bootable VM spec, so define it and reload the VM
  # information.
  @domain = connection.domains.define(d)
  reload
end

#startObject



62
63
64
65
# File 'lib/virtuoso/virtualbox/vm.rb', line 62

def start
  requires_existing_vm
  domain.start
end

#stateObject



5
6
7
# File 'lib/virtuoso/virtualbox/vm.rb', line 5

def state
  domain ? domain.state : :new
end

#stopObject



67
68
69
70
# File 'lib/virtuoso/virtualbox/vm.rb', line 67

def stop
  requires_existing_vm
  domain.stop
end