【解決方法】Vagrantで構築したUbuntu20.04へsynced_folder を設定した際に発生するマウントエラーの対処

VagrantVirtualBoxエラー解消
【解決方法】Vagrantで構築したUbuntu20.04へsynced_folder を設定した際に発生するマウントエラーの対処
Memo

Vagrantで構築したUbuntu20.04へsynced_folder を設定した際に発生するマウントエラーの対処についてメモしました。

環境

  • Vagrant 2.2.16
  • VirtualBox 6.1.22 r144080
  • Ubuntu 20.04

発生したエラー

実行ログ

vagrant upコマンドを実行したログになります。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/ubuntu-20.04'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/ubuntu-20.04' version '202105.25.0' is up to date...
==> default: Setting the name of the VM: sakura-tools_default_1622883076513_78645
==> 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: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
[default] GuestAdditions seems to be installed (6.1.22) correctly, but not running.
update-initramfs: Generating /boot/initrd.img-5.4.0-73-generic
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 
5.4.0-73-generic. Please install them and execute
  /sbin/rcvboxadd setup
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted
Restarting VM to apply changes...
==> default: Attempting graceful shutdown of VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: Warning: Connection reset. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /var/www/html => /Users/XXXXX/ubuntu/html
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,_netdev var_www_html /var/www/html

The error output from the command was:

: Invalid argument

原因

必要なモジュールが足りないことが原因でした。

解決手順

VMへログイン

vagrant sshでVMへログインします。

terminal|ホストOS
$ vagrant ssh

必要パッケージのインストール

アップデート

最初にapt-getコマンドでパッケージの最新化を実施します。

terminal|ゲストOS
sudo apt update && sudo apt upgrade -y

インストール

apt-getコマンドでlinux-headers-genericのパッケージをインストールします。

terminal|ゲストOS
$ sudo apt-get install -y linux-headers-generic

apt-getコマンドでgcc make perlのパッケージをインストールします。

terminal|ゲストOS
$ sudo apt-get install -y gcc make perl

ログアウト

exitコマンドでログアウトします。

terminal|ゲストOS
$ exit;

VMの再起動

vagrant reloadコマンドでVMを再起動します。

terminal|ホストOS
$ vagrant reload

これで解決しました。

原因調査

原因調査さした際のログになります。

vbguestプラグイン

vbguestプラグインはすでにインストール済みです。

t
$ vagrant vbguest --status [default] GuestAdditions seems to be installed (6.1.22) correctly, but not running.

VMへログイン

vagrant sshでVMへログインします。

セットアップの再実施して確認

再度直接セットアップを実施して確認してみます。

$ sudo sh /opt/VBoxGuestAdditions-6.1.22/init/vboxadd setup
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 
5.4.0-73-generic. Please install them and execute
  /sbin/rcvboxadd setup
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted

エラーの内容

ログを確認するとヘッダーが見つからないというエラーが出ています。

VirtualBox Guest Additions: Kernel headers not found for target kernel 

linux-headers-genericのインストール

linux-headers-genericのインストールを実施します。

$ apt-get install linux-headers-5.4.0-74-generic
// 中略
Setting up linux-headers-generic (5.4.0.74.77) ...

セットアップの再実施

再度直接セットアップを実施して確認してみます。

$ sudo sh /opt/VBoxGuestAdditions-6.1.22/init/vboxadd setup
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: Building the modules for kernel 5.4.0-74-generic.

This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted

エラーの内容

gcc make perlのパッケージをインストールが必要というエラーが出ています。

This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.

gcc make perlのインストール

gcc make perlのパッケージをインストールします。

$ sudo apt-get install gcc make perl -y
// 中略
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

セットアップの再実施

再度直接セットアップを実施して確認してみます。

$ sudo sh /opt/VBoxGuestAdditions-6.1.22/init/vboxadd setup
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: Building the modules for kernel 5.4.0-74-generic.
update-initramfs: Generating /boot/initrd.img-5.4.0-74-generic
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted

VMを再起動

vagrant reloadコマンドでVMを再起動します。

$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'bento/ubuntu-20.04' version '202105.25.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: Warning: Connection reset. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions seems to be installed (6.1.22) correctly, but not running.
update-initramfs: Generating /boot/initrd.img-5.4.0-74-generic
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: Building the modules for kernel 5.4.0-74-generic.
update-initramfs: Generating /boot/initrd.img-5.4.0-74-generic
VirtualBox Guest Additions: Running kernel modules will not be replaced until 
the system is restarted
Restarting VM to apply changes...
==> default: Attempting graceful shutdown of VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /var/www/html => /Users/XXXXX/ubuntu/html

以上で解決できました。