LoginSignup
30
31

More than 5 years have passed since last update.

VagrantにCentOS 6.5をインストールする

Last updated at Posted at 2015-04-03

概要

VirtualBox (Vagrant) にCentOS 6.5をインストールする。

目的

  • VagrantのゲストOSにCentOS 6.5を導入する (Guest Additions含む)
  • サスペンド復帰時、時刻をホストに合わせる
  • ゲストOSは共有ディレクトリ、固定IP (NAT) を設定
  • Guest Additionsは自動更新

インストール手順

まずはCentOSのBoxを取得する。

$ mkdir -p ~/Vagrant/centos
$ cd ~/Vagrant/centos
$ vagrant box add centos6.5 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
$ vagrant init centos6.5
$ vi Vagrantfile
Vagrantfile
# ゲストOSは固定IPとする
config.vm.network "private_network", ip: "10.0.0.1"

# 共有ディレクトリの設定
config.vm.synced_folder "/Users/naomichi/Projects", "/home/vagrant/Projects"
$ vagrant up

VirtualBox Guest Additionsのインストールが失敗すると次のようなエラーが発生するが、Vagrant自体は起動可能 (対策は後述)。

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
```

$ vagrant ssh
$ ifconfig

ゲストOSのIPは10.0.0.1となっていることを確認できる。

eth1 Link encap:Ethernet HWaddr 08:00:27:A1:C9:56
inet addr:10.0.0.1 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fea1:c956/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1443 (1.4 KiB) TX bytes:482 (482.0 b)

VirtualBox Guest Additionsの再インストール

vagrant upでインストールエラーが起きた場合、カーネルのアップデートが必要。

$ sudo yum install -y kernel-devel kernel-headers dkms gcc gcc-c++

ホスト側でvagrant reloadを実行後、再度ゲストOSにログイン。

$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox DKMS kernel modules           [  OK  ]
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
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-431.3.1.el6.x86_64

Building the main Guest Additions module                   [FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions              [  OK  ]

kernel-develが無いと怒られる。yumが見つからないためrpmからインストール。

$ sudo yum install -y http://bay.uchicago.edu/centos-vault/6.5/updates/x86_64/Packages/kernel-devel-2.6.32-431.3.1.el6.x86_64.rpm

再度。

$ export KERN_DIR=`ls -t /usr/src/kernels/|head -1`
$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox DKMS kernel modules           [  OK  ]
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules     [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
$ service vboxadd status
The VirtualBox Additions are not currently running.

OK。

$ vagrant reload
$ vagrant ssh

サスペンド復帰時に時刻をホストと同期するよう設定しておく。

$ sudo /usr/sbin/VBoxService --timesync-set-threshold 60000

Vagrantを再起動した後も同期が有効になるようホスト側で設定。

Vagrantfile
config.vm.provider :virtualbox do |vb|
  vb.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 60000]
end

GA自動更新プラグインのインストール (任意)

vagrant-vbguestをインストールしておくことで、GAがアップデートした際はvagrant upの際に自動更新を行うようになる。

$ vagrant plugin install vagrant-vbguest
$ vagrant vbguest

X-Windowを使用していなければ次のエラーは無視してOK。

An error occurred during installation of VirtualBox Guest Additions 4.3.26. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.

$ vagrant vbguest --status
GuestAdditions 4.3.26 running --- OK.
30
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
31