0
votes

I'm using Vagrant to set up an ubuntu box on Virtualbox. In the vagrant file I've specified synced folders to sync my code with the guest:

config.vm.synced_folder "/host/target/webapp", "/guest/webapp"

The problem I have only appears when I the machine is destroyed and then rebuilt with vagrant up. In this case Vagrant syncs my folders on the guest to my folder on the host, instead of host to guest.

"/guest/webapp" ---> "/host/target/webapp"

I don't like this behaviour at all, if I haven't checked in my code on the host to my version control I will loose my code.

Should the sync be bidirectional? Is there a way to configure it to only sync one way preferable host to guest. Could not find anything about this in vagrant basic sync

In comments below there where suggestions that it where my provisioning (Chef-Solo) that where the cause of the problem. To exclude the provisioning phase from this I did following:

Vagrant up --no-provision

The hosts content where synced to the guest.

rm -r /guest/webapp/ on the guest

Resulted in the hosts content where removed as well.

mkdir /guest/webapp/test on the guest

The created folder test where also created on the host.

I think this proves that the sync is bidirectional.

1
This is really weird as I have only seen host --> guest, so even if you sync an existing folder on guest, it will be replaced by the content of the folder from host. Also I have some doubts, if host folder was replaced from content of guest, then your current folder will be empty as vagrant sync a default folder as /vagrant with the current folder. for this case (the /vagrant folder) you confirm it works correctly ? - Frederic Henri
I understand your doubts. I'm using Chef-Solo for provisioning which put standard data to "/guest/webapp", I don't know which happens first, the sync of the folders or provision which loads the standard data. This only happens if it's the first provision, in other words, when I run any Vagrant reload/up --Provision and the box already is created the host folder won't be overwritten. - Jonathan Andersson
Mounting shared folders happens before provisioning. Just run vagrant up first time with no provisioning vagrant up --no-provision and you will see the host folder is not altered, it must come from your provisioning - Frederic Henri
How do you think the provision could change/files folders at host? Is that even possible? I pretty sure my chef recipes don't change any files or folders on the host. As far as I know Chef dose not change "/host/target/webapp". - Jonathan Andersson
I dont have knowledge of chef so I can't comment but did you run vagrant up first time with no provisioning vagrant up --no-provision and you will see the host folder is not altered - Frederic Henri

1 Answers

1
votes

so vagrant Synced folders are really synced (probably why they're not just called shared folder) so once you sync your folder whatever action you make on one side will be synchronized/replicated automatically on the other side. (wether it comes from the host or the guest)

If you really want a one-way sharing, you can look at [rsync] (https://www.vagrantup.com/docs/synced-folders/rsync.html)

Vagrant can use rsync as a mechanism to sync a folder to the guest machine. This synced folder type is useful primarily in situations where other synced folder mechanisms are not available, such as when NFS or VirtualBox shared folders are not available in the guest machine.

The rsync synced folder does a one-time one-way sync from the machine running to the machine being started by Vagrant.