開発用VMのパッケージングとデプロイを簡略化してくれるVagrantのメモです。
紹介記事:
例:
config.vm.provider "virtualbox" do |vb| vb.name = "t3_precise64_demo" # Don't boot with headless mode vb.gui = true # Use VBoxManage to customize the VM. For example to change memory: vb.customize [ "modifyvm", :id, "--memory", "2048", "--vram", "64", "--cpus", "2", "--audio", "none", "--clipboard", "bidirectional", "--usb", "off", "--usbehci", "off", "--nic1", "nat", "--nictype1", "82540EM", "--cableconnected1", "on", "--nic2", "hostonly", "--nictype2", "82540EM", "--cableconnected2", "on", "--hostonlyadapter2", "VirtualBox Host-Only Ethernet Adapter", ] end
Ubuntu 12.04 LTS 64bit版 (2014-05時点では既に 14系列でLTSが始まってて今更ではありますが・・・) でboxを作成してみたのでメモ。
2014-05に試した環境:
Host: Win7SP1 64bit 日本語版 VirtualBox: 4.3.10 Vagrant: 1.5.4
Vagrant公式:
これのChapter 7 が、Ubuntu 13でのVirtualBoxでのbox作成手順の解説になってます。
これらの参考資料からつぎはぎして、ポイントだけ抜書きするとこんな流れになりました。
初期のVagrantはVirtualBoxのマシンをexportしたのをtarにまとめてただけだったようですが、複数のproviderをサポートするようになり、OVAに近い形で、providerごとのVMイメージ+Vagrantが使うmetadata.jsonをtarで固めたファイルになったようです。OVA, OVFと同一ではありませんが、それと似た構成にはなってるそうです。
"Creating Development Environments With Vagrant"版を見ながら作成:
今回のUbuntu 12.04 LTSだと、デフォルトはSELinuxはインストールされず、Firewall(ufw)もinactiveになってるので、特に設定変更は必要ない。
CentOSだと・・・
実際はこれを使うことは無いけど、Vagrantのboxを作るときのお作法らしい。Vagrantの公式ドキュメントにも載ってる。多分こうしておけば、どんなbox使っても、rootになる時に困らないよね、という意図か。
$ sudo su - # passwd ("vagrant"に設定)
vagrant initとかでVM設定のMACアドレスが変化する場合がある。最近のLinuxだと"/etc/udev/rules.d/70-persistent-net.rules" というファイルにどのMACアドレスがどのethNか、というのが自動保存されて永続化されてしまうため、MACアドレスが変わるたびにethNが増えていってしまい、設定作業の手間が増えてしまう。
そこで、以下のように "70-persistent-net.rules" を "/dev/null" のシンボリックリンクとして、自動保存しようとしても保存されず、読み出すことも出来ないようにしておく。
$ cd /etc/udev/rules.d/ $ sudo mv 70-persistent-net.rules .70-persistent-net.rules $ sudo ln -s /dev/null 70-persistent-net.rules
・"admin"グループを新規作成して、"vagrant"ユーザを追加する。(Vagrant公式のprecise64等でのお作法。ただし、最終的にvagrantユーザがパスワード無しでsudo出来るようになっていれば良いので、必ずしもこのグループ設定を行う必要はない。つまり、Vagrantは"admin"グループに依存していない)
$ sudo groupadd admin $ sudo usermod -a -G admin vagrant
・sudoersファイルを調整。
$ sudo visudo
調整ポイント1:
Defaults env_keep+="SSH_AUTH_SOCK"
ファイルの先頭くらいに追加するらしい。SSHポートフォワーディング等をsudo越しに使った時に、SSHエージェント側で接続済みのコネクションを使えるよう、環境変数を引き継げるよう設定。("Creating Development Environments With Vagrant"で指示されてる設定。Vagrant公式ドキュメントや他の参考資料では、特にこの設定は指定されていない)
調整ポイント2:(全参考資料共通)
%admin ALL=(ALL) NOPASSWD: ALL
vagrant ALL=NOPASSWD:ALL
調整ポイント3:(全参考資料共通)
Default requiretty
→入っていれば、コメントアウトしておく。もし無ければ、デフォルトoffなので特に弄る必要はない。
$ mkdir ~/.ssh $ chmod 700 ~/.ssh $ wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys
sudo vi /etc/ssh/sshd_config
→以下の行を設定。(デフォルトはYESなので、もし設定がなければ明示的に追記する必要あり。)
UseDNS no
参考:
"Creating Development Environments With Vagrant"版:
$ sudo apt-get update $ sudo apt-get install linux-headers-$(uname -r) build-essential
Vagrantの公式ドキュメントから:
$ sudo apt-get install linux-headers-generic build-essential dkms
最終的に、kernelのヘッダファイルと、build-essential, dkms が入れば良いのだろう。
以降のビルド操作はVagrant公式ドキュメントの通り。
$ sudo mount /dev/cdrom /media/cdrom $ sudo sh /media/cdrom/VBoxLinuxAdditions.run
"Creating Development Environments With Vagrant"版:
$ rm -rf /tmp/* $ sudo apt-get clean
他にも、boxファイルサイズ削減のため未使用領域をゼロ埋めしたり、manページのキャッシュを削除するなど色々工夫があるらしい。
vagrant package --base VM名 -> vagrant package --base vagrant-ubuntu-raring ==> vagrant-ubuntu-raring: Exporting VM... ==> vagrant-ubuntu-raring: Compressing package to: c:/work/tmp/vagrant_ex/box/package.box
package.boxのファイルサイズは460MBほどになった。8GBの可変サイズでDisk作ったのでこんなもんかな?
"vagrant box add" してみる。
> cd c:\work\tmp\vagrant_ex\box > vagrant box add ubuntu-1204-lts-t1 package.box ==> box: Adding box 'ubuntu-1204-lts-t1' (v0) for provider: box: Downloading: file://c:/work/tmp/vagrant_ex/box/package.box box: Progress: 100% (Rate: 72.4M/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'ubuntu-1204-lts-t1' (v0) for 'virtualbox'! > vagrant box list -i hashicorp/precise32 (virtualbox, 1.0.0) hashicorp/precise64 (virtualbox, 1.1.0) ubuntu-1204-lts-t1 (virtualbox, 0) > cd c:\work\tmp\vagrant_ex > mkdir box_test > cd box_test > vagrant init ubuntu-1204-lts-t1 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. > vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'ubuntu-1204-lts-t1'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: box_test_default_1399303793682_85095 ==> 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: Warning: Remote connection disconnect. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => C:/work/tmp/vagrant_ex/box_test 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
最後に"Synced Folders"の "/vagrant" のmountでエラーが発生してはいるが、この時点で "vagrant ssh" でログイン出来るようになっていた。
"/vagrant" のmountエラーは、box作成時にインストールしたGuest Additionsのバージョンと、実際に動かすVirtualBoxのバージョンとでズレがあったりすると、発生する場合があるらしい。
一般的な対策としては以下のパターンが見つかった:
参考:
2014-05時点:VirtualBox 4.3.10 では、以下のissueの対策例に従う:
vagrant sshでログイン後:
$ sudo /etc/init.d/vboxadd setup $ sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions