【Homestead Master】系列是Laravel底层核心技术实战揭秘这一课程里的第一章,因为考虑到该章内容学员们可能要经常来回查阅,同时也为了提高课程更新速度,所以这一章以文字的形式发布在此。

有时候在你更新了homestead box了以后,当然也有的同学在一开始安装的时候也会遇到:

....
default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 5.1
.....

显示说Guest Additions和VirtualBox程序的版本不一致,有的时候这个不是个问题,甚至这个提示会在启动命令行的中间显示,你不仔细看都不会发现,但有的时候问题又确实是这个造成的,会导致主机和虚拟机之间文件共享有问题,从而可能说你访问站点出现502错误,或者ssh远程登陆进不去。

那么Guest Additions是一个什么鬼?为啥它还要和virtualbox程序版本一致呢?下面是Guest Additions的官方定义:

They consist of device drivers and system applications that optimize the guest operating system for better performance and usability.

也即是说,Guest Additions就是你虚拟机里核心系统安装好之后,额外需要安装的驱动及其他优化程序。所以版本不对应的问题经常出现在你更新了虚拟机里面的系统之后,比如说执行了vagrant box update以后,但是没有更新相应的virtualbox程序,那么相应的驱动什么的就容易跟不上了,就容易有潜在问题。

那么解决办法也就很明显,去更新一下virtualbox就好了,确实是这样。根据提示找个相应版本,或者干脆装最新的virtualbox,往往能解决问题。

但是这样也比较麻烦,因为很可能你下次更新了系统还得这么来一遍,可能是你更新了box,也可能是你虚拟机系统更新了内核,有没有什么办法让这个guest additions自动更新,或者自动对应呢?

其实vagrant有一个插件是专门来解决这个问题,下面是安装命令:

vagrant plugin install vagrant-vbguest

安装成功了以后,每次你执行vagrant up,它都会自动检查你的guest additions,如果发现过期了,就会自动更新,比如说这样:

[default] GuestAdditions versions on your host (4.2.10) and guest (4.2.6) do not match.
[default] Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
[default] Installing Virtualbox Guest Additions 4.2.10 - guest version is 4.2.6
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.2.10 Guest Additions for Linux..........
VirtualBox Guest Additions installer
Removing installed version 4.2.6 of VirtualBox Guest Additions...
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.
You should restart your guest to make sure the new modules are actually used

嗯,省心,值得。

类似的,有时候guest additions直接坏了,比如这样,直接说无法加载共享文件了:

vboxsf

既然是guest additions的问题,其实都可以直接安装刚才那个vagrant-vbguest插件来解决,让它给自动重新安装一下就好了。

嗯,这图是隔壁老王的,一直没来得及告诉他解决方法来着,算了,太忙了。