Class: Vagrant::Actions::VM::SharedFolders

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/actions/vm/shared_folders.rb

Instance Attribute Summary

Attributes inherited from Base

#runner

Instance Method Summary collapse

Methods inherited from Base

#cleanup, #execute!, #follows, #initialize, #precedes, #prepare, #rescue

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

This class inherits a constructor from Vagrant::Actions::Base

Instance Method Details

#after_bootObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant/actions/vm/shared_folders.rb', line 17

def after_boot
  logger.info "Mounting shared folders..."

  @runner.env.ssh.execute do |ssh|
    shared_folders.each do |name, hostpath, guestpath|
      logger.info "-- #{name}: #{guestpath}"
      ssh.exec!("sudo mkdir -p #{guestpath}")
      mount_folder(ssh, name, guestpath)
      ssh.exec!("sudo chown #{Vagrant.config.ssh.username} #{guestpath}")
    end
  end
end

#before_bootObject



12
13
14
15
# File 'lib/vagrant/actions/vm/shared_folders.rb', line 12

def before_boot
  clear_shared_folders
  
end

#clear_shared_foldersObject



30
31
32
33
34
35
36
37
38
# File 'lib/vagrant/actions/vm/shared_folders.rb', line 30

def clear_shared_folders
  logger.info "Clearing previously set shared folders..."

  @runner.vm.shared_folders.each do |shared_folder|
    shared_folder.destroy
  end

  @runner.reload!
end

#create_metadataObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant/actions/vm/shared_folders.rb', line 40

def 
  logger.info "Creating shared folders metadata..."

  shared_folders.each do |name, hostpath, guestpath|
    folder = VirtualBox::SharedFolder.new
    folder.name = name
    folder.hostpath = hostpath
    @runner.vm.shared_folders << folder
  end

  @runner.vm.save(true)
end

#mount_folder(ssh, name, guestpath, sleeptime = 5) ⇒ Object



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
# File 'lib/vagrant/actions/vm/shared_folders.rb', line 53

def mount_folder(ssh, name, guestpath, sleeptime=5)
  # Note: This method seems pretty OS-specific and could also use
  # some configuration options. For now its duct tape and "works"
  # but should be looked at in the future.

  # Determine the permission string to attach to the mount command
  perms = []
  perms << "uid=#{@runner.env.config.vm.shared_folder_uid}"
  perms << "gid=#{@runner.env.config.vm.shared_folder_gid}"
  perms = " -o #{perms.join(",")}" if !perms.empty?

  attempts = 0
  while true
    result = ssh.exec!("sudo mount -t vboxsf#{perms} #{name} #{guestpath}") do |ch, type, data|
      # net/ssh returns the value in ch[:result] (based on looking at source)
      ch[:result] = !!(type == :stderr && data =~ /No such device/i)
    end

    break unless result

    attempts += 1
    raise ActionException.new(:vm_mount_fail) if attempts >= 10
    sleep sleeptime
  end
end

#shared_foldersObject



5
6
7
8
9
10
# File 'lib/vagrant/actions/vm/shared_folders.rb', line 5

def shared_folders
  @runner.env.config.vm.shared_folders.inject([]) do |acc, data|
    name, value = data
    acc << [name, File.expand_path(value[:hostpath]), value[:guestpath]]
  end
end