VagrantのVirtualBoxで利用するboxを作成 CentOS(6.4) ver.

必要となるCentOSのバージョンが手元で見つからなかったので、Vagrantのboxを改めて作ってみる。
その際、久しぶりだったのでVagrant boxの作成手順を再確認。

この手順は特にPackerやVeeWeeを使わずに手作業で行う。
今回、必要なCentOSのバージョンが6.4なので、予めMirror siteを確認しておく。
ここでは、以下のものを利用。
http://ftp.riken.jp/Linux/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-minimal.iso
予めダウンロードしておくこと。

上記にはないバージョンもあるので、CentOSのMirror site一覧から確認するのもよい。
http://www.centos.org/download/mirrors/

次に、VirtualBoxの設定
※各設定に関しては、環境に応じて設定可能
1. VirtualBoxを起動し、新規を選択

2.続けるを選択して、メモリの設定

  • メモリ: 512MB

3.ハードドライブの設定

  • 仮想ハードドライブを作成する

4.ハードドライブのファイルタイプ

5.物理ハードドライブにあるストレージ

  • 可変サイズ

6. ファイルの場所とサイズ

  • 初期設定

ここで一旦、初期設定は完了なのだが、以下の設定も加えておく。

  • USB: OFF

ポート、USBを選択し、USBコントローラーを有効化のチェックを外す

  • オーディオ: OFF

オーディオを選択し、オーディオを有効化のチェックを外す

ネットワークからアダプター1を選択、高度のタブを開きポートフォワーディングを選択
以下の設定を右上の+ボタンから追加

設定が完了したら、次にOSをインストールする
対象のマシンをVirtualBox上で選択し、起動を選択するとOSのisoイメージの選択画面となるので、
対象のisoイメージを選択し、START
今回は上記で予め手元にダウンロードしておいたCentOS-6.4-x86_64-minimal.iso を利用

1. 起動すると、最初に「Welcome to CentOS 6.4!」の画面が表示されます。「Install or upgrade an existing system」を選択してEnter
2.Disc Found
Skipを選択
3.Unsupported Hardware Detected
OK
4.CentOS
OK
5.Language Selection
English
6.Keyboard Selection
US
7.Warning
Warningがでるのですが、ここは「Re-initialize-all」を選択
8.Time Zone Selection
Asia/Tokyoを選択
9.Root Password
vagrant
※Weak Passwordのアラートがでるが、ここはvagrantで設定する必要がある。
10.Partitioning Type
Use entire drive
11.Writing storage configuration to disk
Write changes to disk

以上、設定が終わったところでrebootし、以下の内容でログインする。
llocalhost login : root
Password : vagrant

今回はminimalでインストールを実施したので、起動時にupしていないNICの設定を見直す。

sed -i -e "s:^ONBOOT=no$:ONBOOT=yes:" /etc/sysconfig/network-scripts/ifcfg-eth0
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep ONBOOT
service network restart
ip addr show eth0

MACアドレスとUUIDを無効化

ln -f -s /dev/null /etc/udev/rules.d/70-persistent-net.rules 
sed -i -e "s:HWADDR=.*::g" /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -e "s:UUID=.*::g" /etc/sysconfig/network-scripts/ifcfg-eth0

sshの設定後。 UseDNS を起動

sed -i -e "s:^#UseDNS yes:UseDNS no:" /etc/ssh/sshd_config
service sshd start
chkconfig sshd on

ここからの手順は、通常のターミナルからVirtualBoxにアクセスして作業することとする。

ssh root@localhost -p 2222

vagrant グループ&ユーザ追加

groupadd vagrant
useradd vagrant -g vagrant -G wheel
# sudo設定
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# requiretty を無効
sed -i -e "s:^.*requiretty:#Defaults requiretty:" /etc/sudoers

vagrantユーザ用の公開鍵を配置

su - vagrant
mkdir ~/.ssh
chmod 0700 ~/.ssh
curl -L -o ~/.ssh/authorized_keys  https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
chmod 0600 ~/.ssh/authorized_keys

VirtualBox の Guest Additions をインストール
手順は公式のドキュメントを参考にする
http://docs.vagrantup.com/v2/virtualbox/boxes.html

まずは必要なものをインストール

yum install -y wget 
mkdir /media/VBoxGuestAdditions
mount -o loop,ro VBoxGuestAdditions_4.2.16.iso /media/VBoxGuestAdditions
sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run

これでうまくいくと思いきやエラーとなる。

The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-2.6.32-358.el6.x86_64

Building the main Guest Additions module                   [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions              [  OK  ]
Installing the Window System drivers                       [失敗]
(Could not find the X.Org or XFree86 Window System.)

必要なバージョンの'kernel-devel'をインストール。古いバージョンなので、yumではなくrpm
直接インストールする。

wget ftp://mirror.switch.ch/pool/4/mirror/scientificlinux/6.3/x86_64/updates/security/kernel-devel-2.6.32-358.el6.x86_64.rpm
cd /usr/local/src/
rpm -ivh kernel-devel-2.6.32-358.el6.x86_64.rpm 

#再度、インストールを実施
sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
The gcc utility was not found. If the following module compilation fails then
this could be the reason and you should try installing it.

Building the main Guest Additions module                   [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)

またしてもエラーとなる。'gcc'が足りないといおうことなので、インストールして再度トライが失敗する。

yum install -y gcc
sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run

Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)

今度はログを確認しろとのことなので、エラーを確認したところ以下のエラーが出ていることが分かるので、
必要な'perl'をインストールして再度インストールするとうまく行った。

yum install -y perl
sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run

Verifying archive integrity... All good.
Uncompressing VirtualBox 4.2.16 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.2.16 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
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  ]
Installing the Window System drivers                       [失敗]
(Could not find the X.Org or XFree86 Window System.)

'Installing the Window System drivers [失敗]'はなくても大丈夫そうなので、
無視して終了。

後処理。

rm VBoxGuestAdditions_4.2.16.iso 
umount /media/VBoxGuestAdditions
rmdir /media/VBoxGuestAdditions

最後に、Vagrantのboxを作成。

cd ~/VirtualBox\ VMs/vagrant-centos64_64_mini/
vagrant package --base vagrant-centos64_64_mini --output centos64_64_mini.box
ls -la centos64_64_mini.box

せっかくなので、作成したBoxを使用してVagrantを起動してみる。

vagrant box add centos64_64_mini centos64_64_mini.box
vagrant init centos64_64_mini
vagrant up
vagrant ssh
uname -a
Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

無事に作成完了。
以下に、作成したVagrant Boxを公開しておく。
https://s3-ap-northeast-1.amazonaws.com/banrui/VagrantBox/CentOS/centos64_64_mini.box