読者です 読者をやめる 読者になる 読者になる

CentOS6.6へのVairtualBox と Vagrantのインストール

Chefの本を読んでいたら、Cehfとの連携にはVagrantが最適!との事だったので、
環境を作ってみます。

■環境

[root@vagrant centos6]# cat /etc/redhat-release
CentOS release 6.6 (Final)

[root@vagrant centos6]# uname -r
2.6.32-504.12.2.el6.x86_64

[root@vagrant centos6]# rpm -qa | grep vagrant
vagrant-1.3.5-1.x86_64

perlのインストール

[root@vagrant centos6]# yum install perl

wgetのインストール(入っていなかったため)

[root@vagrant centos6]# yum install perl

レポジトリ(EPL)の構築

[root@vagrant centos6]# sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@vagrant centos6]# cat /etc/yum.repos.d/epel.repo

[root@vagrant centos6]# yum info dkms
[root@vagrant centos6]# yum install dkms

VirtualBox のインストール

[root@vagrant centos6]# cd /etc/yum.repos.d/
[root@vagrant centos6]# sudo wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
[root@vagrant centos6]# yum list | grep VirtualBox
[root@vagrant centos6]# yum install VirtualBox-4.3
[root@vagrant centos6]# sudo usermod -a -G vboxusers ope
[root@vagrant centos6]# sudo usermod -a -G vboxusers root

vagrantの導入

[root@vagrant centos6]# sudo rpm -Uvh http://files.vagrantup.com/packages/a40522f5fabccb9ddabad03d836e120ff5d14093/vagrant_1.3.5_x86_64.rpm

vagrant boxの追加

[root@vagrant centos6]# vagrant box add centos http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.5-i386-v20140504.box

■Vagrantfileの作成

[root@vagrant centos6]# mkdir centos6
[root@vagrant centos6]# chmod 755 centos6/
[root@vagrant centos6]# cd centos6
・・・格納するディレクトリを作った後に
[root@CentOS6 centos6]# vagrant init centos
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

vagrantファイルを作成。

[root@vagrant centos6]# pwd
/tmp/vagrant/centos6
[root@vagrant centos6]#
[root@vagrant centos6]# tree --charset=C
.
`-- Vagrantfile

確認するとできてます。

[root@vagrant centos6]# pwd
/tmp/vagrant/centos6
[root@vagrant centos6]# ls -ltr
合計 8
-rw-r--r-- 1 root root 4610  315 12:16 2015 Vagrantfile

Vagrantファイルの中身

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "centos"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
# config.vm.box_url = "http://domain.com/path/to/above.box"

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network :forwarded_port, guest: 80, host: 8080

# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network :private_network, ip: "192.168.33.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network :public_network

# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider :virtualbox do |vb|
# # Don't boot with headless mode
# vb.gui = true
#
# # Use VBoxManage to customize the VM. For example to change memory:
# vb.customize ["modifyvm", :id, "--memory", "1024"]
# end
#
# View the documentation for the provider you're using for more
# information on available options.

# Enable provisioning with Puppet stand alone. Puppet manifests
# are contained in a directory path relative to this Vagrantfile.
# You will need to create the manifests directory and a manifest in
# the file centos.pp in the manifests_path directory.
#
# An example Puppet manifest to provision the message of the day:
#
# # group { "puppet":
# # ensure => "present",
# # }
# #
# # File { owner => 0, group => 0, mode => 0644 }
# #
# # file { '/etc/motd':
# # content => "Welcome to your Vagrant-built virtual machine!
# # Managed by Puppet.\n"
# # }
#
# config.vm.provision :puppet do |puppet|
# puppet.manifests_path = "manifests"
# puppet.manifest_file = "site.pp"
# end

# Enable provisioning with chef solo, specifying a cookbooks path, roles
# path, and data_bags path (all relative to this Vagrantfile), and adding
# some recipes and/or roles.
#
# config.vm.provision :chef_solo do |chef|
# chef.cookbooks_path = "../my-recipes/cookbooks"
# chef.roles_path = "../my-recipes/roles"
# chef.data_bags_path = "../my-recipes/data_bags"
# chef.add_recipe "mysql"
# chef.add_role "web"
#
# # You may also specify custom JSON attributes:
# chef.json = { :mysql_password => "foo" }
# end

# Enable provisioning with chef server, specifying the chef server URL,
# and the path to the validation key (relative to this Vagrantfile).
#
# The Opscode Platform uses HTTPS. Substitute your organization for
# ORGNAME in the URL and validation key.
#
# If you have your own Chef Server, use the appropriate URL, which may be
# HTTP instead of HTTPS depending on your configuration. Also change the
# validation key to validation.pem.
#
# config.vm.provision :chef_client do |chef|
# chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
# chef.validation_key_path = "ORGNAME-validator.pem"
# end
#
# If you're using the Opscode platform, your validator client is
# ORGNAME-validator, replacing ORGNAME with your organization name.
#
# If you have your own Chef Server, the default validation client name is
# chef-validator, unless you changed the configuration.
#
# chef.validation_client_name = "ORGNAME-validator"
end

次に仮想マシンvagrant initコマンドで起動。

[root@vagrant centos6]# vagrant init centos

・・・とここまでやったところでエラーが。

[root@CentOS6 centos6]# vagrant up
VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

[root@CentOS6 centos6]# VBoxManage --version
WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (2.6.32-504.8.1.el6.x86_64) or it failed to
         load. Please recompile the kernel module and install it by

           sudo /etc/init.d/vboxdrv setup

         You will not be able to start VMs until this problem is fixed.
4.3.24r98716

[root@CentOS6 centos6]# sudo /etc/init.d/vboxdrv setup
Stopping VirtualBox kernel modules                         [  OK  ]
Uninstalling old VirtualBox DKMS kernel modules            [  OK  ]
Trying to register the VirtualBox kernel modules using DKMSError! echo
Your kernel headers for kernel 2.6.32-504.8.1.el6.x86_64 cannot be found at
/lib/modules/2.6.32-504.8.1.el6.x86_64/build or /lib/modules/2.6.32-504.8.1.el6.x86_64/source.
                                                           [失敗]
  (Failed, trying without DKMS)
Recompiling VirtualBox kernel modules                      [失敗]
  (Look at /var/log/vbox-install.log to find out what went wrong)

以下の記事を参考に確認すると、kernel headerとkernelの
バージョンに差分がある可能性との事。
確認すると確かにバージョンにズレがあります。

[root@CentOS6 centos6]# rpm -qa | grep ^kernel-
kernel-headers-2.6.32-504.12.2.el6.x86_64
kernel-devel-2.6.32-504.12.2.el6.x86_64
kernel-2.6.32-504.8.1.el6.x86_64
kernel-2.6.32-504.el6.x86_64
kernel-firmware-2.6.32-504.8.1.el6.noarch

ので、バージョンを揃えるべくkernelのアップデート実施

■Kernelのアップデート

sudo yum update kernel-`uname -r
reboot

更新後に再度確認。

[root@vagrant ~]# rpm -qa | grep ^kernel-
kernel-headers-2.6.32-504.12.2.el6.x86_64
kernel-devel-2.6.32-504.12.2.el6.x86_64
kernel-2.6.32-504.12.2.el6.x86_64
kernel-2.6.32-504.8.1.el6.x86_64
kernel-firmware-2.6.32-504.12.2.el6.noarch
kernel-2.6.32-504.el6.x86_64

これでバージョンが揃ったはず、vagrant upすると今度は成功!

[root@vagrant centos6]# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] Mounting shared folders...
[default] -- /vagrant

後は簡単に動作確認する所まで。

[root@vagrant centos6]# vagrant ssh
Welcome to your Vagrant-built virtual machine.

[vagrant@localhost ~]$ pwd
/home/vagrant

[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

[root@vagrant centos6]# vagrant halt
[default] Attempting graceful shutdown of VM...

[root@vagrant centos6]# vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

[root@vagrant centos6]# vagrant destroy
Are you sure you want to destroy the 'default' VM? [y/N] y
[default] Destroying VM and associated drives...

[root@vagrant centos6]# vagrant status
Current machine states:

default                   not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.
[root@vagrant centos6]#