Setting up correct permissions for VirtualBox shared folders
With the launch of Mountain Lion and after suffering a hard drive failure, I decided to investigate other options for my local development environment than using MAMP. MAMP has served me well over the years, but considering the number of visitors to my MAMP + Lion tutorials, it’s obviously not as easy to use as a LAMP server.
Virtual machines are great. I know many people that use commercial products like Parallels and VMWare Fusion to run Windows instances alongside their OS X environments. I’m not here today to compare the various products, just know that for me, the open source VirtualBox more than adequately fills my need.
After discussing my VM desires with some other developers I work with, I discovered that Shared Folders were the killer feature I needed.
Shared folders allow me to take my ~/Sites directory and map it to the /var/www directory on my LAMP-based VM. For the most part this has worked well for me. Obviously, though if all was well, I wouldn’t be writing this post.
I’ve got some code that generates cached templates. With some recent changes I made to an underlying templating engine I’m working on, the engine always creates cached files, but if you are in a development environment (or debug state), it will just overwrite the files with every request.
This led to a problem where the permissions of my mounted folder prevented me from writing these critical files to the shared folder. I got to a point where I could create the file the first time, but the template could not overwrite the file on subsequent saves.
Doing some searching led to a post on the ubuntu stack exchange, which led me to the correct solution. It’s different enough (at least for my setup) that I wanted to chronicle the changes I made.
Since I am mounting a folder that needs to be written to by my Apache user, I needed to set up the mounted point so that it mounts being owned by www-data. To find this information, on the VM command line, I typed:
$ id www-data
This returns the gid and uid of 33 for my VM. Using this information, I was able to update my rc.local file so that the line I use to automatically mount my shared folder reads:
sudo mount -t vboxsf -o umask=002,gid=33,uid=33 www /var/www
After doing a reboot, the shared folder now mounts using www-data as the owner. Now I can write and overwrite to my cached folder without problems!
I may write up an overall how-to on setting up a headless LAMP VM with VirtualBox, but that’ll be for another time.