NFS based file sharing for Varying Vagrant under Windows

This entry is part 4 of 4 in the series Setup Varying Vagrant Vagrant on Windows

So now that we have vagrant running it is time to make things faster. To do that we are going to:

  1. Increase CPU and Memory allocated to our varying vagrant.
  2. Use NFS based synched directory between our vagrant box and Windows host.

Windows doesn’t support NFS based file sharing by default, so we need to install a separate program for that. Once done, it will help you with:

  1. Greater and Faster file synch across your Windows host and Linux Guest.
  2. Flawless npm package install with symlinking.

Install WinNFSd and Vagrant Plugin

WinNFSd by Marc Harding

First of all kudos to this awesome guy for creating this program, without which none of these would have been possible. You can go to the GitHub repository and download the latest release executable. But we will need a bit of modification from our end to make everything work great. So I have created a separate repo under our company GitLab from where you will find all the stuff needed.

Download files from the links above, there you will find a directory winnfsd. Put it under C:\.

Now edit the file exports.txt and replace the entries with the actual path from your file system. We will need the path for database, config, log and www directory from your vagrant folder.

Right Click on the WinNFSd.exe file and go to properties > compatibility and check Run this program as an administrator.

Under desktop, create a new shortcut and name it WinNFSd Start. Put the following command in the shortcut.

C:\winnfsd\nfsstart.bat start C:\winnfsd\exports.txt

Also create another shortcut, name it WinNFSd Stop. Put the following command.

C:\winnfsd\nfsstart.bat stop C:\winnfsd\exports.txt

As the name suggests, these two shortcuts will start and stop the WinNFSd service according to your need.

Vagrant::WinNFSd Plugin

Go to your vagrant directory.

cd /d/vvv

Run the command to install WinNFSd Vagrant plugin.

vagrant plugin install vagrant-winnfsd

Now we need to change the synced_folder in our vagrant.

Change Synced Folder through Customfile

Since Varying Vagrants Vagrant 2.0, we can add custom code to the Vagrantfile without touching it. Create a file under the same directory as the Vagrantfile and name it Customfile. Put the following content inside.

# -*- mode: ruby -*-
# vi: set ft=ruby ts=2 sw=2 et:

# Enable Public Network
config.vm.network :public_network, type: "dhcp", auto_config: true

# Use NFS for synched_folder
config.vm.provider "virtualbox" do |v, override|
# /srv/database
override.vm.synced_folder "database/", "/srv/database", owner: nil, group: nil, type: "nfs", mount_options: ['rw', 'vers=3', 'tcp', 'fsc' ,'actimeo=1']
# /srv/config
override.vm.synced_folder "config/", "/srv/config", owner: nil, group: nil, type: "nfs", mount_options: ['rw', 'vers=3', 'tcp', 'fsc' ,'actimeo=1']
# /srv/log
override.vm.synced_folder "log/", "/srv/log", owner: nil, group: nil, type: "nfs", mount_options: ['rw', 'vers=3', 'tcp', 'fsc' ,'actimeo=1']
# /srv/www
override.vm.synced_folder "www/", "/srv/www/", owner: nil, group: nil, type: "nfs", mount_options: [ 'rw', 'vers=3', 'tcp', 'fsc' ,'actimeo=1' ]
# Enable symlinks
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/srv/www", "1"]
end

This will essentially do three things:

  • Enable a public network so that Vagrant can be reached from outside your PC.
  • Override the synced folder to use NFS instead of VirtualBox share. It will also optimize some settings for you.
  • Enable symlink which we will need for npm moduels.

Increase CPU and Memory

Previously we had installed Vagrant::Faster plugin to speed up by allocating more memory and CPU. In most cases this would work fine, but sometimes it won’t. Luckily Varying Vagrants Vagrant now gives us control over how much CPU/Memory we can allocate. To get started do the following.

First uninstall the Vagrant::Faster plugin.

vagrant plugin uninstall vagrant-faster

Check if you have vvv-custom.yml file. If not, then start by copying over the vvv-config.yml.

cd /d/vvv
cp vvv-config.yml vvv-custom.yml

Now edit the vvv-custom.yml file and put the following content at the end of the file.

# Vagrant Memory and CPU
vm_config:
memory: 6144
cores: 4

Here we give 6GB of RAM to our box and 4 CPUs. I do that because I have 16GB of RAM and core i7 4790K, but you should do according to your specification. Generally half of CPU and RAM should be fine.

Finalize Changes

Now before we start vagrant, we need to click on the “WinNFSd Start” shortcut we created on the desktop. This starts the WinNFSd server. Vagrant::WinNFSd does come with its own server, but for some reason, it stops working after a while. This separate approach is better and since Vagrant::WinNFSd is written in a way that it listens to a single server only, it works just fine.

Now do a vagrant provision.

vagrant up --provision

And that’s it. Now you have lightning fast file sharing through NFS. If you are using git, then you might want to fine tune it by enabling the preloadIndex.

vagrant ssh
git config core.preloadIndex true

Now git will work fine within your box.