What is the difference between shared folder and synced folder in Vagrant? - vagrant

While going through Vagrant tutorials, I have seen two options for sharing folders between host and guest machines - shared folders and synced folders. What are the difference between them?
Or, is synced folder the new name for shared folder, in Vagrant 2?

Shared Folders is more VirtualBox specific (vboxsf) and have known performance issues as number of files grows.
Vagrant v2 (For vagrant 1.1.x and 1.2.x) docs use a more generic name Synced Folder, which includes the default vboxsf and NFS.
In addition to vboxsf and NFS, sshfs is also worth looking into if your host is running Linux.

As you guessed, synced folder is the new, more generic name for a feature that is not only VirtualBox specific anymore as of Vagrant 1.1.

Related

What is the logical/conceptual difference between .vagrant and .vagrant.d directory?

I want to know logical/conceptual difference between .vagrant and .vagrant.d directory
And also, what is the difference between the following keys
(Please observe the path of these two) one of which is placed in .vagrant directory and the other one in .vagrant.d directory in Windows 7?
C:\Users\username**.vagrant**\machines\default\virtualbox\private_key
C:\Users\username**.vagrant.d**\insecure_private_key
Could not find anywhere in the documentation and on the Internet as well. Thanks in advance.
.vagrant.d is usually placed in your home folder and stores system-wide data/settings, i.e. downloaded vagrant boxes.
.vagrant is placed in your environment/project folder and stores project-specific settings, i.e. current VM settings.
So if you work in user/myproject, create Vagrantfile there and exec vagrant up, then user/myproject/.vagrant directory will be created with VM specific data.
.vagrant.d/insecure_private_key is default insecure private key to access your VMs, it's replaced by vagrant on first VM start with private_key from project's .vagrant directory. In your example: private_key for default VM started with virtualbox provider.

How can I retain shared folders when cloning VMs using virtualbox and vagrant

First, I am on a Mac. Second, I have a virtualbox VM which was created using vagrant and which uses a shared folder to easily pass files back and forth, etc.
I would now like to clone this VM from a particular state so that I can upgrade an application on it and move forward with it. The issue is that the only way I know of to use shared folders here is to start the box using vagrant up (this makes sense as vagrant mounts the folders as part of its boot process); however, using vagrant up always triggers the original VM.
Is there a way to create a clone of a VM using Virtual Box and then to be able to use shared folders so I can easily copy files to and from the host and guest via ssh?
Did some more researching and found that I can mount a shared folder in a clone in the same way I can with the original virtualbox VM using:
mount -t vboxsf -o rw,uid=33,gid=33 <shared_folder_name> <guest_folder>
Note that the uid and gid specified here relate only to Debian-based systems. CentOS IDs are different.
For more on the technique and for solutions for CentOS boxes, see here: http://jimmybonney.com/articles/configure_virtualbox_shared_folder_apache_virtual_host/
I've tried the steps in the above article to allow for auto mounting the shared folder when the VM boots, but I've had no success. As a work-around (which I find acceptable for now), I created an alias in my .bashrc file which seems to work fine.
I would now like to clone this VM from a particular state so that I can upgrade an application on it and move forward with it
one thing you can look is vagrant snapshot
Snapshot works with VirtualBox provider to take a snapshot of your VM at the particular point of time when snapshot is taken. You can then continue working on your VM and when needed you can easily recover from a previous snapshot

Using Vagrant, is there a way to share/sync folder across multiple VMs?

I want to have multiple VMs running on my system and for them each to have one or two folders synced with the host. I'm using vagrant coupled with virtualbox. When there is already a VM up (via vagrant up) Then a new VM is created (via vagrant up in another directory containing a different Vagrantfile) it doesn't get the shared/synced folder mounted. Is it possible to share/sync folders across multiple VMs and if so how?
Note: I have also tried using multi-machine i.e. editing the Vagrantfile to spin up multiple machines, but only one machine gets the shared folder.
Edit 4/22/15:
From discussion below, I believe this problem may result from using Virtualbox specifically. Will update after trying to repro on another OS and using another VM application.
As described in official documentation you can setup specific synced folders in Vagrantfile. So you can setup the same shared folder for every project by simply putting something like this into every Vagrantfile
config.vm.synced_folder "../host_shared_src_folder", "/guest_src_folder"
So I have Vagrantfile like this
Vagrant.configure(2) do |config|
config.vm.box = "base_ubuntu"
config.vm.synced_folder "../shared", "/shared"
end
And folder's layout
testvagrant
| -- shared
| -- testenv1
| -- Vagrantfile
| -- testenv2
| -- Vagrantfile
Then I just do vagrant up in both testenv1 and testenv2 folders and can see (and write to) shared folder under /shared

Reconnect synced folders from Vagrant after VirtualBox restart

I have created a puppet/vagrant/VirtualBox generated installation of Ubuntu running on a Windows host; see https://bitbucket.org/dmenne/rstudio-shiny-server-on-ubuntu for details. This allows users to run RStudio and Shiny server on Windows. In Vagrantfile, I have
config.vm.synced_folder "shiny-server", "/srv/shiny-server", create:true
to create a shared folder. When I start the VM with vagrant up or vagrant reload, everything works ok.
One customer does not want to install vagrant etc, and asks for a standalone VM to be started/stopped with the VirtualBox Manager on Windows. However, after I shutdown a vagrant-booted VM Box, my synced folders do not connect/mount again on VM restart, even if they turn up correctly in the shared folders dialog of the VirtualBox Manager.
How can I shrink-wrap a Vagrant-generated VM so it can be started/stopped without Vagrant?
EDIT March 2015:
I still could not resolve this. When I force automount, on restore media/sf_<folder> is synced, not the required folder. How can I force VirtualBox to use sync-template from Vagrantfile after a restart?
And how do I force automount in vagrant without doing it manually.
After some further debugging and the lack of response to a similar query on the google/vagrant forum:
When you use synced folders in puppet, you must restart VirtualBox with vagrant up or vagrant reload
If you use the VirtualBox GUI to stop/restart, automount restores the synced folders incorrectly, i.e. to /media/sf_; this cannot be corrected without using a vagrant reload,
The only alternative without vagrant is to save the state in VirtualBox (CTRL-V). After a restart, the synced folders are restored correctly. However, it is probably impossible to force end-users to always manually save the state.
To use persistent synced folders, you must use upstart or similar methods; it cannot be done with Vagrantfile only.
See also Alvaro's response:
https://groups.google.com/forum/#!topic/vagrant-up/oBU0kqPLzYk
There are two parts to getting this to work. First, we need to get the synced folders automatically mounting from the host, which is the case of a Linux guest is a bit of a misnomer, because it's more "being automatically made available for mounting", or "automatically shared". For each shared folder (gemainsame ordner) you must turn on auto mount (automatische einbinden)
To do this, click on the edit shared folder icon for each share
And check auto mount (automatische einbinden)
At the end you should see "Ja" under "automatische Einbinden" for all the shares to should auto-sync
If the guest system is Ubuntu (or any Linux system), then having done this, all the synced folders should now be visible within to the guest, but the guest won't automatically mount them. You will need to put an entry into your /etc/fstab for each folder you want automatically mounted in the guest.
Since you are using VirtualBox, Vagrant will presumably choose to implement the synced folders using VirutalBox's shared folder provisioning method. To the guest system in Linux, depending on your version of Guest Additions, this looks like either an smb/cifs share with the name (by default) of //vboxsrv/, or a file system of type "vboxsf".
The vboxsf is from more recent version of guest additions (4.1.18 onwards at least), and automount option 'just works'. On booting a Linux guest VM with an automount set share called 'bob', the system should mount Bob automatically (without an fstab entry) at /media/sf_Bob. The full output of the relevant line from mount is
none on media/sf_Bob type vboxsf (rw,nodev,relatime)
If this isn't working, check that you can see a samba network share from within your guest system, using smbclient or smbtree. It should look like //vboxsrv/Bob. In your fstab then, you would add a line like this
#share name #mount point in guest #fstype #options #dump/pass
//vboxsrv/Bob /mnt/Bob cifs auto,rw 0 0
You may wish to read the manual page for mount.cifs to tune the options for each mount, especially with regards to file ownership and permissions (relevant options to investigate are forceuid, uid, forcegid, gid, file_mode, and dir_mode)

Is there a way to unsync a subfolder in Vagrant?

There are few subfolders in my project folder that wouldn't need to be synced to the guest machine. (In particular the .git folder, which contains >800 files.) Is there a way to unsync subfolders of a synced folder in Vagrant? Or how should I prevent unneeded folders from being synced?
Disabling sync this way doesn't seem to work, when /vagrant/ is synced by default:
config.vm.synced_folder "www/kisa/.git/", "/vagrant/www/kisa/.git/", disabled: true
Normally (with vboxsf, vmhgfs, NFS, ...) the syncing is done by mounting the specified directory from the host to the guest. For performance reasons there should be no need to prevent some content from "syncing", as the data is not transferred unless you access it from the guest. If you write the data on the guest but don't want to sync it back to the host, easiest is to write it somewhere else. =)
The upcoming Vagrant 1.5 will include rsync synced folders which will support rsync__exclude option. Some cloud provider plugins (aws, digital_ocean, ..) already use rsync, but support for excluding depends on the provider. In some cases you just have to sync only the wanted folders separately. You can disable the default sync with
config.vm.synced_folder ".", "/vagrant", disabled: true
I can't pretend to understand this, but the method shown at this. seems to work for me for a Linux (Ubuntu) guest on a windows 7 host.
I wanted to use it for exactly what he describes - to exclude a node_modules directory from being shared.
In case the link disappears the gist is:
$ mkdir ~/vagrant_node_modules
$ sudo mount --bind ~/vagrant_node_modules /vagrant/node_modules
so that /vagrant is shared but /vagrant/node_modules is not.
Someone more knowledgeable than me might know if there are problems with this.

Resources