Reload VM in Vagrant script - bash

Introduction
I'm using Vagrant and want to create a box that fits my needs. I'm currently building my provisioning script but I have a problem which would require me to relog into the box.
What I'm trying to achieve
I want to set my locales to the German language
What I'm doing
After logging into my vagrant box with vagrant ssh I'm running the following commands
sudo apt-get update
sudo cp /var/www/projectfantasy/www/vagrant_ressources/locale.gen /etc/
sudo locale-gen de_DE.UTF-8
These steps are done with the help of Debian's wiki. The last step is
To use the new settings with your programs, log out and back in.
And now I am where I need help. How would I relog while being in the vagrant provisioning script? When I don't relog I'm getting the following warnings when installing extra packages.
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = "de_DE.UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
I mean, these are only warnings so that shouldn't be a problem, right? But I don't want to have warnings and would like to know how I could fix this issue.
When I relog and try to install those packages again it works without any problems.

unfortunately, this is not so simple - if you issue a reboot command or shutdown -r now in your provisioning script the VM will restart (indeed) but then the provisioning will not continue.
fortunately, some people wrote plugin (https://github.com/exratione/vagrant-provision-reboot and https://github.com/aidanns/vagrant-reload) I experienced with the second and it works
make sure to install the plugin
$ vagrant plugin install vagrant-reload
in your vagrantfile, your provisioning will look like
config.vm.provision "shell", path: "vagrant_ressources/preparations.sh"
config.vm.provision :reload
config.vm.provision "shell", path: "vagrant_ressources/bootstrap.sh"
so preparations.sh will execute, then the VM will reload and bootstrap.sh will execute
in your preparations.sh, after you generate locale, make sure variables are set in the locale file :
sudo locale-gen
echo -e 'LANG=de_DE.UTF-8\nLC_ALL=de_DE.UTF-8' > /etc/default/locale
sudo timedatectl set-timezone Europe/Berlin
PS : one note on your usage of the shell provisioning. By default provisioning is run with root privilege so there is no need for all the sudo ; if you want to run the provisioning as vagrant user run it as config.vm.provision :shell, privileged: false ... and you will need the sudo

I couldn't reproduce your warning, but I find this will allow you to set LC_ALL:
sudo update-locale LC_ALL=de_DE.UTF-8
(from https://askubuntu.com/questions/114759/warning-setlocale-lc-all-cannot-change-locale, but works for debian jessie as well)

Related

The /home/vagrant directory is missing from Ubuntu 16.04 (but there is in the 14.04)

Hi Stackoverflow users,
I have a Vagrant installations with Ubuntu 16.04 but these lines cannot be run correctly, because the /home/vagrant directory is missing.
info "Create bash-alias 'app' for vagrant user"
echo 'alias app="cd /app"' | tee /home/vagrant/.bash_aliases
info "Enabling colorized prompt for guest console"
sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/" /home/vagrant/.bashrc
However I run with Ubuntu 14.04 this shell code, and there were not any errors. It could run the above code correclty.
Do you have any idea where or how can I install the "home/vagrant" directory?
Your box ships a default Vagrantfile where you can see
config.vm.synced_folder '.', '/vagrant', disabled: true
you can check default Vagrantfile under the `.vagrant.d/boxes///Vagrantfile``
You can force the creation of the default folder by (re)adding
config.vm.synced_folder '.', '/vagrant'
in your own Vagrantfile

Create an apt-cache on host for multiple vagrant installations

When you startup a vagrant box using a bootstrap.sh, you have to download all packages with apt-get inside each box again.
How can I set up an external folder on my host, that is used for all boxes as cache?
Maybe somehow adding the apt-cache folder in the Vagrantfile lilke this?
config.vm.synced_folder "/var/cache/apt/archives/", "/var/cache/apt/archives/"
You cannot mount the same folder as on your host mashine, but you can create a special folder for your vagrant boxes.
Add these lines in my Vagrantfile:
# add the host apt cache so packages don't have to be downloaded each time
# you need to create this before you start vagrant up:
#sudo mkdir -p /var/tmp/vagrant/apt-archives/
#sudo mkdir -p /var/tmp/vagrant/apt-lists/
#sudo chmod 777 /var/tmp/vagrant/apt-archives/ /var/tmp/vagrant/apt-lists/
config.vm.synced_folder "/var/tmp/vagrant/apt-archives/", "/var/cache/apt/archives/", type: "nfs"
config.vm.synced_folder "/var/tmp/vagrant/apt-lists/", "/var/lib/apt/lists", type: "nfs"
you must create those folders before the first run of vagrant up yourself. Then everything works fine without having to download anything you already have again.
Note: I am not sure, but it could be, that this gets conflicts, if you use different distributions in different vagrant mashines

vagrant ash: sudo: not found

When starting my vagrant box with a small 15MB Busybox image, the first time I get an error during the phase
Mounting shared folders...
It seems vagrant is trying something with sudo, which isn't istalled. I get this error:
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
mkdir -p /vagrant
Stdout from the command:
Stderr from the command:
ash: sudo: not found
It works so far, I can login as root with the password vagrant, but I guess this is not perfect?
This is my setup: https://github.com/rubo77/ffnord-example/blob/pyddhcpd/Vagrantfile
config.ssh.username = 'root'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'
config.ssh.shell = 'ash'
(0..9).each do |i|
config.vm.define "gc-node0#{i}" do |node|
end
end
By default vagrant share/sync the directory /vagrant (guest) with the current project directory, the one containing the Vagrantfile (host), more directories can be shared using config.vm.synced_folder, I don't know if the default /vagrant can be disabled.
If you aren't using the shared directories feature then the missing sudo isn't a problem.
If your filesystem is writeable you can create the /vagrant directory once, so on next up vagrant shouldn't try to sudo mkdir again;
but assuming you're using VirtualBox (I don't know about others emulator behavior/architecture) you'll face another problem, shared directories require the kernel modules vboxsf and vboxguest (from VirtualBox Guest Additions) to be built (against the running kernel sources) and loaded (insmod), and the command mount.vboxsf to be built and installed in /sbin, plus your busybox require the option mount helpers enabled because vagrant use mount -t vboxsf and not mount.vboxsf directly, when vagrant try to mount the shared directories and those requirements aren't matched there is a long timeout before failing with error (a minute/two or more).
Notice that busybox (current stable) support sudo, is just disabled by default.
If you're building the buildroot yourself from sources I can tell that the shared feature works (I managed to get an image with busybox to work as vagrant expect), if you're using some pre-built kernel/busybox... may be a problem, you have to find matching binaries for vboxsf, vboxguest, mount.vboxsf and if the mount helpers option is disabled in your busybox you need to find a workaround (perhaps modifying vagrant to use mount.vboxsf).

VirtualBox: mount.vboxsf: mounting failed with the error: No such device [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I'm using VirtualBox with OS X as host and CentOS on the guest VM.
In OS X I created folder myfolder, added it as shared folder to the VM, turned on the VM, in CentOS created folder /home/user/myfolder and typing:
sudo mount -t vboxsf myfolder /home/user/myfolder
and have output:
/sbin/mount.vboxsf: mounting failed with the error: No such device
What I'm doing wrong?
UPDATED:
Guest Additions installed.
My shared folder/clipboard stopped to work for some reason (probably due to a patch installation on my virtual machine).
sudo mount -t vboxsf Shared_Folder ~/SF/
Gave following result:
VirtualBox: mount.vboxsf: mounting failed with the error: No such device
The solution for me was to stop vboxadd and do a setup after that:
cd /opt/VBoxGuestAdditions-*/init
sudo ./vboxadd setup
You're using share folders, so you need to install VirtualBox Guest Additions inside your virtual machine to support that feature.
Vagrant
If you're using Vagrant (OS X: brew cask install vagrant), run:
vagrant plugin install vagrant-vbguest
vagrant vbguest
In case it fails, check the logs, e.g.
vagrant ssh -c "cat /var/log/vboxadd-install.log"
Maybe you're just missing the kernel header files.
VM
Inside VM, you should install Guest Additions, kernel headers and start the service and double check if kernel extension is running.
This depends on the guest operating system, so here are brief steps:
Install kernel include headers (required by VBoxLinuxAdditions).
RHEL: sudo apt-get update && sudo apt-get install kernel-devel
CentOS: sudo yum update && sudo yum -y install kernel-headers kernel-devel
Install Guest Additions (this depends on the operating system).
Ubuntu: sudo apt-get -y install dkms build-essential linux-headers-$(uname -r) virtualbox-guest-additions-iso
If you can't find it, check by aptitude search virtualbox.
Debian: sudo apt-get -y install build-essential module-assistant virtualbox-ose-guest-utils
If you can't find it, check by dpkg -l | grep virtualbox.
manually by downloading the iso file inside VM (e.g. wget) and installing it, e.g.
wget http://download.virtualbox.org/virtualbox/5.0.16/VBoxGuestAdditions_5.0.16.iso -P /tmp
sudo mount -o loop /tmp/VBoxGuestAdditions_5.0.16.iso /mnt
sudo sh -x /mnt/VBoxLinuxAdditions.run # --keep
Extra debug: cd ~/install && sh -x ./install.sh /mnt/VBoxLinuxAdditions.run
Double check that kernel extensions are up and running:
sudo modprobe vboxsf
Start/restart the service:
manually: sudo /opt/VBoxGuestAdditions*/init/vboxadd setup (add sudo sh -x to debug)
Debian: sudo /etc/init.d/vboxadd-service start
Fedora: sudo /etc/init.d/vboxdrv setup
CentOS: sudo service VBoxService start
Building the main Guest Additions module
If above didn't work, here are more sophisticated steps to fix it. This assumes that you've already VBoxGuestAdditions installed (as shown above).
The most common reason why mounting shared folder doesn't work may related to building Guest Additions module which failed. If in /var/log/vboxadd-install.log you've the following error:
The headers for the current running kernel were not found.
this means either you didn't install kernel sources, or they cannot be found.
If you installed them already as instructed above, run this command:
$ sudo sh -x /opt/VBoxGuestAdditions-5.0.16/init/vboxadd setup 2>&1 | grep KERN
+ KERN_VER=2.6.32-573.18.1.el6.x86_64
+ KERN_DIR=/lib/modules/2.6.32-573.18.1.el6.x86_64/build
So basically vboxadd script is expecting your kernel sources to be available at the following dir:
ls -la /lib/modules/$(uname -r)/build
Check if the kernel dir exists (symbolic link points to the existing folder). If it's not, please install them to the right folder (e.g. in /usr/src/kernels).
So vboxadd script can enter your kernel source directory and run make kernelrelease, get the value and compare with your current kernel version.
Here is the logic:
KERN_VER=`uname -r`
KERN_DIR="/lib/modules/$KERN_VER/build"
if [ -d "$KERN_DIR" ]; then
KERN_REL=`make -sC $KERN_DIR --no-print-directory kernelrelease 2>/dev/null || true`
if [ -z "$KERN_REL" -o "x$KERN_REL" = "x$KERN_VER" ]; then
return 0
fi
fi
If the kernel version doesn't match with the sources, maybe you've to upgrade your Linux kernel (in case the sources are newer than your kernel).
CentOS
Try:
vagrant plugin install vagrant-vbguest vagrant vbgues
If won't work, try the following manual steps for CentOS:
$ sudo yum update
$ sudo yum install kernel-$(uname -r) kernel-devel kernel-headers # or: reinstall
$ rpm -qf /lib/modules/$(uname -r)/build
kernel-2.6.32-573.18.1.el6.x86_64
$ ls -la /lib/modules/$(uname -r)/build
$ sudo reboot # and re-login
$ sudo ln -sv /usr/src/kernels/$(uname -r) /lib/modules/$(uname -r)/build
$ sudo /opt/VBoxGuestAdditions-*/init/vboxadd setup
I am able to resolved this by running below commmand
modprobe -a vboxguest vboxsf vboxvideo
In addition to #Mats answer, I'm adding some more info (it helped me on Debian 8).
My shared folder/clipboard stopped to work for some reason (probably due to a patch installation on my virtual machine).
sudo mount -t vboxsf Shared_Folder ~/SF/
Gave me following result:
VirtualBox: mount.vboxsf: mounting failed with the error: No such device
The solution for me was to stop vboxadd and do a setup after that:
cd /opt/VBoxGuestAdditions-*/init
sudo ./vboxadd setup
At this point, if you still get the following error:
No such device. The Guest Additions installation may have failed. The error has been logged in /var/log/vboxadd-install.log
You need to install linux headers:
apt-get install linux-headers-$(uname -r)
then you can install Guest Additions:
sh /media/cdrom/VBoxLinuxAdditions.run --nox11
and restart your Linux by:
reboot
then you will be able to mount your shared folder!
mount -t vboxsf Shared_Folder ~/SF/
More informations (in French), check this page.
This was the only solution what worked for me:
Install Vagrant plugin: vagrant-vbguest, which can keep your VirtualBox Guest Additions up to date.
vagrant plugin install vagrant-vbguest
Source: https://github.com/aidanns/vagrant-reload/issues/4#issuecomment-230134083
This was resolved by:
yum install gcc kernel-devel make
workaround is here: https://gist.github.com/larsar/1687725
Shared folder was earlier working for me but all f sudden it stopped working (Virualbox - host was Windows 7, Guest was OpenSuSe)
modprobe -a vboxguest vboxsf vboxvideo
then
mount -t vboxsf testsf /opt/tsf (testsf was the folder in Windows C drive which was added in Virtualbox shared folder --- and /opt/tsf is the folder in OpenSuse
My host is Windows10 my VM guest is ubuntu build by vagrant. This worked for me:
vagrant plugin install vagrant-winnfsd
The solution for me was to update guest additions
(click Devices -> Insert Guest Additions CD image)
I also had a working system that suddenly stopped working with the described error.
After furtling around in my /lib/modules it would appear that the vboxvfs module is no more. Instead modprobe vboxsf was the required incantation to get things restarted.
Not sure when that change ocurred, but it caught me out.
I am running VirtualBox 5.1.20, and had a similar issue. Here is a url to where I found the fix, and the fix I implemented:
# https://dsin.wordpress.com/2016/08/17/ubuntu-wrong-fs-type-bad-option-bad-superblock/
if [ "5.1.20" == "${VBOXVER}" ]; then
rm /sbin/mount.vboxsf
ln -s /usr/lib/VBoxGuestAdditions/mount.vboxsf /sbin/mount.vboxsf
fi
The link had something similar to /usr/lib/VBoxGuestAdditions/other/mount.vboxsf, rather than what I have in the script excerpt.
For a build script I use in vagrant for the additions:
https://github.com/rburkholder/vagrant/blob/master/scripts/additions.sh
Seems to be a fix at https://www.virtualbox.org/ticket/16670
For me, on a mac, it turned out I had an old VirtualBox image stored on my machine that didn't have metadata, so it wasn't being updated to the latest version.
That old image had an older version of the vbguest plugin installed in it, which the newer vbguest plugin on my machine couldn't work with.
So to fix it, I just removed the image that my Vagrant was based on, and then Vagrant downloaded the newer version and it worked fine.
# Remove an old version of the virtual box image that my vagrant was using
$ vagrant box remove centos/7
You can find out which boxes you have cached on your machine by running:
$ vagrant box list
I had also upgraded my vbguest plugin in my earlier attempts at getting this to work, using the following process, but I don't think this helped. FYI !
# Get rid of old plugins
vagrant plugin expunge
# Globally install the latest version of the vbguest plugin`
vagrant plugin install vagrant-vbguest
If you find bring the box fails on guest addtions, you can try doing the following to ensure the plugins install correctly. This downloads the latest based image for your system (for me CentOS), and may resolve the issue (it did for me!)
$ vagrant box update
There can be errors/incorrect approach in two scenarios. Check both of it and figure it out
SCENARIO 1 :
Once you are running the VBoxLinuxAdditions.run or VBoxSolarisAdditions.pkg or VBoxWindowsAdditions.exe , check if all the modules are getting installed properly.
1.1.a. In case of VBoxLinuxAdditions, if Building the VirtualBox Guest Additions kernel modules gets failed,
check the log file in /var/log/vboxadd-install.log . If the error is due to kernel version update your kernel and reboot the vm. In case of fedora,
1.1.b. yum update kernel*
1.1.c. reboot
1.2. If nothing gets failed, then all is fine. You are already having the expected kernel version
SCENARIO 2 :
If the VBoxGuestAdditions is installed (check for a folder /opt/VBoxGuestAdditions-* is present .... * represents version) you need to start it before mounting.
2.1. cd /opt/VBoxGuestAdditions-*/init && ./vboxadd start
You need to specify the user id and group id of your vm user as options to the mount command.
2.2.a. Getting uid and gid of a user:
id -u <'user'>
id -g <'user'>
2.2.b. Setting uid and gid in options of mount command:
mount -t vboxsf -o uid=x,gid=x shared_folder_name guest_folder
On Ubuntu this worked:
sudo apt-get install build-essential linux-headers-`uname -r` dkms
Had the same issue with VirtualBox 5.0.16/rXXX
Installed latest VirtualBox 5.0.18 and installed latest Vagrant 1.9.3, issue went toodles.
I added as root user
/etc/rc.d/rc.local
/root/mount-vboxsf.sh
then
chmod +x /etc/rc.d/rc.local
and the sample script /root/mount-vboxsf.sh (set your own the uid and gid options)
modprobe -a vboxguest vboxsf vboxvideo
mount -t vboxsf NAME_SHARED_DIRECTORY /media/sf_NAME_SHARED_DIRECTORY -o rw,uid=0,gid=0
you need add
chmod + /root/mount-vboxsf.sh
I have similar issue, check header if it's not match then run below command
CentOS: sudo yum update && sudo yum -y install kernel-headers kernel-devel
If you're on Debian:
1) remove all installed package through Virtualbox Guest Additions ISO file:
sh /media/cdrom/VBoxLinuxAdditions.run uninstall
2) install Virtualbox packages:
apt-get install build-essential module-assistant virtualbox-guest-dkms virtualbox-guest-utils
Note that even with modprobe vboxsf returning nothing (so the module is correctly loaded), the vboxsf.so will call an executable named mount.vboxsf, which is provided by virtualbox-guest-utils. Ignoring this one will prevent you from understanding the real cause of the error.
strace mount /your-directory was a great help (No such file or directory on /sbin/mount.vboxsf).
An update did the trick for me !
$ vagrant box update
$ vagrant plugin install vagrant-vbguest
Below two commands works for me.
vagrant ssh
sudo mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant
Okay everyone is missing a basic fact.
mkdir /test - Makes sub directory in current directory.
sudo mkdir /test - Make directory in Root.
So if your shared directory name is shared and you do the following:
mkdir /test
sudo mount -t vboxsf shared /test
It generates this error:
sbin/mount.vboxsf: mounting failed with the error: No such file or directory
Because the directory is in the wrong place! Yes that's what this error is saying. The error is not saying reload the VBOX guest options.
But if you do this:
sudo mkdir ~/test
sudo mount -t vboxsf shared ~/test
Then it works fine.
It really amazes me how many people suggest reloading the Vbox guest additions to solve this error or writing a complex program to solve a directory created in the wrong place.

Puppet provisioner in Vagrant expects site.pp to be US-ASCII, fails with "invalid byte sequence"

I have a Vagrant box (Ubuntu 14.04 beta) that configures itself using the Puppet provisioner. After "vagrant up", the shell provisioner runs, installs updates (including language-pack-en), and sets the locale to en_US.UTF-8, e.g.:
export DEBIAN_FRONTEND=noninteractive
echo "LC_ALL=en_US.UTF-8" >> /etc/environment
apt-get update
apt-get -y install language-pack-en
export LC_ALL=en_US.UTF-8
wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb 2>&1
dpkg -i puppetlabs-release-trusty.deb
apt-get update
apt-get -y upgrade
apt-get -y -f remove puppet hiera ruby-hiera ruby-safe-yaml
apt-get -y autoremove
apt-get -y install puppet=3.4.3-1
The shell provisioner completes without an issue, however the puppet provisioner fails with this:
Error: Could not parse for environment production: invalid byte sequence in US-ASCII at /tmp/vagrant-puppet-1/manifests/site.pp:1
If I immediately run vagrant provision, the Puppet provisioner runs then without an issue, which makes me suspect it's not respecting the locale I'm setting in both /etc/environment, and exporting as an environment variable for good measure (which may not be necessary, this was a troubleshooting step).
I ran into this first with Ubuntu 13.10, but setting the environment variables resolved the issue - not so with 14.04.
Has anyone run into this before?
Thanks for any advice.
This is caused by the Puppet daemon not reading the correct locale, even though it's previously set by the shell provisioner.
While this could be mitigated by setting LANG=en_US.UTF-8 when the Puppet daemon runs, Vagrant currently doesn't support passing environment variables to Puppet at runtime. The issue is described here: https://github.com/mitchellh/vagrant/issues/2270.
The link above also provides a workaround. The workaround in the issue thread has to do with setting proxy servers as env. variables, but I modified it to set the LANG in a similar way:
Vagrantfile:
config.vm.provision :puppet do |puppet|
puppet.facter = {
# Specifying the LANG environment variable this way is a work around.
# The work around can be removed when this issue is fixed https://github.com/mitchellh/vagrant/issues/2270
"hack=hack LANG=en_US.UTF-8 hack" => "hack"
}
Adding this to Vagrantfile's Puppet provisioner sets the LANG correctly, and Puppet runs as expected on first boot.
When I remember correctly I faced this problem with puppet and the wrong format. It could be stored as dos format.
Else there something within you site.pp that seem wrong. Recreate this file via vim from the vagrant VM.
I hope this can help you.
Best regards.

Resources