ちょっと話題の記事

Vagrant 1.8の新機能 Linked CloneとSnapshotを試してみた

2015.12.24

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ども、大瀧です。
週明けにリリースされたVagrant バージョン1.8の新機能、Linked CloneとSnapshotを試してみたのでレポートします。

先に結論から書いておきますが、どちらもVagrantを日々使っている方には是非常用して欲しい機能です!

Linked Cloneとは

Linked Cloneはデータセンター仮想化製品ではメジャーな、一つのマスターVMを元にして複数のクローンVMを作り出す機能です。普通にVMをコピーするのではなく、クローンVMはマスターVMの差分データのみを持つことから'Linked'という冠が付いています。VMの初回作成(vagrant up)時の時間が短縮されるのと、仮想ディスクのサイズが差分のみで済むメリットがあります。

設定方法

Virtualbox環境では、以下の設定をVagrantfileVagrant.configure(2) do |config|内に追加します。vagrant initで生成されるVagrantfileであれば、56行目あたりが良いでしょうか。

  # View the documentation for the provider you are using for more
  # information on available options.
  config.vm.provider "virtualbox" do |v|
    v.linked_clone = true
  end

これだけです。

Linked Clone有無の比較

では、vagrant uptimeコマンドに噛ませて実行時間を見てみます。BoxごとにマスターVMを持つことになるので、BoxのクローンVMを初めて起動するときにマスターVMの作成が自動で行われます。そのため、初回はLinked CloneなしのVM作成の時間とほとんど変わりません。

$ time 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: Importing base box 'debian/wheezy64'...
==> default: Cloning VM...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'debian/wheezy64' is up to date...
==> default: Setting the name of the VM: vagrant-wheezy_default_1450885564844_68484
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Installing rsync to the VM...
==> default: Rsyncing folder: /Users/ryuta/Desktop/vagrant-wheezy/ => /vagrant

real	0m48.665s
user	0m5.144s
sys	0m2.690s
$

もう一つ別のディレクトリで同じ内容のVagrantfileを用意してvagrant upを実行すると、2つ目のクローンVMが作成されます。

$ time vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Cloning VM...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'debian/wheezy64' is up to date...
==> default: Setting the name of the VM: vagrant-wheezy2_default_1450885650633_30824
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Installing rsync to the VM...
==> default: Rsyncing folder: /Users/ryuta/Desktop/vagrant-wheezy2/ => /vagrant

real	0m35.982s
user	0m5.026s
sys	0m2.508s
$

1つ目よりも10秒超短くなっているのがわかりますね!

ちなみにVirtualboxの管理画面では、マスターVMとクローン2つがそれぞれ確認できます。

vagrant18_01

仮想マシンのプロパティを見てみると、内部的には恒久的なスナップショットとしてクローンVMの仮想ディスクが扱われているように見えました。

vagrant18_02

なお、クローンVMとマスターVMを全て削除するときは注意が必要です。ドキュメントの[Note]にあるようにクローンVMを全て削除してもマスターVMは自動では削除されないので、Virtualbox側で削除する必要があります。 ただ、VagrantはBoxと既存のマスターVMをUUIDで紐付けるメタデータを持っているため、マスターVMを削除するときに以下のメタデータファイルmaster_idも削除する必要があるようです。

~/.vagrant.d/boxes/<Box名>/<Boxのバージョン>/virtualbox/master_id

cat ~/.vagrant.d/boxes/debian-VAGRANTSLASH-wheezy64/7.9.2/virtualbox/master_id
696d65af-4416-480e-a0a7-60949caaa7d5

これに気づかずに再度クローンVMを起動しようとすると、以下のエラーが発生しクローンVMの作成に失敗してしまいました。

$ 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", "696d65af-4416-480e-a0a7-60949caaa7d5", "--register", "--name", "temp_clone_1450885198814_47614"]

Stderr: VBoxManage: error: Could not find a registered machine with UUID {696d65af-4416-480e-a0a7-60949caaa7d5}
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
$

Snapshotとは

Snapshotも仮想化製品では著名な機能で、仮想マシンのその時点の状態を保存します。Vagrantではコマンド一発で実行できます。従来saharaプラグインで提供されていたのと同等のようです。

  • 単一スナップショット
    • vagrant snapshot push : スナップショット作成
    • vagrant snapshot pop : スナップショット復元
  • 複数(名前付き)スナップショット
    • vagrant snapshot save <スナップショット名> : スナップショット作成
    • vagrant snapshot restore <スナップショット名> : スナップショット復元
    • vagrant snapshot list : 名前付きスナップショットの一覧

試してみます。ホームディレクトリにファイルが無い状態でスナップショットを作成します。

$ vagrant ssh
Linux debian-wheezy 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u6 x86_64
(略)
vagrant@debian-wheezy:~$ ls
vagrant@debian-wheezy:~$ logout
Connection to 127.0.0.1 closed.

$ vagrant snapshot push
==> default: Snapshotting the machine as 'push_1450926609_4848'...
==> default: Snapshot saved! You can restore the snapshot at any time by
==> default: using `vagrant snapshot restore`. You can delete it using
==> default: `vagrant snapshot delete`.
$

では、おもむろにファイルを追加したあと、スナップショットを復元してみます。

$ vagrant ssh
Linux debian-wheezy 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u6 x86_64
(略)
vagrant@debian-wheezy:~$ touch testfile
vagrant@debian-wheezy:~$ ls
testfile
vagrant@debian-wheezy:~$ logout
Connection to 127.0.0.1 closed.
$ vagrant snapshot pop
==> default: Forcing shutdown of VM...
==> default: Restoring the snapshot 'push_1450926609_4848'...
==> default: Deleting the snapshot 'push_1450926609_4848'...
==> default: Snapshot deleted!
==> default: Checking if box 'debian/wheezy64' is up to date...
==> default: Resuming suspended VM...
==> 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: Machine booted and ready!
$

復元ということは、ファイル作成前に戻っているはずなので...

$ vagrant ssh
Linux debian-wheezy 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u6 x86_64
(略)
vagrant@debian-wheezy:~$ ls
vagrant@debian-wheezy:~$

ファイルが無くなっていることを確認できました!

まとめ

Linked Clone、Snapshotともに技術的にはあまり目新しさがないですが、Vagrantらしくシンプルな設定で実現できているのが良いですよね!どちらも開発や動作検証にとても向いている機能なのでぜひ使いこなしていきましょう!