Class: VagrantPlugins::SyncedFolder9p::SyncedFolder

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util, ProviderLibvirt::Util::ErbTemplate
Defined in:
lib/vagrant-libvirt/cap/synced_folder.rb

Instance Method Summary collapse

Methods included from ProviderLibvirt::Util::ErbTemplate

#to_xml

Constructor Details

#initialize(*args) ⇒ SyncedFolder

Returns a new instance of SyncedFolder.


17
18
19
20
21
# File 'lib/vagrant-libvirt/cap/synced_folder.rb', line 17

def initialize(*args)
  super

  @logger = Log4r::Logger.new("vagrant_libvirt::synced_folders::9p")
end

Instance Method Details

#cleanup(machine, _opts) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vagrant-libvirt/cap/synced_folder.rb', line 78

def cleanup(machine, _opts)

  raise Vagrant::Errors::Error("No libvirt connection") if ProviderLibvirt.libvirt_connection.nil?

  @conn = ProviderLibvirt.libvirt_connection.client
 
  begin
    if machine.id && machine.id != ""
      dom = @conn.lookup_domain_by_uuid(machine.id)
      Nokogiri::XML(dom.xml_desc).xpath('/domain/devices/filesystem').each do |xml|
        dom.detach_device(xml.to_s)

        machine.ui.info "Cleaned up shared folders"
      end
    end
  rescue => e
    machine.ui.error("could not detach device because: #{e}")
    raise VagrantPlugins::ProviderLibvirt::Errors::DetachDeviceError,:error_message => e.message
  end

end

#enable(machine, folders, _opts) ⇒ Object

TODO once up, mount folders


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vagrant-libvirt/cap/synced_folder.rb', line 63

def enable(machine, folders, _opts)
  # Go through each folder and mount
  machine.ui.info("mounting p9 share in guest")
  # Only mount folders that have a guest path specified.
  mount_folders = {}
  folders.each do |id, opts|
    mount_folders[id] = opts.dup if opts[:guestpath]
    # merge common options if not given
    mount_folders[id].merge!(:version => '9p2000.L') { |_k, ov, _nv| ov }
  end
  # Mount the actual folder
  machine.guest.capability(
      :mount_p9_shared_folder, mount_folders)
end

#prepare(machine, folders, opts) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vagrant-libvirt/cap/synced_folder.rb', line 37

def prepare(machine, folders, opts)
  
  raise Vagrant::Errors::Error("No libvirt connection") if ProviderLibvirt.libvirt_connection.nil?

  @conn = ProviderLibvirt.libvirt_connection.client

  begin
    # loop through folders
    folders.each do |id, folder_opts|
      folder_opts.merge!({ :accessmode => "passthrough",
                           :readonly => nil }) { |_k, ov, _nv| ov }
      machine.ui.info "================\nMachine id: #{machine.id}\nShould be mounting folders\n #{id}, opts: #{folder_opts}"

      xml =  to_xml('filesystem', folder_opts )
      # puts "<<<<< XML:\n #{xml}\n >>>>>"
      @conn.lookup_domain_by_uuid(machine.id).attach_device(xml, 0)

    end 
  rescue => e
    machine.ui.error("could not attach device because: #{e}")
    raise VagrantPlugins::ProviderLibvirt::Errors::AttachDeviceError,:error_message => e.message
  end
end

#usable?(machine, raise_error = false) ⇒ Boolean

Returns:

  • (Boolean)

23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant-libvirt/cap/synced_folder.rb', line 23

def usable?(machine, raise_error=false)
  # bail now if not using libvirt since checking version would throw error
  return false unless machine.provider_name == :libvirt

  # <filesystem/> support in device attach/detach introduced in 1.2.2
  # version number format is major * 1,000,000 + minor * 1,000 + release
  libvirt_version = ProviderLibvirt.libvirt_connection.client.libversion
  if libvirt_version >= 1002002
    return true
  else
    return false
  end
end