Vagrantfile で Linked Clone をデフォルトにする
はじめに
Vagrant 1.8.0 の便利で快適な新機能に Linked Clone 対応があります。しかしながら、vagrant init などの一連のコマンドラインオプションに Linked Clone の On/Off を切り替えるオプションはありません。この記事では力業ながら Linked Clone をデフォルトにするための方法を纏めます。
本環境
Mac OS X: 10.11.04
Vagrant: 1.8.1
Vagrant は以下からダウンロードし、インストールする。
https://www.vagrantup.com/downloads.html
小ネタ
vagrant init で生成される Vagrantfile は Mac OS X の場合、/opt/vagrant/embedded/gems/gems/vagrant-1.8.1/templates/commands/init/Vagrantfile.erb が元になります。オリジナルの Vagrantfile.erb は以下のようになります。<%= %> や <% -%> がプレースホルダとなりまっています。
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "<%= box_name %>"
<% if box_url -%>
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "<%= box_url %>"
<% else -%>
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
<% end -%>
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
Ruby に慣れていたり、汎用的に作りたい場合には Vagrant Plugin や <% -%> と環境変数などで、Linked Clone を有効にする
vb.linked_clone = true
を Vagrantfile に挿入する仕込みを実装できればキレイなのですが力量不足と労力が見合わないので、シンプルに Vagrantfile.erb に vb.linked_clone = true を直接組み入れてしまいます。編集後の Vagrantfile.erb は以下になります。
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "<%= box_name %>"
<% if box_url -%>
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "<%= box_url %>"
<% else -%>
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
<% end -%>
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
vb.linked_clone = true
end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
注意点 1
まだ、Vagrant 1.8.1+ がリリースされていないので不明ですが、新しいバージョンをインストールすると上書きされると思われるので、バージョンアップの度に Vagrantfile.erb の編集が必要かも知れません。
注意点 2
これは Vagrantfile.erb にまつわる話ではありませんが、Linked Clone 利用していながら、Master VM を消してしまった場合の対処になります。Linked Clone の元ネタを消してしまった状態で vagrant up を実行すると以下のように、指定された UUID を持つ仮想マシンが見つからない、というエラーがでます。
% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Preparing master VM for linked clones...
default: This is a one time operation. Once the master VM is prepared,
default: it will be used as a base for linked clones, making the creation
default: of new VMs take milliseconds on a modern system.
==> default: Cloning VM...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["clonevm", "ac5dd930-a96b-4867-aa02-29350c3d9909", "--register", "--name", "temp_clone_1461043248433_62617"]
Stderr: VBoxManage: error: Could not find a registered machine with UUID {ac5dd930-a96b-4867-aa02-29350c3d9909}
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(pszSrcName).raw(), srcMachine.asOutParam())" at line 431 of file VBoxManageMisc.cpp
この UUID は、各 Box に記録される master_id というファイルに記載されています。vmware/photon という Box であれば ~/.vagrant.d//boxes/vmware-VAGRANTSLASH-photon/1.1.0/virtualbox/master_id に保存されています。Master VM を誤って消した場合は、この master_id ファイルを削除すれば上記エラーを回避し、Master VM のクローンがきちんと行われます。