Vagrant + CentOS7 の環境構築

VirtualBox & vagrant のインストール
これは事前に N 予備校 の講座で実施済み。
$ vagrant -v
Vagrant 2.2.9
vagrant の設定
Host - Guest 間の共有フォルダ構成のため GuestAddition を導入する。
$ vagrant plugin install vagrant-vbguest
プラグインの確認。
$ vagrant plugin list
vagrant-vbguest (0.29.0, global)
CentOS7 box の追加
vagrant は box と呼ばれるイメージファイルと構成ファイルを使って管理する。 今回は CentOS 7.7 の box を追加する。 10 分くらいかかる。
$ vagrant box add bento/centos-7.7 --provider virtualbox
vagrant フォルダの作成
# フォルダ作成、移動
$ mkdir ~/vagrant/centos7
$ cd ~/vagrant/centos7
# Vagrantfile の作成
$ vagrant init bento/centos-7.7
Vagrantfile の修正
他の vagrant と識別するために config.vm.define "centos7"
で囲んだ。
IP と ポートフォワード、共有フォルダ(Host:~/vagrant/centos7/work
= Guest: /home/vagrant/work
)の設定も入れている。
Vagrant.configure("2") do |config|
config.vm.define "centos7" do |server|
config.vm.box = "bento/centos-7.7"
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "private_network", ip: "192.168.33.100"
config.vm.synced_folder "./work", "/home/vagrant/work"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
end
end
end
Vagrant の起動、停止
先ほど定義した識別名 centos7
を後ろに付けておく。
# 起動
$ vagrant up centos7
# SSH 接続
$ vagrant ssh centos7
# 停止
$ vagrant halt centos7
起動に失敗する時の対処法
vagrant up
後に以下のエラーが出た。
[default] GuestAdditions versions on your host (6.1.16) and guest (6.1.6) do not match.
GuestAddition のバージョンが合わないためエラーとなり、起動に失敗した模様。 ssh はできるので起動はできている?が、共有フォルダの構成ができてない状態。
ssh でログインし、Guest 側に直接 GuestAddition を入れ直す。
$ vagrant ssh centos7
$ curl -0 http://download.virtualbox.org/virtualbox/6.1.16/VBoxGuestAdditions_6.1.16.iso > /tmp/vboxguest.iso
$ sudo mount -o loop /tmp/vboxguest.iso /mnt
$ yes yes | sudo /mnt/VBoxLinuxAdditions.run
$ exit
これで再起動 vagrant reload
で解消と思いきや、尚も別のエラーが出た。
カーネルのバージョンの問題と思われる。
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
3.10.0-1127.el7.x86_64. Please install them and execute
/sbin/rcvboxadd setup
Stderr from the command:
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.
再び ssh でログインし、カーネルをアップデートする。
$ vagrant ssh centos7
$ sudo yum -y update kernel
$ sudo yum install gcc kernel-devel kernel-headers make bzip2 perl
$ exit
再起動したらエラーが解消。共有フォルダの構成が組めた。
$ vagrant reload centos7
追記
起動中にさらにエラー(警告?)が出る。
[centos7] GuestAdditions seems to be installed (6.1.6) correctly, but not running.
Host 側の GuestAdditions プラグインが悪さをしていそうということで、 自動アップデートを OFF にする設定を Vagrantfile に追記する。
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = false
end
これで警告は消えた。