Vagrant错误:在Linux客户机中无法挂载文件夹

284

我在使用Vagrant共享文件夹时遇到了一些问题,我的基本系统是Ubuntu 13.10桌面版。

我不明白为什么会出现这个错误,是配置不正确吗?是NFS问题还是Virtualbox Guest Additions的问题?我已经尝试过很多不同的boxes,但是问题仍然存在。

Failed to mount folders in Linux guest. This is usually because
    the "vboxsf" file system is not available. Please verify that
    the guest additions are properly installed in the guest and
    can work properly. The command attempted was:

    mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` /vagrant /vagrant
    mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` /vagrant /vagrant

以下是后的完整过程:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'u131032'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1396020504136_46442
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Error: Connection timeout. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
GuestAdditions versions on your host (4.3.10) and guest (4.2.16) do not match.
 * Stopping VirtualBox Additions
   ...done.
Reading package lists...
Building dependency tree...
Reading state information...
The following packages were automatically installed and are no longer required:
  dkms libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libfontenc1
  libgl1-mesa-dri libglapi-mesa libice6 libllvm3.3 libpciaccess0 libpixman-1-0
  libsm6 libtxc-dxtn-s2tc0 libxaw7 libxcomposite1 libxdamage1 libxfixes3
  libxfont1 libxkbfile1 libxmu6 libxpm4 libxrandr2 libxrender1 libxt6
  x11-common x11-xkb-utils xfonts-base xfonts-encodings xfonts-utils
  xserver-common xserver-xorg-core
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  virtualbox-guest-dkms* virtualbox-guest-utils* virtualbox-guest-x11*
0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded.
After this operation, 11.1 MB disk space will be freed.
(Reading database ... 65615 files and directories currently installed.)
Removing virtualbox-guest-dkms ...

-------- Uninstall Beginning --------
Module:  virtualbox-guest
Version: 4.2.16
Kernel:  3.11.0-18-generic (i686)
-------------------------------------

Status: Before uninstall, this module version was ACTIVE on this kernel.

vboxguest.ko:
 - Uninstallation
   - Deleting from: /lib/modules/3.11.0-18-generic/updates/dkms/
 - Original module
   - No original module was found for this module on this kernel.
   - Use the dkms install command to reinstall any previous module version.


vboxsf.ko:
 - Uninstallation
   - Deleting from: /lib/modules/3.11.0-18-generic/updates/dkms/
 - Original module
   - No original module was found for this module on this kernel.
   - Use the dkms install command to reinstall any previous module version.


vboxvideo.ko:
 - Uninstallation
   - Deleting from: /lib/modules/3.11.0-18-generic/updates/dkms/
 - Original module
   - No original module was found for this module on this kernel.
   - Use the dkms install command to reinstall any previous module version.

depmod....

DKMS: uninstall completed.

------------------------------
Deleting module version: 4.2.16
completely from the DKMS tree.
------------------------------
Done.
Removing virtualbox-guest-x11 ...
Purging configuration files for virtualbox-guest-x11 ...
Removing virtualbox-guest-utils ...
Purging configuration files for virtualbox-guest-utils ...
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Reading package lists...
Building dependency tree...
Reading state information...
dkms is already the newest version.
dkms set to manually installed.
linux-headers-3.11.0-18-generic is already the newest version.
linux-headers-3.11.0-18-generic set to manually installed.
The following packages were automatically installed and are no longer required:
  libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libfontenc1 libgl1-mesa-dri
  libglapi-mesa libice6 libllvm3.3 libpciaccess0 libpixman-1-0 libsm6
  libtxc-dxtn-s2tc0 libxaw7 libxcomposite1 libxdamage1 libxfixes3 libxfont1
  libxkbfile1 libxmu6 libxpm4 libxrandr2 libxrender1 libxt6 x11-common
  x11-xkb-utils xfonts-base xfonts-encodings xfonts-utils xserver-common
  xserver-xorg-core
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Copy iso file /usr/share/virtualbox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
mount: block device /tmp/VBoxGuestAdditions.iso is write-protected, mounting read-only
Installing Virtualbox Guest Additions 4.3.10 - guest version is 4.2.16
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.10 Guest Additions for Linux............
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Building the VirtualBox Guest Additions kernel modules ...done.
Doing non-kernel setup of the Guest Additions ...done.
Starting the VirtualBox Guest Additions ...done.
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.
An error occurred during installation of VirtualBox Guest Additions 4.3.10. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
==> default: Checking for guest additions in VM...
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Exporting NFS shared folders...
==> default: Preparing to edit /etc/exports. Administrator privileges will be required...
nfsd running
sudo: /usr/bin/exportfs: command not found
==> default: Mounting NFS shared folders...
==> default: Mounting shared folders...
    default: /vagrant => /home/me/Documents/Work/project/vagrant
Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` /vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` /vagrant /vagrant

我的 Vagrantfile 配置如下:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

personalization = File.expand_path("../Personalization", __FILE__)
load personalization

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = $base_box
  config.vm.box_url = $base_box_url

  config.vm.hostname = $vhost + ".dev"

  config.hostsupdater.aliases = ["api." + $vhost + ".dev", "mysql." + $vhost + ".dev"]
  config.hostsupdater.remove_on_suspend = true

  # set auto_update to ture to check the correct 
  # additions version when booting the machine
  config.vbguest.auto_update = true
  config.vbguest.auto_reboot = true

  config.vm.network :private_network, ip: $ip

  config.vm.synced_folder "../", "/srv/www/vhosts/" + $vhost + ".dev", type: "nfs"

  config.vm.provider :virtualbox do |v|
    v.customize ["modifyvm", :id, "--memory", 2048]
    v.customize ["modifyvm", :id, "--cpus", "1"]
    v.customize ["modifyvm", :id, "--cpuexecutioncap", "100"]
    v.customize ["modifyvm", :id, "--ioapic", "off"]
    v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  end

  config.vm.provision "shell" do |s|
    s.path = "vagrant-bootstrap.sh"
    s.args = $vhost + " " + $mysql_password + " " + $application_database
  end
end

个性化文件是:

# Name of the vhost to create
$vhost = "project"

# Use the Ubunut 32bit or 64bit
$base_box_url = "http://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-i386-vagrant-disk1.box"

# VM IP
$ip = "192.168.7.7"

# Base box name
$base_box = "u131032"

# MySQL
$mysql_password = "admin"
$application_database = "project"

以下插件已在Vagrant中启用:
$ vagrant plugin list
vagrant-hostsupdater (0.0.11)
vagrant-login (1.0.1, system)
vagrant-share (1.0.1, system)
vagrant-vbguest (0.10.0)

这个问题也发生在我身上,是在我更新VirtualBox 5.1.20之后。每次更新我的虚拟机时都会让我感到担忧,因为这不是第一次了 :( - GusDeCooL
@GusDeCooL 你可以在 Vagrantfile 中使用 config.vbguest.auto_update = false 关闭客户机附加组件的自动更新。 - Henrik
22个回答

360

这个插件 vagrant-vbguest GitHub RubyGems 解决了我的问题:

$ vagrant plugin install vagrant-vbguest

输出:

$ vagrant reload
==> default: Attempting graceful shutdown of VM...
...
==> default: Machine booted and ready!
GuestAdditions 4.3.12 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Exporting NFS shared folders...
==> default: Preparing to edit /etc/exports. Administrator privileges will be required...
==> default: Mounting NFS shared folders...
==> default: VM already provisioned. Run `vagrant provision` or use `--provision` to force it

请确保您正在运行最新版本的VirtualBox


5
这是唯一解决我的方法。在我这样做之后,虚拟机增强功能更新了,并且与我的主机匹配。所有的一切都没有问题。我只需要保持客户机和主机版本相同即可。感谢@karlingen的指导。 - Rixhers Ajazi
1
安装插件后,我开始遇到这个问题。卸载它并不能解决这个问题。 - Yamiko
@Yamiko请确保您正在运行Virtualbox的最新版本。 - karlingen
14
如果有这样一行话,我本可以节省大约20个小时的搜索、重新配置等操作……那你的“请给我买杯啤酒”的按钮在哪里呢? - Yordan Georgiev
1
注意,如果你让它 config.vbguest.auto_update = true(默认值),这个插件可能会引起一个几乎相同的问题。https://github.com/dotless-de/vagrant-vbguest/issues/141 - KCD
显示剩余15条评论

231

我在这里找到了解决方案:vagrant issues。 有两种方法:

  1. 在访客机上运行此命令(即在使用 vagrant ssh 登录vbox之后)

sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions

然后运行vagrant reload以正确挂载文件夹。

  • 如@klang所指出的,更新你的Mac上的VBoxGuestAdditions.iso文件:

  • wget https://www.virtualbox.org/download/testcase/VBoxGuestAdditions_4.3.11-93070.iso‌​
    sudo cp VBoxGuestAdditions_4.3.11-93070.iso /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso
    

    更新 (2014年5月16日)

    由于ISO不再可用,您可以使用4.3.12版本的ISO (http://dlc.sun.com.edgesuite.net/virtualbox/4.3.12/VBoxGuestAdditions_4.3.12.iso)

    注意:此时不提供适用于OS X的vbox4.3.12二进制文件


    5
    您可能只需要在此之后运行 vagrant reload 命令,以重新挂载共享文件夹。 - d4nt
    3
    有没有办法将这个操作添加到 VagrantFile 中,以便在挂载驱动器之前执行? - Jason Austin
    3
    在此之后,您可以像往常一样继续操作:wget https://www.virtualbox.org/download/testcase/VBoxGuestAdditions_4.3.11-93070.iso; sudo cp VBoxGuestAdditions_4.3.11-93070.iso /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso。请注意,这是对GitHub上一个问题的回复。 - klang
    5
    使用装有Vagrant 1.3.5和VirtualBox 4.3.10的Win 7系统,答案#1对我不起作用。我仍然遇到了“无法挂载...”的错误提示。 - Kevin Meredith
    3
    建议的修复方案是选项一,就像这里最初发布的那样。选项二中ISO文件的URL已经失效并显示404错误。 - colinhoernig
    显示剩余13条评论

    49

    我在寻找同样的错误信息时来到了这个页面。 对我来说原因是不同的:我在系统上运行了yum update,安装了一个新的内核。客户附加组件过时了,所以无法加载它们。

    我用以下命令重新构建了它们:

    sudo /etc/init.d/vboxadd setup 
    

    经过 vagrant reload 之后,我的客户端又可以正常运行了。

    我在这里添加这段内容,以防其他人出现与我相同的问题。

    编辑(根据 KCD 的评论):
    你可能会遇到以下错误:

    当前正在运行的内核的标题未找到

    可以通过安装 kernel-devel 来解决此问题(yum install kernel-devel)。


    3
    谢谢。我忘记了我运行了 yum update。我留下这个评论,以便将来的奥斯汀在再次发生这种情况时能够找到它。 - austin
    1
    我在Ubuntu中运行了apt-get update,并留下了一条注释,以便将来再次遇到此问题时进行参考 :-) - skift
    现在和未来,Loïc感谢你 :)(我忘记我也运行了yum update - loïc m.
    它可能会失败并显示“未找到当前运行内核的头文件”,并要求您执行“yum install kernel-devel”命令,这将起作用。 - KCD
    我运行“yum update”时遇到了同样的问题。现在我用你的命令解决了它。谢谢。 - grant sun
    1
    我还需要安装gcc才能使其正常工作。总结一下:1)yum install kernel-devel-$(uname -r)和2)yum install gcc;最后,退出VM并执行vagrant reload。这是在Centos 7 Vagrant box(bento/centos-7.1)上完成的。 - JDS

    29

    逐步修复:

    如果您没有安装 vbguest 插件,请先安装:

    $ vagrant plugin install vagrant-vbguest
    

    运行 Vagrant。

    它显示了一个错误。

    $ vagrant up
    

    登录虚拟机

    $ vagrant ssh
    

    修复!

    在来宾虚拟机中(VM已登录)。

    $ sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
    

    回到主机,在Vagrant中重新加载

    $ vagrant reload
    

    我认为这是最好的、最透明的解决方案。 - Mateusz Odelga
    这个方法可行,但是在Fedora 22中,我必须通过vagrant ssh进入虚拟机并执行sudo ln -s /etc/dnf/dnf.conf /etc/yum.conf命令,因为Fedora已经从yum切换到了dnf。有了这个操作,一切都运行完毕(我想是这样的!)。感谢@dadaso。 - J.Z.

    14

    2016年2月更新

    我独立解决这个问题花了好几个小时。是的,最新的Vagrant和Virtual Box安装依然存在这个问题:

    △  vagrant -v
    Vagrant 1.8.1
    △  vboxmanage -v
    5.0.14r105127
    

    对我而言,症状是类似于以下消息:

    检查虚拟机中的客户附加组件...此虚拟机中的客户附加组件与VirtualBox安装的版本不匹配!

    随后,挂载NFS驱动器失败。

    1). 安装vagrant-vbguest插件。

    根据您使用的Vagrant版本,发出以下命令之一:

    # For vagrant < 1.1.5
    $ vagrant gem install vagrant-vbguest
    
    # For vagrant 1.1.5+
    $ vagrant plugin install vagrant-vbguest
    

    接下来,执行vagrant halt,然后执行vagrant up - 可能仍然存在问题。

    2). ssh进入你的虚拟机并设置一个软链接到正确版本的Guest Additions(这里是5.0.14)。

    $ vagrant ssh
    
    $ sudo ln -s /opt/VBoxGuestAdditions-5.0.14/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
    $ exit
    
    $ vagrant reload
    

    你应该没问题了。默认情况下,宿主机上的挂载驱动器位于/vagrant

    最后的注释:

    如果您仍然遇到与挂载NFS驱动器相关的问题,则有一个解决方法适用于我。 我有一个包含类似配置的vagrantfile

    只需删除挂载类型信息,并简化mount_options设置,使其在全局范围内工作。 Vagrant现在将自动为您的环境选择最佳的同步文件夹选项。


    2
    谢谢你的回复...不过你的vagrantfile配置文件在最后一段之前缺失了,有可能加回去吗? - Phil Gyford
    这对我仍然不起作用。我下载了一个新的Vagrant。vboxmanage已经显示了上面的版本。OP建议的软链接已经存在。 - Tony Ennis
    1
    你的Vagrantfile配置文件在哪里?它在你的回答中找不到。 - Aboelseoud

    12

    运行以下命令安装vagrant-vbguest插件:

    vagrant plugin install vagrant-vbguest
    

    6

    我在使用Centos 7时遇到了同样的问题,我认为这是由于过时的内核与更新版本的VirtualBox组合而导致。根据Blizz的更新,以下是对我有效的解决方法(已安装vagrant-vbguest插件):

    vagrant ssh
    sudo yum -y install kernel-devel
    sudo yum -y update
    exit
    vagrant reload --provision
    

    5
    对于我来说,使用VBoxGuestAdditions 5.1.20时,问题在于/sbin/mount.vboxsf指向了错误的位置。
    使用以下命令可以解决问题:
    sudo ln -sf /opt/VBoxGuestAdditions-5.1.20/lib/VBoxGuestAdditions/mount.vboxsf /sbin/mount.vboxsf
    这对我很有用。

    我不是Vagrant的用户,但我在运行在OSX主机上的Fedora Core客户端上遇到了同样的问题。我认为问题在于mount.vboxsf的位置已经改变,但安装程序仍然在符号链接到旧位置。 - Teddy
    这是客户附加组件5.1.20中已确认的错误:https://www.virtualbox.org/ticket/16670 - Max Smolens

    4

    自从他们下架了下载链接,我正在做的就是找回这个下载链接。 - Pat K
    1
    请注意,此错误已在VirtualBox 4.3.12中修复。 - bbrame
    5
    我在VirtualBox 4.3.26中遇到了完全相同的错误。 - terbooter
    4
    对我来说也是4.3.28版本,遇到了同样的问题。 - Adam Parkin
    请为我提供5.0.14r105127版本,并解决同样的问题。 - Philip Pryde

    3

    我相信这是目前最新的答案,并且对我有效(Guest Additions版本:5.0.6VirtualBox版本:4.3.16Ubuntu 14.04 LTS)。

    https://github.com/mitchellh/vagrant/issues/3341#issuecomment-144271026

    基本上它说:
    Simple and Quick Solution for Failed to mount folders in Linux guest issue.
    
    Add the following line to your Homestead/Vagrantfile:
    
    config.vbguest.auto_update = false
    Your Homestead/Vagrantfile should looks like this:
    
    /...
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    
       # To avoid install and uninstall VBoxGuessAdditions during vagrant provisioning.
        config.vbguest.auto_update = false
    
    .../
    Save it and execute
    
    $ vagrant destroy --force
    $ vagrant up
    

    3
    需要注意的是,执行'vagrant destroy'命令会彻底删除你的虚拟机及其中配置和安装的任何内容,除非我理解有误。 - CargoMeister
    这对我来说是唯一且最快的解决方案。在从这里下载的“Ubuntu precise 64 VirtualBox”镜像上遇到了问题。使用Windows上的VirtualBox 5.1.2和Vagrant 1.8.5。 - Jamin

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接