端くれプログラマの備忘録 Vagrant [Vagrant] CentOSの仮想マシン環境を構築する

[Vagrant] CentOSの仮想マシン環境を構築する

Boxの取得

CentOSのBoxは以下のサイトで見つかった。

A list of base boxes for Vagrant – Vagrantbox.es
http://www.vagrantbox.es/

CentOS7.0 x86_64 minimal (VirtualBoxGuestAddtions 4.3.14)
https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box

> vagrant box add centos7 https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com
/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box

仮想マシンの構築

> mkdir centos7
> cd centos7
> vagrant init centos7
....
> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7_default_1413255248525_61722
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
 default: Adapter 1: nat
==> default: Forwarding ports...
 default: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
 default: SSH address: 127.0.0.1:2200
 default: SSH username: vagrant
 default: SSH auth method: private key
 default: Warning: Connection timeout. Retrying...
 default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
 default: /vagrant => C:/Temp/centos7
Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:
 
mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3`
vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant
 
The error output from the last command was:
 
/sbin/mount.vboxsf: mounting failed with the error: No such device
 
>

“vboxsf”ファイルシステムが利用できないために共有フォルダのマウントに失敗したが、ネットを検索したらすぐ解決法が見つかった。VirtualBox関係のモジュールをビルドする必要があるらしい。

vagrantでmountエラーの解決方法 – Qiita
http://qiita.com/osamu1203/items/10e19c74c912d303ca0b

仮想マシンの起動には成功しているので、sshでCentOSに接続してモジュールのビルドを実行する。

[vagrant@localhost ~]$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox non-DKMS kernel modules [ OK ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module [ OK ]
Building the shared folder support module [ OK ]
Building the OpenGL support module [ OK ]
Doing non-kernel setup of the Guest Additions [ OK ]
Starting the VirtualBox Guest Additions [ OK ]
[vagrant@localhost ~]$

モジュールがビルドできたらsshを切断し、仮想マシンを再起動する。

> vagrant halt
...
> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> 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: Forwarding ports...
 default: 22 => 2222 (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 timeout. Retrying...
 default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
 default: /vagrant => C:/Temp/centos7
>

今度は共有フォルダのマウントに成功。これでCentOSの仮想マシンが無事稼動。

virtualbox_centos7