Vagrantを使ってみる

昨日、Macbook AirにインストールしたVagrantを使ってみました。
Vagrantはちょっと前から気になってはいたのですが、これまで利用していたザクが気が遠くなる程クソ遅かったので、なかなか試す気になれなかったのでした。しかし、新しいモビルスーツを手に入れた今、その制約から解き放たれ、ジオン再興のために動き出す時が来たのです。Airの速さに触れた時、思わず「ソロモンよ、私は帰ってきた!」と叫びたくなりました。

話がそれました。今回のマシン新調に伴いインストールしたものの中で、一番の目玉はなんといってもこのVagrantでしょう。Vagrantは、すばやく仮想環境を立ち上げることのできるツールです。概要は、最近のこの記事が参考になるでしょう。では、簡単にインストールの手順と、実行について書いておきます。OSはOSX 10.8.4 MountainLionです。

1.VirtualBoxのインストール

Vagrantは、ゲストOSを動作させるプロバイダとしてVirtualBoxの他、VMware Fusionなんかも使えるようですが、ほとんどの方がVirtualBoxを選んでいるようなので、私もVirtualBoxにしました。VirtualBoxは、次の場所からダウンロードできます。
Download VirtualBox:VirtualBox

VirtualBoxのダウンロードページ

VirtualBoxのダウンロードページ


上記ダウンロードページから、VirtualBox platform packages:VirtualBox 4.2.16 for OS X hostsと、一応VirtualBox 4.2.16 Oracle VM VirtualBox Extension Packを選択し、次の2つのファイルをダウンロードします。

  • VirtualBox-4.2.16-86992-OSX.dmg
  • Oracle_VM_VirtualBox_Extension_Pack-4.2.16-86992.vbox-extpack

FinderでVirtualBox-4.2.16-86992-OSX.dmgを開き、表示されたダイアログの指示に従ってインストールします。
次に、Extension Packをインストールします。メニューの[環境設定]で表示されるダイアログから[機能拡張]を選択し、表示された画面右側の追加アイコンをクリックして、Oracle_VM_VirtualBox_Extension_Pack-4.2.16-86992.vbox-extpackを選択します。

VirtualBox Extension Packの追加

VirtualBox Extension Packの追加


以上で、VirtualBoxのインストールは完了です。

2.Vagrantのインストール

Vagrantは次の場所からダウンロードします。
Vagrant Donwloads
最新のバージョンを選択して(現時点ではv1.2.4)、表示されたページから.dmgファイルを選択します。

Vagrantのダウンロードページ

Vagrantのダウンロードページ


ダウンロードされたVagrant-1.2.4.dmgファイルをFinderで開いてインストールします。インストールが完了したら、ターミナルで確認してみます。

$ vagrant -v
Vagrant version 1.2.4

インストールできているようです。

3.VagrantにBoxを追加する

Vagrantで使用する仮想OSは、Boxと呼ぶそうです。このBoxは自分で作成できるようなのですが、簡単に用意されているものをインストールしたいと思います。次の場所でBoxの一覧が公開されております。
Vagrantbox.es
今回は、National Renewable Energy Laboratoryというところが公開している、CentOS-6.4-x86_64-v20130427.boxを入れてみます。

$ vagrant box add centos64_64 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box
Downloading or copying the box...
Extracting box...te: 711k/s, Estimated time remaining: --:--:--)
Successfully added box 'centos64_64' with provider 'virtualbox'!

インストールできたか確認。

$ vagrant box list
centos64_64 (virtualbox)

ちゃんと入っているようです。

4.試してみる

まずは、vagrant initでVagrantfileを作成します。

$ vagrant init
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を編集します。VM名をrena、ホスト名もrenaとし、パブリックネットワークを指定しています。
Vagrantfile

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

Vagrant.configure("2") 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 = "centos64_64"

  config.vm.define :rena do |rena|
    rena.vm.hostname = "rena"
    rena.vm.network :public_network
  end
end

vagrant upで起動します。途中でどのネットワークインタフェースを使用するか聞いてくるので、選択します。

$ vagrant up rena
Bringing machine 'rena' up with 'virtualbox' provider...
[rena] Importing base box 'centos64_64'...
[rena] Matching MAC address for NAT networking...
[rena] Setting the name of the VM...
[rena] Clearing any previously set forwarded ports...
[rena] Creating shared folders metadata...
[rena] Clearing any previously set network interfaces...
[rena] Available bridged network interfaces:
1) en0: Wi-Fi (AirPort)
2) p2p0
What interface should the network bridge to? 1
[rena] Preparing network interfaces based on configuration...
[rena] Forwarding ports...
[rena] -- 22 => 2222 (adapter 1)
[rena] Booting VM...
[rena] Waiting for VM to boot. This can take a few minutes.
[rena] VM booted and ready for use!
[rena] Setting hostname...
[rena] Configuring and enabling network interfaces...
[rena] Mounting shared folders...
[rena] -- /vagrant

vagrant sshで接続してみます。

$ vagrant ssh rena
Welcome to your Vagrant-built virtual machine.
[vagrant@rena ~]$ uname -a
Linux rena 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

おお、動いてますね。元気なお子さんですよ..。すみません。
後は普通に使えます。VMから抜けて、終了します。

vagrant@rena ~]$ exit
logout
Connection to 127.0.0.1 closed.
$ vagrant halt rena
[rena] Attempting graceful shutdown of VM...

いや〜、便利ですね。
今回はこの辺で。その他のVagrantトピックはまたの機会に。