Macos – OSX: Locking in vagrant with Ubuntu guest using NFS doesn’t work

macosnfsUbuntuvagrantvirtualbox

On OSX 10.10.3 using VirtualBox 4.3.26 and vagrant 1.7.2 when using NFS for the shared folder in a Ubuntu 14.04 LTS guest, PHP application ins the guest trying to use LOCK_EX on the shared folder simply lock forever (or: too long to be usable).

This setup used to work for months. Around two days ago after a host boot up the Vagrant machine came unusable and since then a new build of that VM exhibits this problem (maybe the initial problem was already related to this).

No new software was installed during the last few days. The OSX upgrade was performed around last week but everything on that machine continued to work.

After a long search we found the culprit being the NFS shared folder. We were using this line a long time now:

config.vm.synced_folder ".", "/vagrant", type: nfs

A strace inside the guest looks like this:

[pid 26767] open("/vagrant/some/file", O_RDWR|O_CREAT, 0666) = 5
[pid 26767] fstat(5, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
[pid 26767] lseek(5, 0, SEEK_CUR)      = 0
[pid 26767] chmod("/vagrant/some/file", 0777) = 0
[pid 26767] flock(5, LOCK_EX

it hangs at this point. I could even reproduce it myself with:

<?php
$f = fopen('lock.test', 'w');
echo "Trying to get lock on file ...\n";
var_dump(flock($f, LOCK_EX));
echo "Got lock, unlocking\n";
var_dump(flock($f, LOCK_UN));
echo "Exiting\n";

As unfortunately expected, it hung right on the first flock line.

I checked the /etc/expoerts system and made sure it was a clean file with no broken/old leftovers once the VM was shut down.

The very same setup on another 10.10.3 with identical other software works without problems (albeit a bit different hardware). I did build the VM on this other hardware multiple times a day and everything works as expected.

Obviously due the performance loss it's absolutely desired to get NFS working properly again (not using the shared folder at all is currently not an option).

Best Answer

I had the exact same problem. I read somewhere that changing to the following options resolved their issues:

config.vm.synced_folder ".", "/vagrant", mount_options: ["rw", "tcp", "nolock", "noacl", "async"], type: "nfs", nfs_udp: false

For me all I had to do was narrow this down to adding the following option:

mount_options: ["nolock"]

So your line should look something like:

config.vm.synced_folder ".", "/vagrant", mount_options: ["nolock"], type: "nfs"
Related Question