PackerでCentOS7のVagrant Boxを作成。そしてboxのパッケージ化

概要

Packerを使ってVagrantのBoxを作成します。
また、作成したboxファイルをパッケージ化してみます。

環境

各バージョン

Virtual Box 4.3.14
Vagrant 1.6.3
Packer v0.6.1

作業マシンはMacOSXで行いました。

必要ソフトウェアのダウンロードとインストール

以下ソフトウェアのダウンロード及びインストールを行ってください。


Virual BoxとVagrantはdmgファイルが公開されていますのでそちらでインストールしてください。
Packerは公式サイトからzipファイルをダウンロードしてきて展開後、
全ファイルを/usr/local/bin/以下にコピーしてください。


Packerの設定ファイル

Chef実践入門 ~コードによるインフラ構成の自動化 (WEB+DB PRESS plus)
こちらの『5.3 Packerで開発環境のboxを作成する』を参考にさせていただきました。

本エントリー内の設定ファイルも上記書籍のサンプルを参考にさせていただいています。
tsunokawa/packer · GitHub

Packerの設定ファイルの準備

設定ファイルをgit cloneします。

$ git clone https://github.com/tsunokawa/packer.git


設定ファイルをcloneすると以下のファイルがダウンロードされます。

packer/
├── centos
   └── 7
       ├── base.sh
       ├── ks.cfg
       └── template.json


マシンイメージのビルド

設定ファイルをcloneしてビルドします。

$ cd packer/centos/7/
$ packer build template.json


表示例
このような表示になっているとビルドが完了しています。
途中VirtualBoxのウィンドウが開きますが、触らず放っておきましょう。

==> virtualbox-iso: Downloading or copying Guest additions
    virtualbox-iso: Downloading or copying: file:///Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso
==> virtualbox-iso: Downloading or copying ISO
    virtualbox-iso: Downloading or copying: http://ftp.iij.ad.jp/pub/linux/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso
(略)
Build 'virtualbox-iso' finished.

==> Builds finished. The artifacts of successful builds are:
--> virtualbox-iso: 'virtualbox' provider box: ./CentOS-7.0.1406-x86_64-ja.box


boxの登録

Packerで作ったboxをVagrantへ登録します。

$ vagrant box add CentOS-7.0.1406-x86_64-ja ./CentOS-7.0.1406-x86_64-ja.box --provider virtualbox


表示例
このような表示になり登録が完了します。

==> box: Adding box 'CentOS-7.0.1406-x86_64-ja' (v0) for provider: virtualbox
    box: Downloading: file:///Users/tsunokawa/CentOS-7.0.1406-x86_64-ja.box
==> box: Successfully added box 'CentOS-7.0.1406-x86_64-ja' (v0) for 'virtualbox'!


Vagrantfileの作成

作業用ディレクトリ作成後、vagrantコマンドでVagrantfileを作成します。

mkdir centos7
cd centos7
vagrant init CentOS-7.0.1406-x86_64-ja


表示例

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.


boxの起動

いよいよ起動です。

vagrant up


表示例
このような表示になり起動するはずです。

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'CentOS-7.0.1406-x86_64-ja'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7_default_1407471995704_56260
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/tsunokawa/centos7


接続

起動した仮想サーバーに接続してみます。

vagrant ssh


接続できるようになったと思います。

Last login: Thu Aug  7 00:00:01 2014 from 10.0.2.2
[vagrant@localhost ~]$ 


boxをパッケージ化

Packerを使えば簡単にVagrantの仮想マシンを作成することが出来ますが、
新しい仮想マシンを作成するのは手間です。
そこでboxをパッケージ化して再利用可能にします。

先程起動させた仮想サーバーを一旦停止させます。

vagrant halt


停止後、パッケージ化します。

vagrant package --output centos-7.0-1406.box

centos-7.0-1406.boxというファイル名でboxファイルが作成されます。
このファイルを社内などにある公開サーバーに保存しておくことで再利用が可能になります。

自作boxを追加

先程作成したboxを別ホストで起動させてみます。

vagrant box add centos7 http://repo.example.com/vagrant/centos/7/centos-7.0-1406.box

これで、centos-7.0-1406.boxというboxファイルがcentos7という名前でbox登録されます。

先程と同じように作業ディレクトリに移動してから

vagrant init centos7
vagrant up
vagrant ssh

とすることで起動&接続することが出来ます。