Vagrantのマウントエラーでわかるバージョン不一致の解決方法

###はじめに
VirtualBox＋Vagrantで仮想環境を立ち上げる前に通知が来ていたので
VirtualBoxのバージョンアップして`Vagarant up`したらエラーが出てしまった∑（;￣□￣Ａ 
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/524511/866832d0-cb0c-996a-ce5a-895d03abadf8.png)

###環境
・Mac OS X
・Virtualbox 6.1
・Vagrant 2.2.9
・CentOS 8.2
###エラー内容

````
hide@mbp MyCentos % vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-8.2' version '202007.17.0' is up to date...
==> 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 (guest) => 2222 (host) (adapter 1)
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/hide/Myvagrant/MyCentos

*ここからがエラー内容
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant

The error output from the command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device
````

`どうやらこのエラーは
ホスト⇔ゲスト間のフォルダ共有機能を提供しているGuest Additionsが
ホスト(VirtualBox)だけバージョンアップしたことでゲスト(Guest Additions)との
バージョンが不一致したことでマウントエラーが起きたようです`

「いや、両方自動でアップデートしてくれよ....(心の声)」

なので、ゲストのバージョンを上げてあげれば、解決できます


###解決方法
ホスト(VirtualBox)をバージョンアップして`vagrant up`をした時に
毎回、ホスト(VirtualBox)のバージョンを確認して
ゲスト(Guest Additions)のバージョンも自動的に上げてくれるプラグインをインストールします
そして、最後に再起動してください

````
hide@mbp MyCentos % vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Fetching micromachine-3.0.0.gem
Fetching vagrant-vbguest-0.24.0.gem
Installed the plugin 'vagrant-vbguest (0.24.0)'!

hide@mbp MyCentos % vagrant reload
````



よし!!!!!!終わった!!!!!!!!と思ったら........再起動後にさらなるエラーが.............



````
hide@mbp MyCentos % vagrant reload
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-8.2' version '202007.17.0' is up to date...
==> 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 (guest) => 2222 (host) (adapter 1)
==> 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: Machine booted and ready!
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims:   
VBoxService inside the vm claims: 6.1.12
Going on, assuming VBoxService is correct...
[default] GuestAdditions seems to be installed (6.1.12) correctly, but not running.
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims:   
VBoxService inside the vm claims: 6.1.12
Going on, assuming VBoxService is correct...
Redirecting to /bin/systemctl start vboxadd.service
Job for vboxadd.service failed because the control process exited with error code.
See "systemctl status vboxadd.service" and "journalctl -xe" for details.
Redirecting to /bin/systemctl start vboxadd-service.service
Job for vboxadd-service.service failed because the control process exited with error code.
See "systemctl status vboxadd-service.service" and "journalctl -xe" for details.
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims:   
VBoxService inside the vm claims: 6.1.12
Going on, assuming VBoxService is correct...
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel 
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all
VirtualBox Guest Additions: Kernel headers not found for target kernel 
4.18.0-193.14.2.el8_2.x86_64. Please install them and execute
  /sbin/rcvboxadd setup
ValueError: File context for /opt/VBoxGuestAdditions-6.1.12/other/mount.vboxsf already defined
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.

*ここからがエラー内容
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

/usr/sbin/rcvboxadd setup

Stdout from the command:

VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel 
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all
VirtualBox Guest Additions: Kernel headers not found for target kernel 
4.18.0-193.14.2.el8_2.x86_64. Please install them and execute
  /sbin/rcvboxadd setup


Stderr from the command:

ValueError: File context for /opt/VBoxGuestAdditions-6.1.12/other/mount.vboxsf already defined
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.

hide@mbp MyCentos % cat /var/l
````

`どうやらこのエラーは
起動中の kernel と kernel のヘッダー(kernel-headers)とディベロッパー(kernel-devel)のバージョンが異なることによって発生するらしい...(´；ω；｀)`


ということでカーネルさんをアップデート！！！！！！！

````
[vagrant@localhost ~]$ sudo yum -y update kernel && sudo yum -y install gcc kernel-devel kernel-headers make bzip2 perl
````


そして、もうエラーが起こらないことが願って、一度停止して起動！！！！！！

````
hide@mbp MyCentos % vagrant halt
==> default: Attempting graceful shutdown of VM...
hide@mbp MyCentos % vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-8.2' version '202007.17.0' is up to date...
==> 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 (guest) => 2222 (host) (adapter 1)
==> 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: Machine booted and ready!
[default] GuestAdditions 6.1.12 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/hide/Myvagrant/MyCentos
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
hide@mbp MyCentos %
````


成功しました(ｳｯﾎ━━━━ヽ(*'∀`*)ﾉ━━━━ｲ



###最後に
今回エラーを解決することができてよかった...
実は、前にもこのエラーが起きてめんどくさくてDockerに逃げてしまってから
また、これに出くわしてしまったので逃げてしまったことに反省です....
みなさんもエラーから逃げた僕を反面教師にして、しっかり自己学習でもエラーを解決してくださいねｗ
読んでくださってありがとうございました！

#参考文献
[Vagrantでマウントエラーが発生したときの解消方法](https://qiita.com/chubura/items/4166585cf3f44e33271d)
[VirtualBox guest additionsがインストールできない場合(CentOS, Fedora)](https://mekou.com/linux-magazine/virtualbox-guest-additions%E3%81%8C%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84%E5%A0%B4%E5%90%88centos-fedora/)