LoginSignup
204
212

More than 5 years have passed since last update.

CentOS 上で Vagrant を導入するまでのメモ(CUI)

Last updated at Posted at 2013-10-27

CentOS 上で Vagrant を導入するまでのメモ

GNOME 等 GUI が動作する環境での Vagrant 導入までの手順については, 様々なサイトで説明されていた.
しかしながら, CUI での Vagrant 導入までの日本語の手順は少なかったのでメモする.
というのも, まず, CUI での VirtualBox 導入までのドキュメントが少なかった.
(もともと, GUI で Linux を扱うことがなかったので, これから X Window 入れて, っつーことが面倒くさかった.)

やってみてわかったことは, VirtualBox をインストールできればよいということ.
インストールしてしまえさえすれば, 自分で VM を起動できなくても vagrant が VM を起動してくれるし,
vagrant のインストールはとても簡単である.

% cat /etc/redhat-release
CentOS release 6.4 (Final)
% uname -a
Linux centos_63 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

まず, VirtualBox をインストールする.
日本語だと情報が少なかったんで, いろいろ読んでみる.
特に参考になったのは以下のドキュメントだった.

http://wiki.centos.org/HowTos/Virtualization/VirtualBox

https://www.virtualbox.org/manual/ch02.html#install-linux-host

基本的に, 上記2つのドキュメント以外は参考にしていない.
(というのも, わりとすんなり入ってくれたからです.)
さて, インストールメモを書いていこうっと.

DKMS のインストール

まず, DKMS をインストールします.
これをインストールする理由は, そうすることが推奨されていたからです.
とは言え, インストールする理由もメモっておきます.

VirtualBox は vboxdrv と呼ばれる特殊なカーネルモジュールを用いています.
カーネルモジュールとは, カーネル空間で動作するプログラムのことであり, 動的に(カーネルが動作しているときに)カーネルに組み込んで, 動作させられるプログラムのことです.

http://www.oidon.net/linux/build-arm-linux-kernel-module

http://itpro.nikkeibp.co.jp/article/Keyword/20081107/318745/

http://homepage3.nifty.com/rio_i/lab/driver24/001module.html

http://www.tldp.org/LDP/lkmpg/2.6/html/index.html

カーネルモジュールは将来的なカーネルアップデートがあった際にはインストールし直さなければなりません. それは非常に面倒くさいです. そのために, dkms のインストールします.

DKMS はカーネルモジュールのビルドとアップデートを助けるフレームワークだそうです. DKMS が インストールされていれば, VirtualBox カーネルモジュールは常に動作し続けることが可能となります. もし, ホスト OS のカーネルがアップデートされたとしても心配いりません. VirtualBox のカーネルモジュール(vboxdrv)も自動的にビルド、アップデートされます.

DKMS のインストール方法は以下
http://wiki.centos.org/HowTos/Virtualization/VirtualBox

上のドキュメントをそのまま実行していけば問題なくできました.

yum からインストールするためには、DKMS があるリポジトリを追加してやればオッケーです.

僕は, なんとなく yum からインストールするようにしたかったので, そうします.

sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
cat /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=1

yum からの DKMS のインストールが可能となったので, インストールします.

% yum info dkms
Name        : dkms
Arch        : noarch
Version     : 2.2.0.3
Release     : 17.el6
Size        : 207 k
Repo        : installed
From repo   : epel
Summary     : Dynamic Kernel Module Support Framework
URL         : http://linux.dell.com/dkms
License     : GPLv2+
Description : This package contains the framework for the Dynamic Kernel Module Support (DKMS)
            : method for installing module RPMS as originally developed by Dell.
yum install dkms

VirtualBox のインストール

つづいて, VirtualBox をインストールします. ここでも, 気分的に yum 使いたかったので, yum からのインストールをメモります.

yum からインストールしたい場合に参考になるページ
http://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-rhel

まず, リポジトリを追加します.

cd /etc/yum.repos.d/
sudo wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo

そして, yum からのインストールが可能になったことを確認します.

% yum list | grep VirtualBox
VirtualBox-3.2.x86_64                    3.2.18_89859_el6-1             virtualbox
VirtualBox-4.0.x86_64                    4.0.20_89853_el6-1             virtualbox
VirtualBox-4.1.x86_64                    4.1.28_89849_el6-1             virtualbox
VirtualBox-4.2.x86_64                    4.2.18_88780_el6-1             virtualbox
VirtualBox-4.3.x86_64                    4.3.0_89960_el6-1              virtualbox

yum からインストールします.

yum install VirtualBox-4.3

つづいて, setup をおこないます. yum install を行うと, yum installer は, vboxusers グループと必要なカーネルモジュール(vboxdrv)を構築します. virtual box のユーザは, vboxusers グループに所属していなければなりません. 次のコマンドでユーザをグループに所属させます.

sudo usermod -a -G vboxusers username

ここまでで, インストールは完了となります.

この後, VirtualBox の起動をしなければならないような気がしたのですが, それは vagrant に任せることにします. なぜなら, CUI からの VirtualBox の起動を手作業でやることは, 中々大変な予感がしたからです. (大変な作業だから Vagrant があるんです)

Vagrant の導入

Vagrant の導入は本当に簡単です. 以下を読めばすぐにできるでしょう.

http://docs.vagrantup.com/v2/installation/index.html

Vagrant とは何でしょう?

とりあえず, そこから調べます. とりあえず, ドキュメントを読みます.

http://docs.vagrantup.com/v2/getting-started/index.html

Vagrant は仮想マシンを肩代わりするツールだそうです. これらの仮想マシンは, VirtualBox, VMware, AWS, で提供されます.
仮想マシンの提供者(VirtualBox, VMWare, AWS)を provider と呼びます.

provider には様々なものを選ぶことができるそうです.

VirtualBox, VMWare, AWS, etc...

用途によって, provider を選択することもできるそうです.(すなわち, provider と仮想マシンの設定が独立しているということ?)

Getting Started では, Virtual Box を利用しています.(だから今回 VirtualBox をインストールしました.)

CentOS 6.4 64bit に vagrant をインストールするには以下のように, rpm からインストールすればいいだけです.

http://docs.vagrantup.com/v2/installation/

sudo rpm -Uvh http://files.vagrantup.com/packages/a40522f5fabccb9ddabad03d836e120ff5d14093/vagrant_1.3.5_x86_64.rpm

インストールするホストに応じて, rpm を選択しましょう.

http://downloads.vagrantup.com/

UP AND RUNNING

インストールは完了しました. 続いてセットアップして起動します.

http://docs.vagrantup.com/v2/getting-started/index.html

ドキュメントに書かれている通りコマンドを叩いてみます.

% vagrant init precise32 http://files.vagrantup.com/precise32.box
% vagrant up

叩くと次のようなメッセージが表示されます.

% vagrant init precise32 http://files.vagrantup.com/precise32.box
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.

Vagrantfile なるものがあるらしいです. よく分からんが, とりあえず, vagrant up をしてみます.

% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Box 'precise32' was not found. Fetching box from specified URL for
the provider 'virtualbox'. Note that if the URL does not have
a box for this provider, you should interrupt Vagrant now and add
the box yourself. Otherwise Vagrant will attempt to download the
full box prior to discovering this error.
Downloading or copying the box...
Progress: 23% (Rate: 992k/s, Estimated time remaining: 0:06:13)

すると, なにやらダウンロードっぽいことが始まります.
マシン 'default' を 'virtualbox' provider へ bring up with しているらしいです.
vagrant up が完了すると以下のようになります.

% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Box 'precise32' was not found. Fetching box from specified URL for
the provider 'virtualbox'. Note that if the URL does not have
a box for this provider, you should interrupt Vagrant now and add
the box yourself. Otherwise Vagrant will attempt to download the
full box prior to discovering this error.
Downloading or copying the box...
Extracting box...te: 422k/s, Estimated time remaining: 0:00:01)0
Successfully added box 'precise32' with provider 'virtualbox'!
[default] Importing base box 'precise32'...
[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] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.3
[default] Mounting shared folders...
[default] -- /vagrant

コマンドはステータスコード 0 で終わったので成功したらしいです.
いろいろ不明点はありますが, とりあえず, Getting Started のフローに従って, vagrant ssh コマンドを叩いてみます.

% vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
vagrant@precise32:~$

すると, ubuntu にログインっぽいことができました.
一応うまくいったってことかな?

どういう仕組みなんだろう?すごい.
これから調査していく予定です.

204
212
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
204
212