How I can get /vagrant folder on ubuntu/xenial64

2020-07-05 15:34发布

问题:

I have just installed vagrant box with command:

vagrant init ubuntu/xenial64

and the booted it up with:

vagrant up --provider virtualbox

after ssh-ing I can not find folder /vagrant

What I am doing wrong?

According to documentation there should be /vagrant folder:

By default, Vagrant will share your project directory (the directory with the Vagrantfile) to /vagrant.

Here is a screenshot where is obvious that there is no /vagrant folder

My host environment is:

  • windows 7
  • Vagrant version 1.8.1
  • Vbox version 5.0.20r106931

Here is my Vagrant file:

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|

  config.vm.box = "ubuntu/xenial64"

  config.vm.provision "shell", path: "provision.sh"

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true

    # Customize the amount of memory on the VM:
    vb.memory = "4096"
    vb.cpus = "2"

    # Hack for nom global install
    # https://github.com/npm/npm/issues/7308
    vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/vagrant", "1"]

  end
end

and here is my provisioning file:

echo "------------------------------"
echo "--- Updating packages list ---"
echo "------------------------------"
apt-get -y update

echo "-----------------------"
echo "--- Installing curl ---"
echo "-----------------------"
apt-get -y install curl

echo "----------------------"
echo "--- Installing git ---"
echo "----------------------"
apt-get -y install git

echo "-----------------------------"
echo "--- Installing python-pip ---"
echo "-----------------------------"
apt-get -y install python-pip
yes | pip install --upgrade pip

echo "--------------------------"
echo "--- Installing node.js ---"
echo "--------------------------"
apt-get -y install  nodejs

echo "----------------------"
echo "--- Installing npm ---"
echo "----------------------"
apt-get -y install npm

回答1:

Update:

This problem was finally fixed in version v20160921.0.0 and higher of the official 16.04 vagrant box. The release before that v20160909.0.0 is also fixed but has another unrelated issue, as pointed out by Poma in the comments.

You can use the following command to download the new version and also replace your old VM instance with a fresh one. However, upgrading the box wipes your original VM instance.

vagrant box update

If you don't want to wipe your VM for some reason, you can still apply my original fixes below.

Original Post:

Apparently the official ubuntu/xenial64 image is broken, as indicated in this bug report on launchpad.

Here's a quote from the comments by Louis Zuckerman with all the issues which are still unfixed as of version 20160627.0.0 from what I could tell:

There are a number of issues with the official vagrant box for xenial:

  1. necessary packages are missing (synced folders not working, no config management)
  2. default /vagrant synced folder is disabled
  3. vm name is static, so you can only have one instance of the box on a host

[...]

These steps fixed issues 1 and 2 for me:

  1. ssh into the box and install the missing guest additions manually

    vagrant ssh
    sudo apt-get install virtualbox-guest-utils
    exit 
    
  2. manually add the missing mountpoint to your Vagrantfile

    config.vm.synced_folder ".", "/vagrant/" # fix broken ubuntu/xenial64 image
    

To fix the third issue you have to give a unique name to the box manually by adding a provider-specific option for VirtualBox to your Vagrantfile.

config.vm.provider "virtualbox" do |vb|
  vb.name = "This_Name_Is_Unique_For_This_Machine"
end

There is a more detailed discussion of this issue here as pointed out in the comments to this answer.



回答2:

Is there a reason people are not using bento/ubuntu-16.04 ?

See this response: https://github.com/mitchellh/vagrant/issues/7155

the boxes published by canonical under the ubuntu namespace for 16.04 are very broken. They do not follow the recommended guides for how to build a Vagrant box, and they are missing key components such as guest additions, required packages, or they do things like hardcode the hostname to make running concurrent VMs impossible. ... In general, users have had more success with the boxes under the bento namespaces. A common misconception is that a namespace like "ubuntu" represents the canonical space for Ubuntu boxes. This is untrue.

There seems to be many reasons NOT to use the ubuntu/xenial64 boxes and just use a working box from the bento namespace instead of trying to hack the ubuntu namespace boxes to work. The Ubuntu brand may be more comfortable for some people to see, but I think it's broken with Vagrant.



回答3:

For some reason ubuntu/xenial64 doesn't come with a synced /vagrant folder.

I had to setup my own in the vagrant file:

config.vm.synced_folder ".", "/vagrant"

The first option is the source (Windows) the second is the target (Ubuntu)

Vagrant Synced Folders



回答4:

Vagrant by default mounts a /vagrant folder on the guest machine that is synced with the folder where the Vagrantfile is.

There are some reasons why vagrant couldn't mount your shared folder, the most common is that you don't have the virtualbox drivers correctly installed and initialized.That happens when you don't have your kernel headers on the same version of the virtual box packages.

First make sure you don't have this line on your Vagrantfile because this will disable the synced folder:

config.vm.synced_folder ".", "/vagrant", disabled: true

If you don't have it, it's probably a error with the virtualbox modules on your system, and i would recommend you to check the drivers doing the following steps.

Destroy your running virtual machine:

vagrant destroy -f

You can check your kernel version using:

uname -r

After that install the headers for your kernel:

sudo apt-get install linux-headers-'uname-r'

Then, install the virtualbox packages for your kernel:

sudo apt-get install virtualbox virtualbox-dkms virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-additions-iso

Load the virtualbox drivers:

sudo modprobe vboxdrv

Now you can try to mount the virtual machine with vagrant again:

vagrant up

Remember that you can reconfigure your vm to have as many shared folders that you want, you can do that with:

config.vm.synced_folder "src/", "/srv/website"

The first argument is the path to the folder on the host machine, the second one is where the folder will be present on the guest machine.

Hope this helps you.



回答5:

You could try installing the vbguest addition plugin, and see if that helps.

$ vagrant plugin install vagrant-vbguest

vb-guest plugin repo

Also, sometimes when you install the vbox on your computer if you dont give permissions to the additional drivers that it wants to install it will not function properly. You might need to re install to make sure that you accept the prompt when asking to install some drivers.

In case none of this works, you might try to boot withouth a gui, and paste here the output when trying to start the vagrant machine.