Class: VagrantPlugins::ProviderKvm::Driver::Driver

Inherits:
Object
  • Object
show all
Includes:
Errors, Util, Util::Commands
Defined in:
lib/vagrant-kvm/driver/driver.rb

Defined Under Namespace

Classes: VMNotFound

Constant Summary collapse

VM_STATE =

enum for states return by libvirt

[
:no_state,
:running,
:blocked,
:paused,
:shutdown,
:shutoff,
:crashed]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Commands

#run_command, #run_root_command

Constructor Details

#initialize(uuid = nil) ⇒ Driver

Returns a new instance of Driver.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant-kvm/driver/driver.rb', line 37

def initialize(uuid=nil)
  @logger = Log4r::Logger.new("vagrant::provider::kvm::driver")
  @uuid = uuid
  @virsh_path = "virsh"
  @pool_migrate = false

  load_kvm_module!
  connect_libvirt_qemu!

  if @uuid
    # Verify the VM exists, and if it doesn't, then don't worry
    # about it (mark the UUID as nil)
    raise VMNotFound if !vm_exists?(@uuid)
  end
end

Instance Attribute Details

#nameObject (readonly)

The Name of the virtual machine we represent



29
30
31
# File 'lib/vagrant-kvm/driver/driver.rb', line 29

def name
  @name
end

#pool_migrateObject (readonly)

Vagrant 1.5.x pool migration



35
36
37
# File 'lib/vagrant-kvm/driver/driver.rb', line 35

def pool_migrate
  @pool_migrate
end

#uuidObject (readonly)

The UUID of the virtual machine we represent



32
33
34
# File 'lib/vagrant-kvm/driver/driver.rb', line 32

def uuid
  @uuid
end

Instance Method Details

#activate_storage_pool(pool_name) ⇒ Object

Activate the driver’s storage pool



260
261
262
263
264
265
266
267
268
# File 'lib/vagrant-kvm/driver/driver.rb', line 260

def activate_storage_pool(pool_name)
  # Get storage pool if it exists
  @pool = init_storage_pool(pool_name)
  # check neccesity of migrating to new storage pool format
  # this is happen when user has already used vagrant-kvm 0.1.5 and after
  # XXX needs clarification
  check_migrate_box_storage_pool
  @logger.info("Retrieving storage pool #{pool_name}")
end

#add_nic(nic) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
# File 'lib/vagrant-kvm/driver/driver.rb', line 510

def add_nic(nic)
  domain = @conn.lookup_domain_by_uuid(@uuid)
  # Use DOMAIN_XML_SECURE to dump ALL options (including VNC password)
  original_xml = domain.xml_desc(Libvirt::Domain::DOMAIN_XML_SECURE)
  definition = Util::VmDefinition.new(original_xml)
  definition.add_nic(nic)
  domain.undefine
  xml = definition.as_xml
  @logger.debug("add nic\nFrom #{original_xml} \nTo: #{xml}")
  @conn.define_domain_xml(xml)
end

#alive!Object

Verifies that the connection is alive introduced from ruby-libvirt 0.5.0

This will raise a VagrantError if things are not ready.



609
610
611
612
613
# File 'lib/vagrant-kvm/driver/driver.rb', line 609

def alive!
  unless @conn.alive?
    raise Vagrant::Errors::KvmNoConnection
  end
end

#can_save?Boolean

Returns:

  • (Boolean)


549
550
551
552
553
554
# File 'lib/vagrant-kvm/driver/driver.rb', line 549

def can_save?
  domain = @conn.lookup_domain_by_uuid(@uuid)
  definition = Util::VmDefinition.new(domain.xml_desc)
  disk_bus = definition.get(:disk_bus)
  return disk_bus != 'sata'
end

#check_migrate_box_storage_poolObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/vagrant-kvm/driver/driver.rb', line 329

def check_migrate_box_storage_pool
  # Migration to new pool directory structure in vagrant 1.5.x
  # delete if created with <vagrant-1.4.x
  if Vagrant::VERSION >= "1.5.0"
    begin
      if @pool && @pool.persistent?
        # pool was made by vagrant-kvm-0.1.5 and before
        # vagrant-kvm-0.1.5 NOT working in vagrant 1.5.x
        # so need migrate
        @pool_migrate = true
      end
    rescue Libvirt::RetrieveError
      raise Errors::KvmFailStoragePool
    end
  end
end

#clear_shared_foldersObject



494
495
496
# File 'lib/vagrant-kvm/driver/driver.rb', line 494

def clear_shared_folders
  #stub
end

#create_network(config) ⇒ Object

Create network



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/vagrant-kvm/driver/driver.rb', line 169

def create_network(config)
  # Get the network if it exists
  network_name = config[:name]
  hosts = config[:hosts]
  @logger.info("empty host!") unless hosts
  begin
    network = @conn.lookup_network_by_name(network_name)
    definition = Util::NetworkDefinition.new(network_name,
                                             network.xml_desc)
    old_def    = Util::NetworkDefinition.new(network_name,
                                             network.xml_desc)
    definition.update(config)
    @logger.info("Update network #{network_name}")
    @logger.debug("From:\n#{old_def.as_xml}")
    @logger.debug("TO:\n#{definition.as_xml}")

    # Only destroy existing network if config has changed. This is
    # necessary because other VM could be currently using this network
    # and will loose connectivity if the network is destroyed.
    if network.active?
      @logger.info "Reusing existing configuration for #{network_name}"
      update_command = Libvirt::Network::NETWORK_UPDATE_COMMAND_ADD_LAST
      hosts.each do |host|
        if old_def.already_exist_host?(host)
          update_command = Libvirt::Network::NETWORK_UPDATE_COMMAND_MODIFY
        end
      end
      if update_command == Libvirt::Network::NETWORK_UPDATE_COMMAND_MODIFY
          @logger.info ("Updating network #{network_name} using UPDATE_COMMAND_MODIFY")
      else
          @logger.info ("Updating network #{network_name} using UPDATE_COMMAND_ADD_LAST")
     end

     network.update(update_command,
      Libvirt::Network::NETWORK_SECTION_IP_DHCP_HOST,
      -1,
      definition.as_host_xml,
      Libvirt::Network::NETWORK_UPDATE_AFFECT_CURRENT
      )
     network.create unless network.active?
    else # network is not active
      @logger.info "Recreating network config for #{network_name}"
      network.undefine
      network = define_network(definition)
    end

  rescue Libvirt::RetrieveError
    # Network doesn't exist, create with defaults
    definition = Util::NetworkDefinition.new(network_name)
    definition.update(config)
    @logger.info("Creating network #{network_name}")
    @logger.debug("with\n#{definition.as_xml}")
    network = define_network(definition)
  end
end

#create_storage_pool(pool_name, pool_path, dir_mode) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/vagrant-kvm/driver/driver.rb', line 284

def create_storage_pool(pool_name, pool_path, dir_mode)
  @logger.info("Creating new storage pool #{pool_name} in #{pool_path}")
  # create dir if it doesn't exist
  FileUtils.mkpath(pool_path) unless Dir.exists?(pool_path)
  storage_pool_xml = <<-EOF
      <pool type="dir">
      <name>#{pool_name}</name>
      <target>
        <path>#{pool_path}</path>
        <permissions>
         <owner>#{Process.uid}</owner>
         <group>#{Process.gid}</group>
         <mode>#{dir_mode}</mode>
        </permissions>
      </target>
      </pool>
  EOF
  # Create transient pool
  # Pools defined here will be removed after system reboot.
  @logger.debug("Creating storage pool with XML:\n #{storage_pool_xml}")
  begin
    pool = @conn.create_storage_pool_xml(storage_pool_xml)
    pool.build unless pool.active?
    pool.refresh
    pool
  rescue
    # check conflict and shutdown old pool
    defined_pool_list = @conn.list_storage_pools
    defined_pool_list.each do |defined_pool_name|
      defined_pool = @conn.lookup_storage_pool_by_name(defined_pool_name)
      defined_pool_doc = REXML::Document.new defined_pool.xml_desc
      defined_pool_path = File.expand_path(defined_pool_doc.elements["/pool/target/path"].text)
      @logger.debug("check whether conflict with #{defined_pool_name} on #{defined_pool_path}")
      if defined_pool_path == File.expand_path(pool_path)
        defined_pool.destroy
        pool = @conn.create_storage_pool_xml(storage_pool_xml)
        pool.build unless pool.active?
        pool.refresh
        return pool
      end
    end
    raise Errors::KvmFailStoragePool
  end
end

#create_volume(args = {}) ⇒ Object

create empty volume in storage pool args: disk_name, capacity, path, image_type, box_pool, box_path,

backing, owner, group, mode, label


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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vagrant-kvm/driver/driver.rb', line 56

def create_volume(args={})
  args = { # default values
    :owner => '-1',
    :group => '-1',
    :mode  => '0744',
    :label => 'virt_image_t',
    :backing => false
  }.merge(args)
  msg = "Creating volume #{args[:disk_name]}"
  msg += " backed by volume #{args[:box_path]}" if args[:backing]
  capacity = args[:capacity]
  @logger.info(msg)
  storage_vol_xml = <<-EOF
  <volume>
    <name>#{args[:disk_name]}</name>
    <allocation>0</allocation>
    <capacity unit="#{capacity[:unit]}">#{capacity[:size]}</capacity>
    <target>
      <path>#{args[:path]}</path>
      <format type='#{args[:image_type]}'/>
      <permissions>
        <owner>#{args[:owner]}</owner>
        <group>#{args[:group]}</group>
        <mode>#{args[:mode]}</mode>
        <label>#{args[:label]}</label>
      </permissions>
    </target>
    EOF

  if args[:backing]
    storage_vol_xml += <<-EOF
    <backingStore>
      <path>#{args[:box_path]}</path>
      <format type='#{args[:image_type]}'/>
    </backingStore>
   EOF
  end
  storage_vol_xml += "</volume>"

  @logger.debug "Creating volume with XML:\n#{storage_vol_xml}"
  if args[:backing]
    vol = @pool.create_volume_xml(storage_vol_xml)
  else
    pool = @conn.lookup_storage_pool_by_name(args[:box_pool])
    clonevol = pool.lookup_volume_by_path(args[:box_path])
    # create_volume_xml_from() can convert disk image type automatically.
    vol = @pool.create_volume_xml_from(storage_vol_xml, clonevol)
  end
  @pool.refresh
end

#define_network(definition) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/vagrant-kvm/driver/driver.rb', line 225

def define_network(definition)
  xml = definition.as_xml
  @logger.debug "Defining new network with XML:\n#{xml}"
  network = @conn.define_network_xml(xml)
  network.create
  network
end

#deleteObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vagrant-kvm/driver/driver.rb', line 116

def delete
  domain = @conn.lookup_domain_by_uuid(@uuid)
  definition = Util::VmDefinition.new(domain.xml_desc)
  volume = @pool.lookup_volume_by_path(definition.attributes[:disk])
  volume.delete
  # XXX remove pool if empty?
  @pool.refresh
  # remove any saved state
  domain.managed_save_remove if domain.has_managed_save?
  domain.undefine
end

#execute_command(command) ⇒ Object



601
602
603
# File 'lib/vagrant-kvm/driver/driver.rb', line 601

def execute_command(command)
  raw(*command)
end

#export(xml_path) ⇒ Object

Export



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/vagrant-kvm/driver/driver.rb', line 563

def export(xml_path)
  @logger.info("FIXME: export has not tested yet.")
  new_disk = 'disk.img'
  # create new_disk
  domain = @conn.lookup_domain_by_uuid(@uuid)
  definition = Util::VmDefinition.new(domain.xml_desc)
  disk_image = definition.attributes[:disk]
  to_path = File.dirname(xml_path)
  new_path = File.join(to_path, new_disk)
  @logger.info("create disk image #{new_path}")
  run_command("qemu-img convert -c -S 4k -O qcow2 #{disk_image} #{new_path}")
  # write out box.xml
  definition.update(:disk => new_disk,:gui  => false,:uuid => nil)
  File.open(xml_path,'w') do |f|
    f.puts(definition.as_xml)
  end
  # write metadata.json
  json_path=File.join(to_path, 'metadata.json')
  File.open(json_path,'w') do |f|
    f.puts('{"provider": "kvm"}')
  end
end

#find_box_disk(xml) ⇒ Object



128
129
130
131
132
# File 'lib/vagrant-kvm/driver/driver.rb', line 128

def find_box_disk(xml)
  definition = File.open(xml) { |f|
    Util::VmDefinition.new(f.read) }
  definition.attributes[:disk]
end

#free_storage_pool(pool_name) ⇒ Object



346
347
348
349
350
351
352
353
354
355
# File 'lib/vagrant-kvm/driver/driver.rb', line 346

def free_storage_pool(pool_name)
  begin
    pool = @conn.lookup_storage_pool_by_name(pool_name)
    # XXX  check reference counter for parallel action?
    pool.destroy
    pool.free
  rescue Libvirt::RetrieveError
    @logger.info("fail to free storage pool #{pool_name}")
  end
end

#get_default_ipObject



233
234
235
# File 'lib/vagrant-kvm/driver/driver.rb', line 233

def get_default_ip
  "192.168.123.10"
end

#haltObject

Halts the virtual machine



135
136
137
138
# File 'lib/vagrant-kvm/driver/driver.rb', line 135

def halt
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.destroy
end

#host_arch?Boolean

Returns:

  • (Boolean)


672
673
674
# File 'lib/vagrant-kvm/driver/driver.rb', line 672

def host_arch?
  check_os_release?("/etc/arch-release")
end

#host_debian?Boolean

Returns:

  • (Boolean)


664
665
666
# File 'lib/vagrant-kvm/driver/driver.rb', line 664

def host_debian?
  check_os_release?("/etc/debian_version")
end

#host_gentoo?Boolean

Returns:

  • (Boolean)


668
669
670
# File 'lib/vagrant-kvm/driver/driver.rb', line 668

def host_gentoo?
  check_os_release?("/etc/gentoo-release")
end

#host_redhat?Boolean

Checks which Linux OS variants

host_redhat? host_ubuntu? host_debian? host_gentoo? host_arch?

Returns:

  • (Boolean)


644
645
646
647
648
649
650
651
# File 'lib/vagrant-kvm/driver/driver.rb', line 644

def host_redhat?
  # Check also Korora, CentOS, Fedora,
  #   Oracle Linux < 5.3 and
  #   Red Hat Enterprise Linux and Oracle Linux >= 5.3
  return true if check_os_release?("/etc/redhat-release",
    ["CentOS","Fedora","Korora","Enterprise Linux Enterprise Linux","Red Hat Enterprise Linux"])
  false
end

#host_suse?Boolean

Returns:

  • (Boolean)


653
654
655
# File 'lib/vagrant-kvm/driver/driver.rb', line 653

def host_suse?
  check_os_release?("/etc/SuSE-release")
end

#host_ubuntu?Boolean

Returns:

  • (Boolean)


657
658
659
660
661
662
# File 'lib/vagrant-kvm/driver/driver.rb', line 657

def host_ubuntu?
  if rel = check_lsb_release?
    return true if rel == "Ubuntu"
  end
  false
end

#hostname?String

Returns a hostname of the guest introduced from ruby-libvirt 0.5.0

Returns:

  • (String)


426
427
428
429
# File 'lib/vagrant-kvm/driver/driver.rb', line 426

def hostname?
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.hostname
end

#import(box_xml_filename, volume_name, args = {}) ⇒ Object

Imports the VM

Parameters:

  • definition (String)

    Path to the VM XML file.

  • volume_name (String)

    Name of the imported volume

  • attributes (Hash)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vagrant-kvm/driver/driver.rb', line 145

def import(box_xml_filename, volume_name, args={})
  @logger.info("Importing VM #{@name}")
  # create vm definition from xml
  definition = File.open(box_xml_filename) { |f| Util::VmDefinition.new(f.read) }
  volume_path = lookup_volume_path_by_name(volume_name)
  args = {
    :image_type => "qcow2",
    :qemu_bin => "/usr/bin/qemu",
    :disk => volume_path,
    :network => 'vagrant',
    :name => @name,
    :uuid => nil
  }.merge(args)
  args.merge!(:virtio_rng => nil) if @conn.version.to_i < 1003000  # virtio_rng supported in 1.3.0+
  definition.update(args)
  # create vm
  @logger.info("Creating new VM")
  xml_definition = definition.as_xml
  @logger.debug("Creating new VM with XML config:\n#{xml_definition}")
  domain = @conn.define_domain_xml(xml_definition)
  domain.uuid
end

#init_storage_pool(pool_name, pool_path = '/tmp', dir_mode = '0755') ⇒ Object

Initialize or Create a new storage pool



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/vagrant-kvm/driver/driver.rb', line 271

def init_storage_pool(pool_name, pool_path='/tmp', dir_mode='0755')
  begin
    # Try to retrieve the storage pool
    pool = @conn.lookup_storage_pool_by_name(pool_name)
    @logger.info("Activating storage pool #{pool_name}")
    pool.create unless pool.active?
    pool.refresh
    pool
  rescue Libvirt::RetrieveError
    create_storage_pool(pool_name, pool_path, dir_mode)
  end
end

#list_all_network_ipsObject



367
368
369
370
371
372
373
374
375
# File 'lib/vagrant-kvm/driver/driver.rb', line 367

def list_all_network_ips
  interfaces = read_network_interfaces
  ips = []
  interfaces.each do |interface|
    next if interface.type == :user
    ips << list_network_ips(interface.name)
  end
  ips
end

#list_default_network_ipsObject



377
378
379
# File 'lib/vagrant-kvm/driver/driver.rb', line 377

def list_default_network_ips
  list_network_ips('vagrant')
end

#list_network_ips(network_name) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/vagrant-kvm/driver/driver.rb', line 381

def list_network_ips(network_name)
  begin
    network = @conn.lookup_network_by_name(network_name)
    network_definition = Util::NetworkDefinition.new(network_name, network.xml_desc)
    ips = []
    if network_definition
      ips = network_definition.hosts.map {|host| host[:ip]}
    end
    ips
  rescue Libvirt::RetrieveError
    @logger.info("error when getting network xml")
    []
  end
end

#lookup_volume_path_by_name(volume_name) ⇒ Object



362
363
364
365
# File 'lib/vagrant-kvm/driver/driver.rb', line 362

def lookup_volume_path_by_name(volume_name)
  volume = @pool.lookup_volume_by_name(volume_name)
  volume.path
end

#network_state?(network_name) ⇒ Boolean

Returns:

  • (Boolean)


404
405
406
407
408
409
410
411
# File 'lib/vagrant-kvm/driver/driver.rb', line 404

def network_state?(network_name)
  begin
    network = @conn.lookup_network_by_name(network_name)
    network.active?
  rescue Libvirt::RetrieveError
    false
  end
end

#raw(*command, &block) ⇒ Object

Executes a command and returns the raw result object.



587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/vagrant-kvm/driver/driver.rb', line 587

def raw(*command, &block)
  int_callback = lambda do
    @interrupted = true
    @logger.info("Interrupted.")
  end

  # Append in the options for subprocess
  command << { :notify => [:stdout, :stderr] }

  Vagrant::Util::Busy.busy(int_callback) do
    Vagrant::Util::Subprocess.execute(@virsh_path, *command, &block)
  end
end

#read_mac_addressObject



454
455
456
457
458
# File 'lib/vagrant-kvm/driver/driver.rb', line 454

def read_mac_address
  domain = @conn.lookup_domain_by_uuid(@uuid)
  definition = Util::VmDefinition.new(domain.xml_desc)
  definition.attributes[:mac]
end

#read_machine_ipObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/vagrant-kvm/driver/driver.rb', line 237

def read_machine_ip
  if @uuid && vm_exists?(@uuid)
    begin
      domain = @conn.lookup_domain_by_uuid(@uuid)
      definition = Util::VmDefinition.new(domain.xml_desc)
      mac_address = definition.get(:mac)
      network_name = definition.get(:network)
      network = @conn.lookup_network_by_name(network_name)
      network_definition = Util::NetworkDefinition.new(network_name,
                                             network.xml_desc)
      network_definition.get(:hosts).each do |host|
        return host[:ip] if mac_address == host[:mac]
      end
    rescue Libvirt::RetrieveError
      @logger.info("cannot get definition og #{@uuid}")
    end
  else
    @logger.debug("missing uuid? #{@uuid}")
  end
  get_default_ip
end

#read_network_interfacesHash

Returns a list of network interfaces of the VM.

Returns:

  • (Hash)


399
400
401
402
# File 'lib/vagrant-kvm/driver/driver.rb', line 399

def read_network_interfaces
  domain = @conn.lookup_domain_by_uuid(@uuid)
  Util::VmDefinition.list_interfaces(domain.xml_desc)
end

#read_stateObject



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/vagrant-kvm/driver/driver.rb', line 438

def read_state
  domain = @conn.lookup_domain_by_uuid(@uuid)
  state, reason = domain.state
  # check if domain has been saved
  case VM_STATE[state]
  when :shutoff
    if domain.has_managed_save?
      return :saved
    end
    return :poweroff
  when :shutdown
    return :poweroff
  end
  VM_STATE[state]
end

#resetObject

reset the guest introduced from ruby-libvirt 0.5.0



433
434
435
436
# File 'lib/vagrant-kvm/driver/driver.rb', line 433

def reset
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.reset
end

#reset_volume_permission(userid, groupid) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/vagrant-kvm/driver/driver.rb', line 107

def reset_volume_permission(userid, groupid)
  @logger.info("Revert image owner to #{userid}:#{groupid}")
  domain = @conn.lookup_domain_by_uuid(@uuid)
  definition = Util::VmDefinition.new(domain.xml_desc)
  volume_path = definition.attributes[:disk]
  run_root_command("chown #{userid}:#{groupid} " + volume_path)
  run_root_command("chmod 660 " + volume_path)
end

#resumeObject

Resumes the previously paused virtual machine.



461
462
463
464
465
466
# File 'lib/vagrant-kvm/driver/driver.rb', line 461

def resume
  @logger.debug("Resuming paused VM...")
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.resume
  true
end

#saveObject

Suspend the virtual machine and saves its states.



530
531
532
533
# File 'lib/vagrant-kvm/driver/driver.rb', line 530

def save
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.managed_save
end

#set_diskbus(disk_bus) ⇒ Object



486
487
488
# File 'lib/vagrant-kvm/driver/driver.rb', line 486

def set_diskbus(disk_bus)
  update_domain_xml(:disk_bus => disk_bus)
end

#set_gui(vnc_port, vnc_autoport, vnc_password) ⇒ Object



477
478
479
480
481
482
483
484
# File 'lib/vagrant-kvm/driver/driver.rb', line 477

def set_gui(vnc_port, vnc_autoport, vnc_password)
  @logger.debug("Enabling GUI")
  update_domain_xml(
    :gui => true,
    :vnc_port => vnc_port,
    :vnc_autoport => vnc_autoport,
    :vnc_password => vnc_password)
end

#set_mac_address(mac) ⇒ Object



472
473
474
475
# File 'lib/vagrant-kvm/driver/driver.rb', line 472

def set_mac_address(mac)
  update_domain_xml(:mac => mac)
  @logger.debug("set mac: #{mac}")
end

#set_name(name) ⇒ Object



468
469
470
# File 'lib/vagrant-kvm/driver/driver.rb', line 468

def set_name(name)
  @name = name
end

#share_folders(folders) ⇒ Object



490
491
492
# File 'lib/vagrant-kvm/driver/driver.rb', line 490

def share_folders(folders)
  update_domain_xml(:p9 => folders)
end

#startObject

Starts the virtual machine.



523
524
525
526
527
# File 'lib/vagrant-kvm/driver/driver.rb', line 523

def start
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.create
  true
end

#start_network(network_name) ⇒ Object



413
414
415
416
417
418
419
420
# File 'lib/vagrant-kvm/driver/driver.rb', line 413

def start_network(network_name)
  begin
    network = @conn.lookup_network_by_name(network_name)
    network.create unless network.active?
  rescue Libvirt::RetrieveError
    false
  end
end

#storage_pool_pathObject



357
358
359
360
# File 'lib/vagrant-kvm/driver/driver.rb', line 357

def storage_pool_path
  doc = REXML::Document.new @pool.xml_desc
  doc.elements["/pool/target/path"].text
end

#suspendObject

Suspend the virtual machine temporaly



557
558
559
560
# File 'lib/vagrant-kvm/driver/driver.rb', line 557

def suspend
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.suspend
end

#suspend_for_duration(target, duration) ⇒ Object

Suspend for duration introduced from ruby-libvirt 0.5.0



537
538
539
540
# File 'lib/vagrant-kvm/driver/driver.rb', line 537

def suspend_for_duration(target, duration)
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.pmsuspend_for_duration(target, duration)
end

#update_domain_xml(options) ⇒ Object



498
499
500
501
502
503
504
505
506
507
508
# File 'lib/vagrant-kvm/driver/driver.rb', line 498

def update_domain_xml(options)
  domain = @conn.lookup_domain_by_uuid(@uuid)
  # Use DOMAIN_XML_SECURE to dump ALL options (including VNC password)
  original_xml = domain.xml_desc(Libvirt::Domain::DOMAIN_XML_SECURE)
  definition = Util::VmDefinition.new(original_xml)
  definition.update(options)
  domain.undefine
  xml = definition.as_xml
  @logger.debug("Updating domain xml\nFrom: #{original_xml}\nTo: #{xml}")
  @conn.define_domain_xml(xml)
end

#verify!Object

Verifies that the driver is ready and the connection is open

This will raise a VagrantError if things are not ready.



618
619
620
621
622
# File 'lib/vagrant-kvm/driver/driver.rb', line 618

def verify!
  if @conn.closed?
    raise Vagrant::Errors::KvmNoConnection
  end
end

#vm_exists?(uuid) ⇒ Boolean

Checks if a VM with the given UUID exists.

Returns:

  • (Boolean)


627
628
629
630
631
632
633
634
# File 'lib/vagrant-kvm/driver/driver.rb', line 627

def vm_exists?(uuid)
  begin
    @logger.info("Check if VM #{uuid} exists")
    @conn.lookup_domain_by_uuid(uuid)
  rescue Libvirt::RetrieveError
    false
  end
end

#wakeupObject

Wakeup the virtual machine introduced from ruby-libvirt 0.5.0



544
545
546
547
# File 'lib/vagrant-kvm/driver/driver.rb', line 544

def wakeup
  domain = @conn.lookup_domain_by_uuid(@uuid)
  domain.pmwakeup
end