Vagrant syncing process - vagrant

I've just started using Vagrant and I've managed to set up a box ok it seems. I have been a little confused about syncing folders work though. I took from Vagrant that syncing allowed you to use the file system on your Mac and then sync those changes into your virtual machine, is that right?
I have these settings currently
config.vm.synced_folder "../../../Library/WebServer/Documents/", "/Users/jonnny/ubuntu-vagrant/www"
I would expect that now that when I look in chrome that inside my 127.0.0.1:1234/www/
That the files within my Library/WebServer/Documents/ would be there. But they're not, have I done it wrong or misunderstood what's going on here?
Update
I wasn't sure what you meant with my file/folder structure, so probably best I just show them.
Users
Jonnny
ubuntu-vagrant
- Vagrantfile
- /www (dir)
- bootstrap.sh
VagrantFile
VirtualBox VMs
- ubuntu-vagrant_342374982374923
- box-disk1.vmdk
- logs (dir)
- ubuntu-vagrant_342374982374923.vbox
I did try your suggestion and reload the box, but the folder still didn't seem synced.

The syntax seems to be correct. But for the second argument, /Users/jonnny/ubuntu-vagrant/www, it should be the targeted location inside the ubuntu filesystem, not the one which will be created on your Mac OS filesystem.
Try something like /www/ or /usr/johnny/www, according to the location you want.
Update
For the vagrant arguments:
the first should be the location of the folder you want to share inside your Mac Os filesystem relatively from where the Vagrantfile is in your Mac Os filesystem.
the second is where inside the ubuntu-vagrant filesystem you want to access these files.
I think your folder structure is not good: you dont need a Vagrantfile folder or a www folder in your Mac OS filesystem. Just consider one folder: the one where the Vagrantfile is, Users/Johhny/ubuntu-vagrant. The www folder will just exists inside the VM. Then if the Library folder is at the same level than the Users folder in your Mac OS filesystem, you should have :
Library
...
Users
Johhny
ubuntu-vagrant
Vagrantfile
bootstrap.sh
And inside the Vagrantfile you should have this line :
config.vm.synced_folder "../../../Library/WebServer/Documents/", "/www", create: true
According to the doc, for the create: true paramater : If true, the host path will be created if it does not exist. Defaults to false.

Related

Vagrant - set the location of the virtual hard drive for VirtualBox

I have executed following commands (on Windows, using Git Bash) in the directory D:\vagrant\precise32\02-lamp\
$ vagrant box add precise32 http://files.vagrantup.com/precise32.box
$ vagrant init precise32
$ vagrant up
Note. I haven't changed original Vagrantfile.
I thought the directory D:\vagrant\precise32\02-lamp\ would be the place of the VDI-like file but it is not. The working directory serves as the shared folder.
I found the location of the Vagrant box
C:\Users\USER\.vagrant.d\boxes\precise32\0\virtualbox
According to Where is Vagrant saving changes to the VM I found in the VirtualBox GUI the location of the Virtual hard drive file. Which is
C:\Users\USER\VirtualBox VMs\02-lamp_default_1458429875795_57100\
I would like to put this file not in the system drive C:\ but in the data drive which is D:\.
How to set such vagrant configuration?
For VirtualBox, you can change the location of what is known as the Default Machine Folder through the GUI's Preferences dialog box.
This guide, while a couple of years old, works fine and I followed it last week for how to move an existing vagrant/VirtualBox drive to a new location.
EDIT
I have quoted the steps from the above link/guide, for posterity:
Move ~/.vagrant.d to the external drive. I renamed it vagrant_home so
I'd be able to see it without ls -a.
Set VAGRANT_HOME to
/path/to/drive/vagrant_home in ~/.bash_profile.
Open the VirtualBox
app, open Preferences, and set its Default Machine Folder to
/path/to/drive/VirtualBox VMs.
Close VirtualBox.
Move your
VirtualBox VMs folder to the drive. Reopen VirtualBox. You'll see
your VMs are listed as "inaccessible". Remove them from the list.
For
each VM in your VirtualBox VMs folder on the external drive, browse
to its folder in Finder and double-click the .vbox file to restore it
to the VirtualBox Manager. (Is there an easier method than this?)
Finally, move any existing Vagrant directories you've made with
vagrant init (these are the directories with a Vagrantfile in each) to
the external drive. Since these directories only store metadata you
could leave them on your main drive, but it's nice to keep everything
together so you could fairly easily plug the whole drive into another
machine and start your VMs from there.
It is also possible to do this via CLI for when you ALWAYS want to change where Virtualbox creates the VMs during import (because Virtualbox usually wants to put them in a single place rather than tracking wherever they live on the disk the way VMware does it).
Note that changing this setting via the GUI or CLI does NOT move existing VMs, it will simply set a new path to be used by the next machine imported/created. If you have existing machines you want to move, you could shutdown and close all of the instances of Virtualbox and then use mv /old/path /new/path from a cmd/shell window or cut and paste the folder to the new location in the GUI and then change the machinefolder to that path and open Virtualbox and it should detect all the existing VMs.
Using the CLI makes it much easier to script/automate if you have a large number of users needing to move the VMs path out of their home directory to avoid huge files getting automatically backed up. The "best" place for the VMs depends a little bit on your system, but /usr/local/ can be a good place to create a new folder on macOS or Linux.
# Look at the current path
vboxmanage list systemproperties | grep machine
# Output (commented for easier copying and pasting of commands)
# Default machine folder: /Users/<YourUser>/VirtualBox VMs
# Set it to a different folder in your home aka ~
# If you user has access to the path and can create files/folders, then
# the folder doesn't need to exist beforehand, Virtualbox will create it
vboxmanage setproperty machinefolder ~/VirtualMachines
# No output produced
vboxmanage list systemproperties | grep machine
# Output (commented for easier copying and pasting of commands)
# Default machine folder: /Users/<YourUser>/VirtualMachines
You can also set it to a folder outside of home, but this usually requires creation of the folder and the permissions to be fixed before Virtualbox can use it.
# [Optional] Only needed if moving out of the home directory to
# a place the user doesn't have permission to access by default
sudo mkdir -p /usr/local/VirtualMachines && \
sudo chown -R ${USER} /usr/local/VirtualMachines
# If you add : like this `${USER}:` to the above, instead of
# setting the group to admin or wheel it will use the user's default group
# which can be seen by running `id -g`
vboxmanage setproperty machinefolder /usr/local/VirtualMachines
# No output produced
vboxmanage list systemproperties | grep machine
# Output (commented for easier copying and pasting of commands)
# Default machine folder: /usr/local/VirtualMachines
If you change your mind you can easily set it back to the default, but you'll need to move your VMs back again yourself.
vboxmanage setproperty machinefolder default
vboxmanage list systemproperties | grep machine
# Output (commented for easier copying and pasting of commands)
# Default machine folder: /Users/<YourUser>/VirtualBox VMs
For each VM in your VirtualBox VMs folder on the external drive, browse to its folder in Finder and double-click the .vbox file to restore it to the VirtualBox Manager. (Is there an easier method than this?)
There is an easier way...
Go into the VirtualBox Manager GUI click Machine > Add and locate the .vbox you'd like to add back.

Vagrant and synced folders: How to set the owner to a user other than the default vagrant user?

I am trying to set up a development environment for a Rails 2 project within a CoreOS Vagrant VM that has a number of moving parts.
Once the VM is provisioned there is a Puppet script that installs all the bits and pieces and starts them up.
The bit that I am working on, which is on my local machine (OS X 10.10.5), is exposed in to the VM via a synced folder and appears in the right place in the VM with owner vagrant and group vagrant.
In order for it to work with the rest of the system the owership of that folder needs to be set to projectx instead.
Simply telling Puppet to set the permissions doesn't work. They don't change. I've confirmed this by manually chowning the files and they don't change.
I can't just set the user in the Vagrantfile though as, until Puppet has done its stuff there is no projectx user.
It's looking like my only solution is to hack the Puppet files and eliminate the use of the projectx user but I'd rather find a way to set the owner of the synced folder instead as that would be much cleaner.
How do I set the ownership of a synced folder to a user that is not created until after the VM has been provisioned?
Unfortunately there are no easy ways to do that.
Common workaround is to use uid and gid in sync folder configuration. This way you can create a synced folder as a user who will be added to the system later.
projectx_uid = 1001
projectx_gid = 1001
config.vm.synced_folder "src/", "/srv/website",
owner: projectx_uid, group: projectx_guid0
This means projectx should be created at the very beginning of the provisioning process so it will have predictable uid/gid
Related issue in Vagrant's tracker: https://github.com/mitchellh/vagrant/issues/936

Cannot access Vagrant shared folders

I just trying to add Vagrant to my workflow and I have following probably noob problem. I pull standard hashicorp standard 14.04 image, vagrant up it, SSH to it install my python requirements on it and then then try to execute build commands against code in Vagrant shared folder and run in to problems. Basic errors I get say those locations don't exist or cannot be found.
First action I, go ls /vagrant and can see my shared folders. I cannot cd to them from Vagrant machine I have tried to halt the machine.
Vagrantfile shared folder code.
{"virtualbox":{"/vagrant":{"guestpath":"/vagrant","hostpath":"/Users/Kimmo/Documents/Mist.io","disabled":false}}}
I am using Virtualbox as provider newest version.
My dev machine is OSX 10.9.5
There are not access limitations on the folder itself.
Thanks for you help in advance :)
Does /vagrant exists inside the VM just after you start it ?
If no you can add this parameter : "create":true. According to the doc, for the create: true paramater : If true, the host path will be created if it does not exist. Defaults to false.
If the folder /vagrant exists but you can cd or ls it, you can add parameters wich will define the right/owner of this folder:
owner: (string) the user who should be the owner of this synced folder. By default this will be the SSH user. Some synced folder types don't support modifying the owner.
group: (string) the group that will own the synced folder. By default this will be the SSH user. Some synced folder types don't support modifying the group.
Also for the hostpath parameter you have to give him a folder path, not a file path. In your conf I can see: "hostpath":"/Users/Kimmo/Documents/Mist.io". If Mist.io is a file and you want to access this file inside your VM, just give the path to the folder containing this file, /Users/Kimmo/Documents in your case.

Understanding Vagrant Synced folder setup with the default & html folder

I'm working on getting a Vagrant setup running for the first time on a Windows 8.1 machine. I've been pouring over all the documentation to understand how it all works. I used puphpet to help me configure a Ubuntu 14 LAMP development box to use.
My confusion is around how the synced folders are setup. It seems that the synced folders on install, always create a new 'default' and 'html' folder in whatever local directory I specify in my puphpet setup. I then have to put any code files I want to run in the default folder.
For example: host folder: ./www ends up having /www/default and www//html in it after Vagrant up. Why is this occurring? I left the remote folder to the default /var/www.
My ideal setup was to have a Vagrant box folder setup on my machine, and then have my code project reside in the same directory as the Vagrant box files. However, it seems that I can only put the code project in the 'default' directory that Vagrant creates.
I can't find any mention of this in the documentation. Is this normal behaviour for this to be happening? If not, how can I get around this so that I can keep the code in whatever directory that I want?
Here's the relevant code from the puphpet config file:
synced_folder:
Rh7fCuil7xE4:
source: ./www
target: /var/www
sync_type: default
rsync:
args: '-z'
exclude: .vagrant/
auto: 'false'
Author of puphpet here.
Nginx creates a folder for its default vhost (same with Apache). That's what those two folders are. It also appears there's a small bug in puphpet that will create both folders, even though one is the right one. I'll see about fixing it, but it shouldn't affect your VM in the slightest.
/var/www is what you've defined as your target, so anything that appears in your master's ./www will also appear in your VM's /var/www and vice-versa.

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