解决Vagrant中VirtualBox Guest Additions版本不匹配问题

目录

本文的方法可以用来解决因如下原因

No guest additions were detected on the base box for this VM!

导致无法挂载共享目录的问题。
最新版的 vagrant-vbguest 不需要复杂的配置,只需要安装后重新 vagrant up 即可。

>vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.13.0)'!

重新启动后,会自动下载 VBoxGuestAdditions。

vagrant reload
...省略...
==> nwpc-log-reporter: Machine booted and ready!
[nwpc-log-reporter] No installation found.
...省略...
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 5.1.4 - guest version is unknown
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.4 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.0.26 of VirtualBox Guest Additions...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Building Guest Additions kernel modules.
vboxadd.sh: Starting the VirtualBox Guest Additions.
Could not find the X.Org or XFree86 Window System, skipping.
==> nwpc-log-reporter: Checking for guest additions in VM...
...省略...

原文如下:

  • * * 

重启Vagrant后,发现无法挂载目录,显示:

Failed to mount folders in Linux guest.

使用如下命令重新安装也不不好使

sudo /etc/init.d/vboxadd setup

还好,找到一个vagrant插件,专门用来更新VirtualBox的Guest插件 —— vagrant-vbguest
安装时又遇到两个坑。该插件使用ruby编写,需要ruby的两个库——json和rdoc。需要先使用gem安装这两个库,才能继续安装该插件。

gem install json -v '1.8.2'
gem install rdoc -v '4.2.0'

安装vagrant-vbguest插件

vagrant plugin install vagrant-vbguest

经过漫长的等待,安装成功。

Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.10.0)'!

修改Vagrantfile,添加vbguest插件的设置项:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # ...
  # we will try to autodetect this path.
  # However, if we cannot or you have a special one you may pass it like:
  # config.vbguest.iso_path = "#{ENV['HOME']}/Downloads/VBoxGuestAdditions.iso"
  # or
  # config.vbguest.iso_path = "//company.server/VirtualBox/%{version}/VBoxGuestAdditions.iso"
  # set auto_update to false, if you do NOT want to check the correct
  # additions version when booting this machine
  config.vbguest.auto_update = true
  # do NOT download the iso file from a webserver
  config.vbguest.no_remote = false
  # ...
end

第一次使用时,如上设置,更新后就可以将其设为false和true,不用再检查。
更新vboxguest插件同样是一个漫长的过程,下载速度极慢,更新vboxguest后,就可以正常挂载目录了。