Full Duplex Shared Folders in Vagrant - vagrant

I have a guest box that has data I want to sync to the host so that I am able to edit it using an editor on the host. I want any saved changes made on the host to sync back to the guest. I've tried using normal shared folders config.vm.synced_folder "workspace/", "/my/folder/to/sync/" but this just deletes the contents of the guest folder!
I've looked at using rsync but I'm unsure if this supports editing and syncing both ways

Rsync is type performs one-way synchronization fromm host to guest. When you boot the VM, Vagrant copies the contents of the synchronized folder from host to guest. Vagrant does not monitor the folder on the host for any changes. To change this behavior, use the rsync_auto parameter set to true.

Related

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

Vagrant .vagrant file on Dropbox

I'm using Vagrant on two machines (home and office) with my working directories stored in Dropbox. I regularly run into the pronblem:
The VirtualBox VM was created with a user that doesn't match the
current user running Vagrant. VirtualBox requires that the same user
be used to manage the VM that was created. Please re-run Vagrant with
that user. This is not a Vagrant issue.
This is easily solved, brute-force, by rm -rf .vagrant, but I'd like to find a more elegant solution -- like an easily automated way to put the .vagrant files elsewhere.
Update
Here's the scenario: a working directory shared between office and home machine via Dropbox.
I vagrant up on office machine, successfully.
I shut down office machine and go home.
I get the message about on vagrant up, saying the id values aren't the same as when I created. (503 vs 501 in general)
I do rm -rf .vagrant and do vagrant up again, successfully.
When I go back to the office, same problem.
On the original issue
I get the message about on vagrant up, saying the id values aren't the
same as when I created. (503 vs 501 in general)
you can fix it by editing your UID in the following file .vagrant\machines\default\virtualbox\creator_uid and changing the 501/503 to a 0. (0 means you run as sudo)
But I think another issue that will pop up is that each of your machines is creating its own VM and stores it under your /Users/user/Documents/Virtual Machines and you're not sharing those files.
when you create a VM using VirtualBox, vb will assign the VM and id - this id is key and will make the link between vagrant and vb. As you create 2 different VMs, the VM have different Id and vagrant is not able to switch between the 2.
Hope this is clear so far, but how to go from there - you would have couple of options:
apparently you don't mind much about the content of the VM (as you do rm -rf so you recreate the VM each time you switch computer) so you can create a .vagrant.home and .vagrant.office and switch the .vagrant to point to the corresponding folder when you change machine so at least you don't need to recreate the VM, you can just start the VM from the corresponding computer. This is not ideal but will work.
you can avoid switching the .vagrant directory from above points by setting the env variable VAGRANT_CWD so you will not store the .vagrant directory under your dropbox account but you can have a script that export this variable and store the .vagrant directory separately on each of your machine (a folder that is not shared) so each machine will create its own VM.
You can have the VAGRANT_CWD set in your bash profile for each computer if you plan to have a different location, but you can also have for example a workhere.sh script that will just do export VAGRANT_CWD=/folder... if you have the same settings on both computer and just do source workhere before you do vagrant up
the other option (I did not test) will involve a few steps: export/import the VM once created from VirtualBox into one computer to another. The issue here will be to keep Id. I am not sure but normally virtual box should keep the Id when you import into the other computer.
you can check the file under /Users/<user>/Library/VirtualBox/VirtualBox.xml and review the entry under <MachineRegistry>, you will get the uuid of the machine entry, make sure they match between your 2 computers after you export/import
for vagrant there is another file you need to sync between your 2 computer /Users/<user>/.vagrant.d/data/machine-index/index which list of available VM on the computer. If you have other VM on one computer not available on this other, it would be ok to sync but best is to copy only the necessary entry for the VM you want to sync.
After you've done this and you sync the .vagrant folder, you should be able to vagrant up from one computer or the other.
Again, I did not test all the steps but I assume this would work.
Few notes:
this will work only if the 2 computers have the same settings (same OS (if not version at least same family), folder structure as some files involve full path, same virtual box version ...)
vagrant box should be in sync on the 2 computers (but I assume this is the case as you can vagrant up from same Vagrantfile)
if you destroy/create the VM on any of the computer, you will need to sync again the Id as virtual box will assign a new Id each time you create a new VM
Vagrant provides an environment variable for exactly this use case: VAGRANT_DOTFILE_PATH
See the documentation here:
https://www.vagrantup.com/docs/other/environmental-variables.html#vagrant_dotfile_path
VAGRANT_DOTFILE_PATH
VAGRANT_DOTFILE_PATH can be set to change the directory where Vagrant stores VM-specific state, such as the VirtualBox VM UUID. By default, this is set to .vagrant. If you keep your Vagrantfile in a Dropbox folder in order to share the folder between your desktop and laptop (for example), Vagrant will overwrite the files in this directory with the details of the VM on the most recently-used host. To avoid this, you could set VAGRANT_DOTFILE_PATH to .vagrant-laptop and .vagrant-desktop on the respective machines. (Remember to update your .gitignore!)

Vagrant synced folders disappearing

I'm using vagrant and have hashicorp/precise32 box. My projects in /products/ folder on this box. I want to reach this folder from my host computer. So added this configuration to my Vagrantfile
config.vm.synced_folder "/products","/products"
Also created /products folder on host. But after reload when I connect to box over SSH, /products folder's all contents are disappearing. When I comment out the config.vm.synced_folder line and reload, contents appering again.
Can you please tell me what's I missing?
Thank you.
Vagrant sync folder works the other way around
Synced folders enable Vagrant to sync a folder on the host machine to
the guest machine, allowing you to continue working on your project's
files on your host machine, but use the resources in the guest machine
to compile or run your project.
so if you have an existing folder on your guest machine and you ask vagrant to create a sync folder with one of your folder from the host, it will do but replace the content from the content of your host.
There is an old question with some good reference to understand it further

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