Vagrant mount guest samba shares

Mar 1, 2014

Vagrant offers an unprecedented level of ease when it comes to recreating the same development environment across different machines. I currently am stuck with windows as my main OS, both at home and at work. Most of the work I do at home revolves around Node.js (and currently a bit of ruby too, thanks to Jekyll, but that might be changing soon) whereas the work that I do at the office mostly involves C++ and cross-compilation to QNX. At home I use a precise64 vagrant box that currently hosts jekyll, node, npm and a few other tools that I use everyday. At work, the vagrant box has gcc, gdb, g++, and mounts my user account from the buildserver locally via sshfs. At both places I use Emacs as my main editor, and Tramp support on Windows is just notoriously pathetic. I tried a lot of different methods like ssh, scpx, pscp, plink, nothing worked. I then tried a couple of vagrant plugins like vagrant-winnfsd and vagrant-sshfs, but neither was particular convenient to use. The fundamental problem that I faced with NFS is that it would screw up all my file permissions and wasn’t particularly happy with long paths. SSHFS didn’t work because Windows doesn’t have support for userspace filesystems, so FUSE was out, and there is no decent sshfs driver for windows.

There is light at the end of the tunnel. I followed three different guides and have finally set up a private Samba share between my workstation and the vagrant instance. The private part is important because I don’t want the network admins at work to have access to my private stuff, e.g. this blog. Anyway, here’s how it’s done

Vagrantfile

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.network :private_network, ip: "10.0.0.10"
end

This creates a private network that is only accessible from the host and the guest. The 10.0.0.10 subnet is configurable, you can set it to any internal IP subnet you desire.

Samba configuration

Install samba with sudo apt-get install samba on debian boxes or with your repo-specific method of installing samba. Next up, find and edit the following lines in /etc/samba/smb.conf and set the values as shown below

wins support = yes
dns proxy = no
interfaces = 127.0.0.0/8 10.0.0.0/24
bind interfaces only = yes

To create the share itself, add the following lines to /etc/samba/smb.conf

[myshare]
  path = /home/vagrant
  writable = Yes
  guest ok = no
  force user = vagrant

The myshare can be changed to whatever you want, that will be the name of the share. You will have to set a samba password for the vagrant user, this can be done with sudo smbpasswd vagrant. Finally, restart the samba daemon with sudo service smbd restart.

Mounting in windows

The share will be accessible from \\Precise64\myshare. I still haven’t figured out how to change the server name (Precise64) to something else.

Bonus - Vagrantfile

Most of this can be automated by using a provisioning script with your Vagrantfile. After you’ve hooked up your provisioning script with your Vagrantfile, running vagrant up will run all the commands in the provisioning script on your vagrant instance with root privileges. Here’s the relevant portion from my simple bash script:

# Install packages
apt-get update
apt-get install -y samba

# Initialize samba
cp /vagrant/smb.conf /etc/samba/smb.conf
service smbd restart