Class: VagrantPlugins::ProviderKvm::Util::VmDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-kvm/util/vm_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, source_type = 'libvirt') ⇒ VmDefinition

Returns a new instance of VmDefinition.



40
41
42
43
44
45
46
47
48
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 40

def initialize(definition, source_type='libvirt')
  @uuid = nil
  @network = 'default'
  if source_type == 'ovf'
    create_from_ovf(definition)
  else
    create_from_libvirt(definition)
  end
end

Instance Attribute Details

#archObject (readonly)

Returns the value of attribute arch.



15
16
17
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 15

def arch
  @arch
end

#cpusObject (readonly)

Returns the value of attribute cpus.



12
13
14
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 12

def cpus
  @cpus
end

#diskObject

Returns the value of attribute disk.



13
14
15
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 13

def disk
  @disk
end

#macObject (readonly)

Returns the value of attribute mac.



14
15
16
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 14

def mac
  @mac
end

#nameObject (readonly)

Attributes of the VM



11
12
13
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 11

def name
  @name
end

#networkObject (readonly)

Returns the value of attribute network.



16
17
18
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 16

def network
  @network
end

Class Method Details

.list_interfaces(definition) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 18

def self.list_interfaces(definition)
  nics = {}
  ifcount = 0
  doc = Nokogiri::XML(definition)
  # look for user mode interfaces
  doc.css("devices interface[type='user']").each do |item|
    ifcount += 1
    adapter = ifcount
    nics[adapter] ||= {}
    nics[adapter][:type] = :user
  end
  # look for interfaces on virtual network
  doc.css("devices interface[type='netwok']").each do |item|
    ifcount += 1
    adapter = ifcount
    nics[adapter] ||= {}
    nics[adapter][:network] = item.at_css("source")["network"]
    nics[adapter][:type] = :network
  end
  nics
end

Instance Method Details

#as_libvirtObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 97

def as_libvirt
  # RedHat and Debian-based systems have different executable names
  # depending on version/architectures
  qemu_bin = [ '/usr/bin/qemu-kvm', '/usr/bin/kvm' ]
  qemu_bin << '/usr/bin/qemu-system-x86_64' if @arch.match(/64$/)
  qemu_bin << '/usr/bin/qemu-system-i386'   if @arch.match(/^i.86$/)

  xml = KvmTemplateRenderer.render("libvirt_domain", {
    :name => @name,
    :uuid => @uuid,
    :memory => size_from_bytes(@memory, "KiB"),
    :cpus => @cpus,
    :arch => @arch,
    :disk => @disk,
    :mac => format_mac(@mac),
    :network => @network,
    :qemu_bin => qemu_bin.detect { |binary| File.exists? binary }
  })
  xml
end

#create_from_libvirt(definition) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 83

def create_from_libvirt(definition)
  doc = Nokogiri::XML(definition)
  @name = doc.at_css("domain name").content
  @uuid = doc.at_css("domain uuid").content if doc.at_css("domain uuid")
  memory_unit = doc.at_css("domain memory")["unit"]
  @memory = size_in_bytes(doc.at_css("domain memory").content,
                          memory_unit)
  @cpus = doc.at_css("domain vcpu").content
  @arch = doc.at_css("domain os type")["arch"]
  @disk = doc.at_css("devices disk source")["file"]
  @mac = doc.at_css("devices interface mac")["address"]
  @network = doc.at_css("devices interface source")["network"]
end

#create_from_ovf(definition) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 50

def create_from_ovf(definition)
  doc = Nokogiri::XML(definition)
  # we don't need no namespace
  doc.remove_namespaces!
  @name = doc.at_css("VirtualSystemIdentifier").content
  devices = doc.css("VirtualHardwareSection Item")
  for device in devices
    case device.at_css("ResourceType").content
      # CPU
    when "3"
      @cpus = device.at_css("VirtualQuantity").content
      # Memory
    when "4"
      @memory = size_in_bytes(device.at_css("VirtualQuantity").content,
                              device.at_css("AllocationUnits").content)
    end
  end

  # disk volume
  diskref = doc.at_css("DiskSection Disk")["fileRef"]
  @disk = doc.at_css("References File[id='#{diskref}']")["href"]

  # mac address
  # XXX we use only the first nic
  @mac = format_mac(doc.at_css("Machine Hardware Adapter[enabled='true']")['MACAddress'])

  # the architecture is not defined in the ovf file
  # we try to guess from OSType
  # see https://www.virtualbox.org/browser/vbox/trunk/src/VBox/Main/include/ovfreader.h
  @arch = doc.at_css("VirtualSystemIdentifier").
    content[-2..-1] == '64' ? "x86_64" : "i686"
end

#format_mac(mac) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 187

def format_mac(mac)
  if mac.length == 12
    mac = mac[0..1] + ":" + mac[2..3] + ":" +
      mac[4..5] + ":" + mac[6..7] + ":" +
      mac[8..9] + ":" + mac[10..11]
  end
  mac
end

#get_memory(unit = "bytes") ⇒ Object



118
119
120
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 118

def get_memory(unit="bytes")
  size_from_bytes(@memory, unit)
end

#set_mac(mac) ⇒ Object



122
123
124
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 122

def set_mac(mac)
  @mac = format_mac(mac)
end

#size_from_bytes(qty, unit) ⇒ Object

Takes a qty and a unit returns byte quantity in that unit



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 165

def size_from_bytes(qty, unit)
  qty = qty.to_i
  case unit.downcase
  when "b", "bytes"
    qty.to_s
  when "kb", "kilobytes"
    (qty / 1000).to_s
  when "kib", "kibibytes"
    (qty / 1024).to_s
  when "mb", "megabytes"
    (qty / 1000000).to_s
  when "m", "mib", "mebibytes"
    (qty / 1048576).to_s
  when "gb", "gigabytes"
    (qty / 1000000000).to_s
  when "g", "gib", "gibibytes"
    (qty / 1073741824).to_s
  else
    raise ArgumentError, "Unknown unit #{unit}"
  end
end

#size_in_bytes(qty, unit, mib = false) ⇒ Object

Takes a quantity and a unit returns quantity in bytes mib = true to use mebibytes, etc defaults to false because ovf MB != megabytes



130
131
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
# File 'lib/vagrant-kvm/util/vm_definition.rb', line 130

def size_in_bytes(qty, unit, mib=false)
  qty = qty.to_i
  unit = unit.downcase
  if !mib
    case unit
    when "kb", "kilobytes"
      unit = "kib"
    when "mb", "megabytes"
      unit = "mib"
    when "gb", "gigabytes"
      unit = "gib"
    end
  end
  case unit
  when "b", "bytes"
    qty.to_s
  when "kb", "kilobytes"
    (qty * 1000).to_s
  when "kib", "kibibytes"
    (qty * 1024).to_s
  when "mb", "megabytes"
    (qty * 1000000).to_s
  when "m", "mib", "mebibytes"
    (qty * 1048576).to_s
  when "gb", "gigabytes"
    (qty * 1000000000).to_s
  when "g", "gib", "gibibytes"
    (qty * 1073741824).to_s
  else
    raise ArgumentError, "Unknown unit #{unit}"
  end
end