Vagrant - VM can not follow host-symlink within virtualbox synced_folder - vagrant

I set up a vagrant machine to maintain an older project of mine. I use VirtualBox as provider and created a synced_folder to publish the project to the vm.
The amount of assets is kind of huge (~40GByte), so i dont want to keep that on my ssd. So i moved those assets to an hdd and created a symlink from my project-folder pointing to the hdd. All of this happens on the host system.
My problem is, that when i ssh into my vagrant machine and inspect the assets-folder i can see a broken symlink. I want my vm to follow this path.
The folder structure (the part, that matters) looks like:
root/
|--src/
|--public/
|--|--assets -> /media/hdd
As you can see /root/public/assets points to my hdd mount.
My synced_folder is set up like
Vagrant.configure("2") do |config|
# skipped lines
# skipped lines
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
# skipped lines
# skipped lines
end
Now i dont know how to configure the synced_folder to follow the path on the host machine.
I thought about having another line like
config.vm.synced_folder "/media/hdd", "/vagrant/public/assets", type: "virtualbox"
but i am not the only one using this setup, so i would like to solve it with some kind of option, like:
config.vm.synced_folder ".", "/vagrant", type: "virtualbox", options: ["optionIDontKnowAbout", "1"]
What i tried so far:
config.vm.provider "virtualbox" do |vb|
# should allow symlinks in synced folders
vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
end
But that did not help.
Thanks in advance

For the moment i use the following workaround. It does not answer my initial quesion, so i wont delete it:
if ENV['USER'].downcase == 'yourusername'
config.vm.synced_folder "/media/hdd", "/media/hdd", type: "virtualbox"
end

Related

Vagrant synced folder causes contents to be erased

I'm trying to add add an additional synced folder besides the default /vagrant.
I've read the documentation which indicates that this should be as simple as:
config.vm.synced_folder "/path/to/somewhere/on/host", "/path/to/guest_directory", create: true
When I add this to my Vagrantfile and then reprovision it, the guest_directory folder shows up in the expected place on the host, but it's empty. And the folder in the guest OS is also empty... which breaks my site because those are the website files. Interestingly, if I remove that from the Vagrantfile and reprovision again, the files reappear in the guest OS where they were originally.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
# NEITHER OF THESE WORK...
# config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/home/vagrant/web__public_web", create: true
# config.vm.synced_folder "./web__public_web", "/home/vagrant/web__public_web", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
You can see that I've tried a two different type: options and got the same wrong result with both. I also tried placing a symlink to the project files inside /vagrant/ and that didn't work either. (The broken symlink showed up in the host os directory instead of the files I want to access)
Here are a couple more details about my setup, just in case they're relevant:
In the guest OS the project files live in /home/vagrant/web__public_web but the document root is /srv/www/... which contains a symlink pointing to the former path.
VirtualBox: Version 5.2.18 r124319 (Qt5.6.3)
Vagrant: v2.1.0
OSX: 10.13.6
What am I doing wrong?
Each mount requires a unique id, if the box is already built you'll need to rebuild it.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/path/where/mounted/on/vm", id: "unique_id_1", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.synced_folder "/home/vagrant/web__public_web", "/path/where/mounted/on/vm2", id: "unique_id_2", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
I answered my own question...
It turns out the sync was working all along. But I misunderstood the directionality of the sync. I was expecting the files in the guest OS to show up in the host directory, but it's the other way around. The empty folder on the host directory was getting synced to the guest and wiping out all the files.
After I manually copied all of the files from the guest to the host directory, the same configurations worked as expected.
Bottom line: if you want to sync an existing guest folder with files in it, you have to manually move it into place on the host OS first.

How to sync directory within Vagrant box (virtualbox provider) with local directory?

I have used the vagrantfile provided in this here tutorial and can't seem to get my local files synced to the VM. I tried mkdir vagrant_data and that didn't show them in there. What do I do?
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.synced_folder "./", "/vagrant_data"
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "2048"
end
I believe you may be giving the wrong file path of the host machine. Instead of giving the ./ relative path which could refer to anything. Try using the absolute path instead. Here if the Official tutorial on how to configure.

How to exclude (ignore) certain folders in vagrant rsync?

I wish to run npm install in my vagrant virtual box.
But whenever I ran the npm install command, my rsync will execute. Since my host computer does not have node_modules installed, it simply remove the folder completely for me.
What do I need to do so that my vagrant rsync will ignore the node_modules folder?
I cannot have node_modules being rsynced into the guest machine because my host and guest are two different systems. So far my vagrantfile looks like this.
Vagrant.configure("2") do |config|
config.vm.box = "laravel/homestead"
config.vm.hostname = "localhost"
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.synced_folder "./", "/home/vagrant/app"
config.vm.provider "virtualbox" do |v|
v.name = "web2"
v.memory = "4096"
v.cpus = 2
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME", "1"]
end
config.vm.synced_folder "./", "/home/vagrant/app", type: "rsync", rsync_auto: true, rsync_exclude: ['node_modules*']
config.vm.provision :shell, path: "provision/setup.sh"
end
And I execute vagrant rsync via
vagrant rsync-auto
rsync__exclude as well as rsync__auto takes 2 _
You need to rewrite your line as
config.vm.synced_folder "./", "/home/vagrant/app", type: "rsync", rsync__auto: true, rsync__exclude: ['./node_modules*']
Step 3 on http://perrymitchell.net/article/npm-symlinks-through-vagrant-windows/ worked better for me in the end...
From the page:
The steps are simple:
Delete the node_modules directory if it exists.
Create a directory called, say "node_modules_projectname" in the VM's home directory (~) (Some articles and posts recommend making the
directory in /tmp, but obviously this is cleared on reboot, so it may
not be an optimal experience for this type of thing).
Link a local node_modules dir from within the project's directory
ln -s ~/node_modules_projectname /path/to/your-project/node_modules
Install the packages in the project directory:
npm install

Setting vagrant home directory during provisioning

I have a VM that I'm pulling down with vagrant and using a VERY basic vagrant file
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "vagrant-rhel-devel"
config.vm.network :private_network, ip: "192.168.33.101"
config.vm.synced_folder ".", "/vagrant"
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
# vb.gui = true
# Enable 3d Rendering
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
# Sets 32megs video ram, higher number here = more POWERS
vb.customize ["modifyvm", :id, "--vram", "32"]
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
vb.name = "RedHat 3D"
end
end
My issue is that the vagrant user gets a home directory of /localhome/vagrant and I'd like him to have /home/vagrant as the home directory.
Is this something I'm able to change with provisioning or is it something that is set in the VM itself? I'm rather unskilled at the provisioning step at the moment so an example would be great.
The HOME directory has been set when the user has been created so this was done before the VM was packaged as vagrant box.
From there, you should be able to change that with
usermod -m -d /path/to/new/home/dir userNameHere
so in your case it would be
usermod -m -d /home/vagrant vagrant
check if you have existing files under the /localhome directory that would need to be copied into the new one (bash preferences file)
if you do not plan to destroy/create a bunch of new VM from this box, its not needed to add as provisioning; if you do plan to use this box and create lot of new VM, then it could make sense, you would just need to add an inline shell provision

config.vm.share_folder setting not found

I am getting this error with this line in my Vagrantfile during vagrant up, until I comment it out.
The setting is documented here:
http://docs-v1.vagrantup.com/v1/docs/config/vm/share_folder.html
Not sure why the following documented paramenter causes an error
config.vm.share_folder "puppetdir", "/etc/puppet", "/vagrant/mypuppetdir"
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:
vm:
* The following settings don't exist: share_folder
The latest virtualbox and latest vagrant. everything else works fine.
On Vagrant 1.1+ you should use config.vm.synced_folder, the docs you are looking at are for older versions. Please refer to the updated documentation for more info: http://docs.vagrantup.com/v2/synced-folders/basic_usage.html
Shared Folders has been renamed to Synded Folder since 1.1.
In your Vagrantfile you should be using the following
config.vm.synced_folder "../data", "/vagrant_data"
# by default enabled, uncomment to disable
# config.vm.synced_folder ".", "/vagrant", disabled: true
NOTE: By default, Vagrant will share your project directory (the directory where Vagrantfile resides) to /vagrant.
config.vm.synced_folder ".", "/vagrant", disabled: true
More flexible example
vagrant_data = File.expand_path("../vagrant_data", __FILE__)
Dir::mkdir(vagrant_data) unless FileTest::directory?(vagrant_data)
config.vm.synced_folder "vagrant_data", "/vagrant_data"
Take a look at this for more information => shared folders VS synced folders
Extending from https://github.com/mitchellh/vagrant/issues/936#issuecomment-7179034
if you need to mount a volume as a user that doesn't exist when the vm boots you can get there like so:
# Vagrantfile line
config.vm.synced_folder "host_folder", "/svr/fake_mount_folder", id: "whatever_name"
# ...
config.vm.provision "shell", inline: <<-SHELL
# ...
# In my case a package installed a user with UID 110, GID 116
mount -t vboxsf -o uid=110,gid=116 whatever_name /media/actual_mounted_folder
# ...
SHELL

Resources