はじめに
これまで Vagrant を使用した LAMP 環境を構築してきましたが、この環境を他の PC へ移動したい場合もあると思います。今回はその方法を記述していきたいと思います。
なお、この環境構築の移動ですが、ホスト OS にインストールされている VirtualBOX のバージョンによってエラーが出ることがありますので、その際はググって解決してください!(笑
環境
移行元
- Windows10 Pro(20H2)
- Vagrant(2.2.9)
- VirtualBOX(6.1.8)
移行先
- macOS Catalina(10.15.7)
- Vagrant(2.2.10)
- VirtualBOX(6.1.16)
移行元での作業
仮想マシンが起動している場合は、停止コマンドを実行して停止します。
# 仮想マシンが起動しているディレクトリへ移動して仮想マシンを停止する
vagrant halt
次に package.box ファイルを作成するために以下のコマンドを実行します。生成されるファイルは数 GB の容量がある場合があるので注意が必要です。
# package.boxファイルが作成されます
vagrant package
作成したファイルを移行先の PC へ移動します。
移行先の PC
移行先の PC での作業は以下のコマンドを実行します。VirtualBOX と Vagrant は事前にインストールしておいてください。
# boxファイルを登録します
vagrant box add [任意のbox名] [package.boxファイルへのパス]
# 登録されているか確認
vagrant box list
# 登録したbox名で初期化します
vagrant init [登録したbox名]
# 仮想マシンを起動します
vagrant up
エラーが発生した場合
私の環境では以下のエラーが発生しました。ただ、仮想マシン自体は起動しており vagrant ssh コマンドで ssh 接続は出来ました。
# 一部抜粋
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims:
VBoxService inside the vm claims: 6.1.16
Going on, assuming VBoxService is correct...
[default] GuestAdditions seems to be installed (6.1.16) correctly, but not running.
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims:
VBoxService inside the vm claims: 6.1.16
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.16
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.28.1.el8_2.x86_64. Please install them and execute
/sbin/rcvboxadd setup
ValueError: File context for /opt/VBoxGuestAdditions-6.1.16/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.28.1.el8_2.x86_64. Please install them and execute
/sbin/rcvboxadd setup
Stderr from the command:
ValueError: File context for /opt/VBoxGuestAdditions-6.1.16/other/mount.vboxsf already defined
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.
ゲストとホストで GuestAdditions のバージョンが異なっているから発生しているエラーのようです。
ゲスト:6.1.8
ホスト:6.1.16
[default] GuestAdditions versions on your host (6.1.16) and guest (6.1.8) do not match.
まずはこちらのエラーから対処していきます。
vagrant-vbguest プラグイン入れておくと自動的に更新してくれる、という情報があったのでインストールします。
# vagrant-vbguest プラグインをインストール
vagrant plugin install vagrant-vbguest
# 仮想マシンを起動
vagrant up
vagrant-vbguest プラグインを入れたけど再びエラーが発生
ここで同様のエラーが発生してしまいました。
==> default: [vagrant-hostsupdater] Removing hosts
==> default: Attempting graceful shutdown of VM...
==> 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: 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: Machine booted and ready!
[default] GuestAdditions versions on your host (6.1.16) and guest (6.1.8) do not match. <-- エラーが解決せず!
Last metadata expiration check: 0:18:59 ago on Thu 31 Dec 2020 01:51:17 AM UTC.
Package centos-linux-release-8.3-1.2011.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
Last metadata expiration check: 0:19:02 ago on Thu 31 Dec 2020 01:51:17 AM UTC.
No match for argument: kernel-devel-4.18.0-147.8.1.el8_1.x86_64
Error: Unable to find a match: kernel-devel-4.18.0-147.8.1.el8_1.x86_64
Unmounting Virtualbox Guest Additions ISO from: /mnt
umount: /mnt: not mounted.
==> default: Checking for guest additions in VM...
==> default: [vagrant-hostsupdater] Checking for host entries
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
umount /mnt
Stdout from the command:
Stderr from the command:
umount: /mnt: not mounted.
Vagrant 側のバージョンを上げるために GuestAdditions の ISO ファイルをダウンロードして、ISO ファイルをマウントするディレクトリを作成してインストールしていきます。
# ISOファイルをダウンロード
wget http://download.virtualbox.org/virtualbox/6.1.16/VBoxGuestAdditions_6.1.16.iso
# 一時保存フォルダを作成
sudo mkdir home_tmp
# 作成したフォルダにISOファイルをマウントする
sudo mount -o loop,ro VBoxGuestAdditions_6.1.16.iso home_tmp
# インストールを実行
sudo sh home_tmp/VBoxLinuxAdditions.run
# 保存したISOファイルを削除
rm VBoxGuestAdditions_6.1.16.iso
# フォルダのマウントを解除
sudo umount home_tmp/
# 作業フォルダを削除
sudo rmdir home_tmp/
# ゲストOSからログアウト
exit
# Vagrantを再起動
vagrant reload
またエラーが発生しました。
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims: 6.1.8
VBoxService inside the vm claims: 6.1.16
Going on, assuming VBoxService is correct...
[default] GuestAdditions seems to be installed (6.1.16) correctly, but not running.
Got different reports about installed GuestAdditions version:
Virtualbox on your host claims: 6.1.8
VBoxService inside the vm claims: 6.1.16
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: 6.1.8
VBoxService inside the vm claims: 6.1.16
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-147.8.1.el8_1.x86_64. Please install them and execute
/sbin/rcvboxadd setup
ValueError: File context for /opt/VBoxGuestAdditions-6.1.16/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: [vagrant-hostsupdater] Checking for host entries
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-147.8.1.el8_1.x86_64. Please install them and execute
/sbin/rcvboxadd setup
Stderr from the command:
ValueError: File context for /opt/VBoxGuestAdditions-6.1.16/other/mount.vboxsf already defined
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.
GuestAdditions が正しくインストールされているけど実行されていないとメッセージが出ています。
GuestAdditions seems to be installed (6.1.16) correctly, but not running.
その他エラーメッセージを確認していくと、
See "systemctl status vboxadd.service" and "journalctl -xe" for details.
このコマンドを実行して詳細を確認してね、とありました。
また、関連のログファイルは/var/log/vboxadd-setup.log
に配置されています。
ゲスト OS にログインしてコマンドを実行してみます。
# ゲスト OS にログイン
vagrant ssh
# root ユーザーで実行
sudo systemctl status vboxadd.service
vboxadd.service
Loaded: loaded (/opt/VBoxGuestAdditions-6.1.16/init/vboxadd; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2021-01-25 23:35:11 UTC; 10min ago
Process: 2259 ExecStart=/opt/VBoxGuestAdditions-6.1.16/init/vboxadd start (code=exited, status=1/FAILURE)
Main PID: 2259 (code=exited, status=1/FAILURE)
Jan 25 23:35:10 localhost.localdomain vboxadd[2259]: VirtualBox Guest Additions: /sbin/rcvboxadd quicksetup all
Jan 25 23:35:10 localhost.localdomain vboxadd[2259]: VirtualBox Guest Additions: Kernel headers not found for target kernel
Jan 25 23:35:10 localhost.localdomain vboxadd[2259]: 4.18.0-147.8.1.el8_1.x86_64. Please install them and execute
Jan 25 23:35:10 localhost.localdomain vboxadd[2259]: /sbin/rcvboxadd setup
Jan 25 23:35:11 localhost.localdomain vboxadd[2259]: ValueError: File context for /opt/VBoxGuestAdditions-6.1.16/other/mount.vboxsf already defined
Jan 25 23:35:11 localhost.localdomain vboxadd[2259]: modprobe vboxguest failed
Jan 25 23:35:11 localhost.localdomain vboxadd[2259]: The log file /var/log/vboxadd-setup.log may contain further information.
Jan 25 23:35:11 localhost.localdomain systemd[1]: vboxadd.service: Main process exited, code=exited, status=1/FAILURE
Jan 25 23:35:11 localhost.localdomain systemd[1]: vboxadd.service: Failed with result 'exit-code'.
Jan 25 23:35:11 localhost.localdomain systemd[1]: Failed to start vboxadd.service.
vboxadd.service
が起動していません。
また、Kernel headers が見つかりませんと出力されています。
Jan 25 23:35:10 localhost.localdomain vboxadd[2259]: VirtualBox Guest Additions: Kernel headers not found for target kernel
Jan 25 23:35:10 localhost.localdomain vboxadd[2259]: 4.18.0-147.8.1.el8_1.x86_64. Please install them and execute
vagrant-vbguest プラグインを入れた直後のエラーメッセージにも、カーネルに関するエラーが出力されていました。
No match for argument: kernel-devel-4.18.0-147.8.1.el8_1.x86_64
Error: Unable to find a match: kernel-devel-4.18.0-147.8.1.el8_1.x86_64
Kernel headers をインストールする
まずは使用中の kernel と kernel headers のバージョンを合わせるために、パッケージのアップデートを行います。
# パッケージをアップデートする
sudo dnf -y update
# アップデート後に再起動
sudo reboot
# 不足しているパッケージやビルドするためのツールをインストール
sudo dnf install gcc kernel-devel kernel-headers dkms make bzip2 perl
# ゲストOSからログアウト
exit
# Vagrantを再起動する
vagrant reload
# 再起動後SSHログイン
vagrant ssh
SSH でログインして状態を確認します。
sudo systemctl status vboxadd.service
● vboxadd.service
Loaded: loaded (/opt/VBoxGuestAdditions-6.1.16/init/vboxadd; enabled; vendor preset: disabled)
Active: active (exited) since Tue 2021-01-26 00:25:38 UTC; 1min 22s ago
Process: 844 ExecStart=/opt/VBoxGuestAdditions-6.1.16/init/vboxadd start (code=exited, status=0/SUCCESS)
Main PID: 844 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4963)
Memory: 0B
CGroup: /system.slice/vboxadd.service
Jan 26 00:25:32 localhost.localdomain dracut[5840]: -rw-r--r-- 1 root root 1377 May 11 2019 usr/share/terminfo/v/vt220
Jan 26 00:25:32 localhost.localdomain dracut[5840]: lrwxrwxrwx 1 root root 20 Aug 11 21:13 usr/share/unimaps -> /usr/lib/kbd/unimaps
Jan 26 00:25:32 localhost.localdomain dracut[5840]: drwxr-xr-x 3 root root 0 Aug 11 21:13 var
Jan 26 00:25:32 localhost.localdomain dracut[5840]: lrwxrwxrwx 1 root root 11 Aug 11 21:13 var/lock -> ../run/lock
Jan 26 00:25:32 localhost.localdomain dracut[5840]: lrwxrwxrwx 1 root root 6 Aug 11 21:13 var/run -> ../run
Jan 26 00:25:32 localhost.localdomain dracut[5840]: drwxr-xr-x 2 root root 0 Aug 11 21:13 var/tmp
Jan 26 00:25:32 localhost.localdomain dracut[5840]: ========================================================================
Jan 26 00:25:32 localhost.localdomain dracut[5840]: *** Creating initramfs image file '/boot/initramfs-4.18.0-240.10.1.el8_3.x86_64.img' done ***
Jan 26 00:25:37 localhost.localdomain vboxadd[844]: ValueError: File context for /opt/VBoxGuestAdditions-6.1.16/other/mount.vboxsf already defined
Jan 26 00:25:38 localhost.localdomain systemd[1]: Started vboxadd.service.
vboxadd.service
が正しく起動していました。
再び Vagrant を再起動します。起動時にカーネルのビルドが行われるので少し時間がかかると思います。
# 再起動
vagrant reload
==> default: [vagrant-hostsupdater] Removing hosts
==> default: Attempting graceful shutdown of VM...
==> 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: 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: Machine booted and ready!
[default] GuestAdditions seems to be installed (6.1.16) correctly, but not running.
Redirecting to /bin/systemctl start vboxadd.service
Redirecting to /bin/systemctl start vboxadd-service.service
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
4.18.0-240.10.1.el8_3.x86_64.
ValueError: File context for /opt/VBoxGuestAdditions-6.1.16/other/mount.vboxsf already defined
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: [vagrant-hostsupdater] Checking for host entries
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /Users/root1/Desktop/myVagrant/project03
これで不具合が解消されました!
終わりに
今回は Vagrant で構築した環境を他の PC へ移行する手順を紹介しました。
一度ハマると中々抜け出せない環境構築ですが、作ってしまったものを使いまわして効率よく開発を進めて行きたいと思います!
バージョン上げるの怖いな。。。