Class: VagrantPlugins::SyncedFolder9P::SyncedFolder

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

Instance Method Summary collapse

Methods included from ProviderLibvirt::Util::ErbTemplate

#to_xml

Constructor Details

#initialize(*args) ⇒ SyncedFolder

Returns a new instance of SyncedFolder.



20
21
22
23
# File 'lib/vagrant-libvirt/cap/synced_folder_9p.rb', line 20

def initialize(*args)
  super
  @logger = Log4r::Logger.new('vagrant_libvirt::synced_folders::9p')
end

Instance Method Details

#cleanup(machine, _opts) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vagrant-libvirt/cap/synced_folder_9p.rb', line 94

def cleanup(machine, _opts)
  if machine.provider.driver.connection.nil?
    raise Vagrant::Errors::Error('No Libvirt connection')
  end
  @conn = machine.provider.driver.connection.client
  machine.ui.info I18n.t("vagrant_libvirt.cap.9p.cleanup")
  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)
      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

once up, mount folders



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vagrant-libvirt/cap/synced_folder_9p.rb', line 77

def enable(machine, folders, _opts)
  # Go through each folder and mount
  machine.ui.info I18n.t("vagrant_libvirt.cap.9p.mounting")
  # Only mount folders that have a guest path specified.
  mount_folders = {}
  folders.each do |id, opts|
    next unless opts[:mount] && opts[:guestpath] && !opts[:guestpath].empty?
    mount_folders[id] = opts.dup
    # 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_9p_shared_folder, mount_folders
  )
end

#prepare(machine, folders, _opts) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/vagrant-libvirt/cap/synced_folder_9p.rb', line 35

def prepare(machine, folders, _opts)
  raise Vagrant::Errors::Error('No Libvirt connection') if machine.provider.driver.connection.nil?
  @conn = machine.provider.driver.connection.client

  machine.ui.info I18n.t("vagrant_libvirt.cap.9p.preparing")

  begin
    # loop through folders
    folders.each do |id, folder_opts|
      folder_opts.merge!(target: id,
                         accessmode: 'passthrough',
                         mount: true,
                         readonly: nil) { |_k, ov, _nv| ov }

      mount_tag = Digest::MD5.new.update(folder_opts[:hostpath]).to_s[0, 31]
      folder_opts[:mount_tag] = mount_tag

      xml = Nokogiri::XML::Builder.new do |xml|
        xml.filesystem(type: 'mount', accessmode: folder_opts[:accessmode]) do
          xml.driver(type: 'path', wrpolicy: 'immediate')
          xml.source(dir: folder_opts[:hostpath])
          xml.target(dir: mount_tag)
          xml.readonly unless folder_opts[:readonly].nil?
        end
      end.to_xml(
        save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION |
                   Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS |
                   Nokogiri::XML::Node::SaveOptions::FORMAT
      )
      @logger.debug {
        "Attaching Synced Folder device with XML:\n#{xml}"
      }
      @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)


25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-libvirt/cap/synced_folder_9p.rb', line 25

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 = machine.provider.driver.connection.client.libversion
  libvirt_version >= 1_002_002
end