Skip to content

Instantly share code, notes, and snippets.

@fnordfish
Last active November 19, 2015 10:48
Show Gist options
  • Select an option

  • Save fnordfish/2444d8bc55febfabb0fa to your computer and use it in GitHub Desktop.

Select an option

Save fnordfish/2444d8bc55febfabb0fa to your computer and use it in GitHub Desktop.
automatically mount nfs share from your Vagrant guest onto your OS X host
Vagrant.configure(2) do |global_config|
global_config.vm.define :my_project do |config|
#
# ... All your stuff ...
#
# Auto-Mount the boxes "/home/vagrant" onto your local machine
if Vagrant.has_plugin? 'vagrant-triggers'
def box_ip(config)
if (net_cgf = config.vm.networks.find {|k, _| k == :private_network})
net_cgf[1][:ip]
end
end
config.trigger.before [:suspend, :halt, :force_halt, :destroy, :reload] do
if ip = box_ip(config)
if Vagrant::Util::Platform.darwin?
run "umount -h #{ip}"
end
end
end
config.trigger.after [:up, :resume, :reload] do
if ip = box_ip(config)
remote_mnt = "#{ip}:/home/vagrant"
local_mnt = File.join(@machine.env.root_path, 'mnt')
Dir.mkdir(local_mnt) unless Dir.exist?(local_mnt)
if Vagrant::Util::Platform.darwin?
current_mounts = run("mount")
if m = current_mounts.lines.find {|l| l.include? remote_mnt}
success "Already mounted #{m.strip}"
else
info "Mounting..."
run("mount_nfs -v -r 8192 -w 8192 -bsT -o nonegnamecache #{remote_mnt} #{local_mnt}")
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment