17

I have been banging my head against a wall for a week and a half trying to work out how to properly share files between a host and guest using Vagrant and VirtualBox.

What I need to achieve is an auto-provisioning box that downloads our codebase from github. The codebase permissions need to vary from file to file (PHP files, shell scripts, tmp folders, log folders, etc). The codebase files also need to be accessible from the host box for editing.

So far I have tried normal virtualbox sharing, NFS sharing, NFS sharing with bindFS. None of these seem to allow changing the individual file permissions.

This seems to be an absolute showstopper for Vagrant. I honestly do not understand how Vagrant is useful for the purpose of sharing development environments.

Does anyone know how to properly set this up? Is it even possible?

For reference:

  • host OS : Ubuntu 12.04
  • guest OS : Debian 6 (squeeze)
  • vagrant : 1.2.2
  • VirtualBox : 4.2.12
Andrew
  • 1,226
  • 2
  • 13
  • 20

6 Answers6

15

There are several partial answers here, but I'd like to make it much easier for people finding this question, so that they do not have to experiment with various ways of combining those partial answers into one.

This is what I use, and it is working well for me. Since it's difficult or impossible to use italics inside a <code> block, I've put the bits you'll want to change inside <angled brackets>.

config.vm.synced_folder ".", "/var/www/vhosts/<project_name>", 
    id: "project",
    owner: <username>,
    group: <group_name>,
    mount_options: ["dmode=775,fmode=664"]

Or possibly (depending on the version of CIFS/SMB you're using):

    mount_options: ["dir_mode=775,file_mode=664"]

Without the owner and group, Vagrant will use the value of config.ssh.username (when the box was booted) as the owner and group. By using a configuration like the one above, you can control what owner and group are assigned to the shared folder, since logging into the VM and trying to use chgrp and chown does not work.

Without the mount_options, you'll have no way to control what rights are given to the owner and group (and others). Even though this is a VM, and you could argue that it's okay to use 777 rights for these, I don't recommend it since it's a bad habit, and if you're not careful you're liable to carry it over into production, weakening your production security. But obviously you can change the 775 and 664 that I used to whatever you want. If you don't know what those mean, Google "Linux file permissions".

You may (out of habit) insert a space after the comma that separates the mount_options array. Don't. It won't work with a comma.

The id can be anything you want.

iconoclast
  • 21,213
  • 15
  • 102
  • 138
  • 3
    For future reference, what version of vagrant are you referencing here? One of the problems I came across was that different versions of Vagrant support different options. – Andrew Feb 25 '14 at 22:28
  • Vagrant changed the config syntax. Here is the old docs for version 1 on the syntax: http://docs-v1.vagrantup.com/v1/docs/config/vm/share_folder.html – Chloe Mar 18 '14 at 18:32
  • 1
    On newer version of CIFS, il should be `mount_options: ["dir_mode=0775,file_mode=0664"]`. (note the dir_mode and the octal form of the value) – magnetik Dec 09 '14 at 15:30
  • @magnetik: can you tell me (us) which version marks the change between dmode/fmode and dir_mode/file_mode? – iconoclast Dec 09 '14 at 16:56
  • Don't know, on ubuntu 14.04 it's dir_mode. – magnetik Dec 09 '14 at 18:16
  • @magnetik: thanks for the info. I'm not sure why the mod-nazis rejected your edit, since it was clearly useful to anyone reading the answer. But it's been added now despite their efforts to "improve" StackOverflow. – iconoclast Dec 09 '14 at 19:37
  • Thanks. But how do you use this? Someone gave me a bunch of code and said to run `vagrant up`. Can I mount something into the running virtualbox? Can I use it when restarting the virtualbox? Do I have to rerun the vagrant up? Does it go in the Vagrantfile? Might it conflict with anything else in the Vagrantfile? I know that's lots of questions, but just a basic notion of how to use this code block, or a link to documentation on the sort of way that you use this would help set me off in the right direction. – nealmcb Oct 30 '15 at 20:44
  • @nealmcb: I would try to answer some of your questions briefly, but they've very unclear: what is this "bunch of code"—a project or code for the Vagrantfile? What is the "it" you want to use when restarting the virtualbox VM? Also, you just asked ***several*** question inside a ***comment***. It is *far* better to post questions *as **questions*** to StackOverflow—if they have not already been answered. And it might be good to take a look at the Vagrant documentation first—you may find several answered right away. Best of luck. – iconoclast Oct 31 '15 at 16:24
  • Right - you're very kind. The "it" is your code. I want to help make this answer more self-contained or easy for someone with really minimal knowledge of vagrant. After experimenting a bit, I found that it worked to add it to the end of my Vagrantfile, use arguments to `config.vm.synced_folder` that specified first a path name on the local host, then one inside the container, make sure I used an "id" that was valid _inside the container_, or just drop that part, run "vagrant up", and I was good to go, as I recall. But some of that may vary, so some expert hints would be even better. – nealmcb Nov 01 '15 at 22:29
  • Just a small tip - when you replace the and the values should be put inside quotes. – authentictech Oct 26 '19 at 14:21
7

Im not sure if this quite answers your question, but I had issue with my applications not being able to write to the disk on a shared folder system so I used the following in my Vagrantfile to allow r/w access to any shared files from within the client.

config.vm.synced_folder "./vhosts/", "/var/www/vhosts/", id: "vagrant-root", extra: "dmode=777,fmode=777" 
carbontwelve
  • 1,090
  • 1
  • 13
  • 24
6

Have you tried setting up a Samba server or something similar so that you are sharing the guest file system with the host rather than the other way around?

I ran into issues with the built in sharing in Vagrant as well so I just setup everything the way I wanted on the guest and then setup a simple Samba share to force the user, group and default file creation mask etc. This was to work with a Windows machine though so there could be a better choice than Samba...

I had an ubuntu guest so setup was just as simple as running

$ apt-get install samba

Add a share block for each folder you want to share in the /etc/samba/smb.conf file, something like:

[data]
    comment = Local Dev Server - /data
    path = /data
    browsable = yes
    guest ok = yes
    read only = no
    create mask = 0777
    force user = root
    force group = root
#[data] End
Matt Cooper
  • 9,962
  • 2
  • 31
  • 26
  • This is how we currently run our VMs (with files stored locally inside the VM and shared outwards to the host). However, I was hoping Vagrant offered something better, especially since it offers a convenient way to destroy and rebuild VMs. – Andrew Jul 07 '13 at 23:59
  • Sounds like you are already doing it the right way then... I don't think there is a better way to be honest... its not really a limitation of Vagrant as such either... more likely virtual box that makes it more difficult... perhaps a VMware vm would be better but I haven't used one before so can't really comment. – Matt Cooper Jul 08 '13 at 20:30
  • +1 Is there a standard way to auto-edit files using Vagrant/puppet/chef such that the script could detect and if necessary add the share block to the `smb.conf` file so this does not need to be done manually? – therobyouknow Dec 03 '13 at 12:46
  • 1
    Yes there is, both puppet and chef have the concept of template files, so you can just make a template from the smb.conf file and have that placed in the correct location on provision... check out the chef example under the "Using Templates" heading here: http://docs.opscode.com/resource_template.html – Matt Cooper Dec 03 '13 at 19:30
  • +1 thanks @MattCooper for this link, I'll need to study it as it's rather inaccessible to the newcomer to puppet and there are no examples, but it is a start - so thanks. There is also [puppet-samba](https://github.com/ajjahn/puppet-samba) which in theory can be incorporated into a Vagrant setup, but again the documentation is not user-friendly as there is no *full* example provided - so I've raised an [issue about improving the documentation](https://github.com/ajjahn/puppet-samba/issues/22). I expect to continue research - and if I can improve these myself, I will. – therobyouknow Jan 17 '14 at 08:56
  • This is exactly what I want. How come I keep getting `"Failed to retrieve share list from server: Connection refused"` when trying to open the folder in the host (Ubuntu)? – Redsandro Apr 09 '14 at 15:27
4

Update: Have a look at http://docs.vagrantup.com/v2/synced-folders/basic_usage.html again, at the bottom. Example (copy-n-paste from docs):

config.vm.synced_folder "src/", "/srv/website", owner: "root", group: "root"
Felix Rabe
  • 4,206
  • 4
  • 25
  • 34
0

Did you try synced folder?

I have been using Vagrant + VirtualBox for a while, and we use the synced folder to share code between host and Vagrant VM.

Here's a link:

http://docs.vagrantup.com/v2/synced-folders/basic_usage.html

Here's a code example:

Vagrant.configure("2") do |config|
  config.vm.synced_folder "src/", "/srv/website"
end

A little bit background

We setup the VirtualBox as our development environment, and we run our application, and host database server, Tomcat server inside VM. We have our code shared between host and VM via Synced folder so that we could develop using Eclipse which is running on host.

Environment: Mac

Mingyu
  • 31,751
  • 14
  • 55
  • 60
  • I am using synced folders, but they don't allow altering of owners or permissions from the guest side. – Andrew Jul 04 '13 at 04:15
  • And because the files are 'stored' on the host, not the guest, symlinks etc dont work in the shared filesystem when the host is windows. (thats the problem that lead me to find this question - going to try samba) – barryhunter Sep 04 '13 at 17:34
  • Yes I had that problem too, so just set the permissions to 777 or 666 on the host side. – therobyouknow Dec 03 '13 at 12:47
  • Are you using Windows or Mac? When using Windows 7 on the same setup as you, I ran into this problem - http://stackoverflow.com/questions/23089023/maven-clean-fails-on-linux-vagrant-shared-drive. But, on a previous project where I used Mac, I don't remember encountering such a problem. – Kevin Meredith Apr 16 '14 at 19:51
  • I used Mac... I have updated the response to include this piece of information – Mingyu Apr 16 '14 at 19:53
-1

You could try copying the codebase from the shared folder into a guest machine specific folder, say from /vagrant/codebase to /codebase. Once you get the files all the way into the guest machine, you can chown them like normal. It isn't ideal, but you could write a simple script to run whenever you modify the code from the host side. You could also try a start script that copies in any files when the VM boots, depending on how often you need to update the files.

bfitzpatrick
  • 1,513
  • 11
  • 13
  • 2
    We are using these VMs for development, which means the files are being changed constantly throughout the day. We use the host machines for development (running IDEs). Having a copy of the files that requires syncing will introduce a major source of problems. – Andrew Oct 28 '13 at 23:37