「Packerを利用したUbuntu14.04 ServerのVagrant用Boxファイルの作成」|ただいまRubyの修行中
はじめに
Ubuntu 14.04 Serverが公開されたので新しい環境を作成することにしました。
まだ、適当なUbuntu 14.04 ServerのBoxファイルが無いので今後のために自作する方法を習得しておきます。
Packerを利用する方法を探していたら必要な設定ファイル一式を公開しているサイトを見つけたので、有難く利用させて頂きます。
参照サイト
packer - Ubuntu 14.04 を Vagrant に準備する - Qiita
時雨堂さんのPackerテンプレート
shiguredo/packer-templates · GitHub
概要
作業環境
Ubuntu 14.04 Desktop 64bit |
VirtualBox 4.3.12r93733 |
Vagrant 1.6.2 |
Packer v0.6.0 |
手順概要
- VirtualBoxのインストール
- Vagrantのインストール
- Packerのインストール
- Packerテンプレートの取得
- Boxファイルの生成
- 生成したBoxファイルで仮想環境の構築とssh接続テスト
1.VirtualBoxのインストール
公式サイトからVirtualBoxのインストーラーをダウンロードする。
Downloads – Oracle VM VirtualBox
(ファイル「virtualbox-4.3_4.3.10-93012~Ubuntu~raring_amd64.deb」)
stupiddog@pc001:~$ dpkg -i ~/Downloads/virtualbox-4.3_4.3.12-93733~Ubuntu~raring_amd64.deb ... stupiddog@pc001:~$ vboxmanage -v 4.3.12r93733
2.Vagrantのインストール
公式サイトからVagrantのインストーラーをダウンロードする。
Download Vagrant - Vagrant
(ファイル「vagrant_1.6.2_x86_64.deb」)
stupiddog@pc001:~$ sudo dpkg -i ~/Downloads/vagrant_1.6.2_x86_64.deb ... stupiddog@pc001:~$ vagrant -v Vagrant 1.6.2
3.Packerのインストール
公式サイトからPackerのパッケージをダウンロードする。
Downloads - Packer
(ファイル「0.6.0_linux_amd64.zip」)
公式サイトのドキュメントに従ってファイルを展開しパスを通します。
Install Packer - Packer
今回はユーザーのホームディレクトリへ配置しました。
stupiddog@pc001:~$ unzip ~/Downloads/0.6.0_linux_amd64.zip -d ~/packer ... stupiddog@pc001:~$ echo 'export "$HOME/packer:$PATH"' >> ~/.bashrc stupiddog@pc001:~$ source ~/.bashrc stupiddog@pc001:~$ packer -v Packer v0.6.0
4.Packerテンプレートの取得
Githubからファイルを取得(clone)する
stupiddog@pc001:~$ git clone https://github.com/shiguredo/packer-templates.git ~/packer-templates Cloning into '/home/stupiddog/packer-templates'... remote: Reusing existing pack: 133, done. remote: Total 133 (delta 0), reused 0 (delta 0) Receiving objects: 100% (133/133), 21.33 KiB | 0 bytes/s, done. Resolving deltas: 100% (55/55), done. Checking connectivity... done.
5.Boxファイルの生成
取得したPackerテンプレートの中にはバージョン毎にファイルが分けられています。
テンプレートは、VirtualBox以外に VMWareにも対応していますが今回は、VirtualBoxのBoxイメージのみを生成します。
Packerコマンドのオプションとして、-only=virtualbox-isoを指定して実行します。
stupiddog@pc001:~$ cd ~/packer-templates/ubuntu-14.04 stupiddog@pc001:~$ packer build -only=virtualbox-iso template.json stupiddog@pc001:~/packer-templates/ubuntu-14.04$ packer build -only=virtualbox-iso template.json virtualbox-iso output will be in this color. ==> virtualbox-iso: Downloading or copying Guest additions checksums virtualbox-iso: Downloading or copying: http://download.virtualbox.org/virtualbox/4.3.12/SHA256SUMS ==> virtualbox-iso: Downloading or copying Guest additions virtualbox-iso: Downloading or copying: http://download.virtualbox.org/virtualbox/4.3.12/VBoxGuestAdditions_4.3.12.iso ... virtualbox-iso (vagrant): Compressing: metadata.json virtualbox-iso (vagrant): Compressing: packer-virtualbox-iso-disk1.vmdk Build 'virtualbox-iso' finished. ==> Builds finished. The artifacts of successful builds are: --> virtualbox-iso: 'virtualbox' provider box: ubuntu-14-04-x64-virtualbox.box drwxrwxr-x 5 stupiddog stupiddog 4096 5月 21 22:28 ./ drwxrwxr-x 9 stupiddog stupiddog 4096 5月 21 20:46 ../ drwxrwxr-x 2 stupiddog stupiddog 4096 5月 21 20:46 http/ drwxr-xr-x 2 stupiddog stupiddog 4096 5月 21 21:07 packer_cache/ drwxrwxr-x 2 stupiddog stupiddog 4096 5月 21 20:46 scripts/ -rw-rw-r-- 1 stupiddog stupiddog 4108 5月 21 20:46 template.json -rw-rw-r-- 1 stupiddog stupiddog 511869354 5月 21 22:28 ubuntu-14-04-x64-virtualbox.box
Ubuntu14.04 ServerのISOイメージを公式サイトからダウンロードし、エラーが無ければ生成されたBoxファイルがカレントディレクトリに作成されます。
(ファイル「ubuntu-14-04-x64-virtualbox.box」)
6.生成したBoxファイルで仮想環境の構築とssh接続テスト
作業ディレクトリ「~/vm/a001」を作成し、Vagrantfileを下記の内容で配置してvagrant upコマンドを実行します。
仮想環境用のディレクトリを作成する
管理用ディレクトリを作成して、そこをカレントディレクトリとします。
stupiddog@pc001:~$ mkdir -p ~/vm/a001 stupiddog@pc001:~$ cd ~/vm/a001 stupiddog@pc001:~/vm/a001$ tree ~/vm/a001 /home/stupiddog/vm/a001 └── vagrantfile 0 directories, 1 file
Vagrantfileを作成する
管理用ディレクトリに以下の内容で「vagrantfile」ファイルを作成します。
VAGRANT_API_VERSION = "2" Vagrant.configure(VAGRANT_API_VERSION) do |config| config.vm.box = "ubuntu1404" config.vm.box_url = "~/packer-tamplates/ubuntu-14-04-x64-virtualbox.box" config.vm.hostname = "a001.local" config.vm.network "private_natwork", ip:"192.168.100.100" end
仮想環境を構築する
stupiddog@pc001:~/vm/a001$ cat vagrantfile VAGRANT_API_VERSION = "2" Vagrant.configure(VAGRANT_API_VERSION) do |config| config.vm.box = "ubuntu1404" config.vm.box_url = "~/packer-tamplates/ubuntu-14-04-x64-virtualbox.box" config.vm.hostname = "a001.local" config.vm.network "private_network", ip:"192.168.100.100" end stupiddog@pc001:~/vm/a001$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'ubuntu1404'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: a001_default_1400689238903_18288 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> 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: Setting hostname... ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => /home/stupiddog/vm/a001 stupiddog@pc001:~/vm/a001$ vagrant ssh Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64) * Documentation: https://help.ubuntu.com/ Last login: Mon May 19 10:27:04 2014 from 10.0.2.2 vagrant@a001:~$