NFS pitfalls with Vagrant

2015-10-15 | #solution, #webdev

Using NFS for mounting the shared folders in a Vagrant VM definitely has speed advantages, but it comes with some pitfalls, which I experienced recently.


Problem 1: Recognizing file system changes in mounted folder inside the VM

Say you edit code in your editor of choice on your host system, changing files in a mounted folder, but having a watcher inside the VM, waiting for changes to happen. With the default settings it can take ages until the VM reacts to changes, making this process nerve-wrecking. To change this, you'll have to decrease the time a files stat's are allowed to stay not updated. You can do this directly in your vagrant file.

config.vm.synced_folder "../", "/var/www/htdocs", nfs: true, :mount_options => ['actimeo=2']

This will set all file stats being older than 200ms to "outdated", resulting in much faster refresh times. I am yet to see any downside, performance- or otherwise to this.


Problem 2: Certain actions inside the VM fail with permission error, although done by root

This most likely is a NFS "root squashing" error, caused by a security mechanism of NFS trying to prevent a clients root to do root things on the host. Which normally is fine, but can be terribly annoying in a vagrant context. Again, you can fix this directly in your vagrant file, by disabling root squash for linux and BSD/OSX with their respective settings.

config.vm.synced_folder "../", "/var/www/htdocs", nfs: true, :linux__nfs_options => ["no_root_squash"], :map_uid => 0, :map_gid => 0

This should fix those pains for Linux and OSX hosts alike.


Of course you can also combine the options of 1 & 2.