Class: VagrantPlugins::Utm::SyncedFolder

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_utm/synced_folder.rb

Overview

Default Synced folder implementation for UTM

Instance Method Summary collapse

Instance Method Details

#cleanup(machine, _opts) ⇒ Object



91
92
93
# File 'lib/vagrant_utm/synced_folder.rb', line 91

def cleanup(machine, _opts)
  driver(machine).clear_shared_folders if machine.id && machine.id != ""
end

#disable(machine, folders, _opts) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vagrant_utm/synced_folder.rb', line 76

def disable(machine, folders, _opts)
  if machine.guest.capability?(:unmount_virtualbox_shared_folder)
    folders.each_value do |data|
      machine.guest.capability(
        :unmount_virtualbox_shared_folder,
        data[:guestpath], data
      )
    end
  end

  # Remove the shared folders from the VM metadata
  names = folders.map { |id, _data| os_friendly_id(id) }
  driver(machine).unshare_folders(names)
end

#enable(machine, folders, _opts) ⇒ Object

This is called after VM Boot to mount the synced folders. Mount the shared folders inside the VM.



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant_utm/synced_folder.rb', line 25

def enable(machine, folders, _opts) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/PerceivedComplexity
  share_folders(machine, folders)

  # sort guestpaths first, so we don't step on ourselves
  folders = folders.sort_by do |_id, data|
    if data[:guestpath]
      data[:guestpath].length
    else
      # A long enough path to just do this at the end.
      10_000
    end
  end

  # Go through each folder and mount
  machine.ui.output(I18n.t("vagrant.actions.vm.share_folders.mounting"))
  # refresh fstab
  fstab_folders = [] # rubocop:disable Lint/UselessAssignment
  folders.each do |id, data|
    if data[:guestpath]
      # Guest path specified, so mount the folder to specified point
      machine.ui.detail(I18n.t("vagrant.actions.vm.share_folders.mounting_entry",
                               guestpath: data[:guestpath],
                               hostpath: data[:hostpath]))

      # Dup the data so we can pass it to the guest API
      data = data.dup

      # Calculate the owner and group
      ssh_info = machine.ssh_info
      data[:owner] ||= ssh_info[:username]
      data[:group] ||= ssh_info[:username]

      # Unmount the folder before we mount it
      machine.guest.capability(
        :unmount_virtualbox_shared_folder,
        data[:guestpath], data
      )

      # Mount the actual folder
      machine.guest.capability(
        :mount_virtualbox_shared_folder,
        os_friendly_id(id), data[:guestpath], data
      )
    else
      # If no guest path is specified, then automounting is disabled
      machine.ui.detail(I18n.t("vagrant.actions.vm.share_folders.nomount_entry",
                               hostpath: data[:hostpath]))
    end
  end
end

#prepare(machine, folders, _opts) ⇒ Object

This is called before VM Boot to prepare the synced folders. Add required configs to the VM.



19
20
21
# File 'lib/vagrant_utm/synced_folder.rb', line 19

def prepare(machine, folders, _opts)
  share_folders(machine, folders)
end

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

rubocop:disable Style/OptionalBooleanParameter

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
# File 'lib/vagrant_utm/synced_folder.rb', line 7

def usable?(machine, _raise_errors = false) # rubocop:disable Style/OptionalBooleanParameter
  # These synced folders only work if the provider is UTM
  return false if machine.provider_name != :utm

  # This only happens with `vagrant package --base`. Sigh.
  return true unless machine.provider_config

  machine.provider_config.functional_9pfs
end