Vagrant 1.8 で追加された Snapshot 機能を触ってみた

今朝、Vagrant + Ansible Local Provisionerで遊んでた。

bufferings.hatenablog.com

最後に「snapshot機能が追加されたんだったっけー?」とか思って気になったので調べた。

1.8で追加されてた

結論から言うと、1.8で追加されてた。

vagrant snapshot - Command-Line Interface - Vagrant by HashiCorp

クラスメソッドさんの紹介記事を見てsnapshot機能が印象に残ってたんだな。

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

snapshotコマンド

push&popとsave&restoreの2系統ある。

push & pop

名前から考えたらスタックかな。

vagrant snapshot push
vagrant snapshot pop

save & restore

こっちは名前をつけて操作できるみたい。

vagrant snapshot save
vagrant snapshot restore

deleteとlist

それとdeleteとlistがある。

vagrant snapshot delete
vagrant snapshot list

この6つだけなんだね。シンプル。

触ってみる

push & pop

VMを起動してない方が速いよってことなので。

$vagrant halt
==> default: Attempting graceful shutdown of VM...

なにもない状態でlist

$ vagrant snapshot list
==> default: No snapshots have been taken yet!
    default: You can take a snapshot using `vagrant snapshot save`. Note that
    default: not all providers support this yet. Once a snapshot is taken, you
    default: can list them using this command, and use commands such as
    default: `vagrant snapshot restore` to go back to a certain snapshot.

で、push

$vagrant snapshot push
==> default: Snapshotting the machine as 'push_1454741090_2679'...
==> 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`.

push_1454741090_2679って名前が振られたのかな。listしてみよう。

$vagrant snapshot list
push_1454741090_2679

popしてみよう。

$vagrant snapshot pop
==> default: Restoring the snapshot 'push_1454741090_2679'...
==> default: Deleting the snapshot 'push_1454741090_2679'...
==> default: Snapshot deleted!
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: A newer version of the box 'ubuntu/trusty64' is available! You currently

リストアして、削除してるんだね。popだからね。 で、VM起動してもうた。haltしとこう。

$vagrant halt
==> default: Attempting graceful shutdown of VM...

何もない状態でpop

何もない状態でpopしてみる。

$vagrant snapshot pop
==> default: No pushed snapshot found!
==> default:
==> default: Use `vagrant snapshot push` to push a snapshot to restore to.

うん。そうだよね。

複数回push

複数回pushしたらどうなるんだろう?

$vagrant snapshot push
==> default: Snapshotting the machine as 'push_1454741490_2367'...
==> 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 snapshot list
push_1454741490_2367
$
$
$
$vagrant snapshot push
==> default: Snapshotting the machine as 'push_1454741510_1043'...
==> 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 snapshot list
push_1454741490_2367
push_1454741510_1043
$

おー。pushされた。で、popだね。

$vagrant snapshot pop
==> default: Restoring the snapshot 'push_1454741510_1043'...
==> default: Deleting the snapshot 'push_1454741510_1043'...
==> default: Snapshot deleted!
==> default: Checking if box 'ubuntu/trusty64' is up to date...
.
.
.
$vagrant snapshot list
push_1454741490_2367

ふむ。名前の通りだね。

save & restore

じゃ、次行こう。 とその前に、haltして、さっきのpushの残りのsnapshotは一回削除しとこう。

$vagrant halt
==> default: Attempting graceful shutdown of VM...
$vagrant snapshot delete push_1454741490_2367
==> default: Deleting the snapshot 'push_1454741490_2367'...
==> default: Snapshot deleted!

よし。save。

$vagrant snapshot save sample1
==> default: Snapshotting the machine as 'sample1'...
==> 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 snapshot list
sample1
$vagrant snapshot save sample2
==> default: Snapshotting the machine as 'sample2'...
==> 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 snapshot list
sample1
sample2
$vagrant snapshot save sample3
==> default: Snapshotting the machine as 'sample3'...
==> 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 snapshot list
sample1
sample2
sample3

ふむふむ。そういえばリストは古いほうが上に表示されるんかな。で、リストア。sample2をリストアしてみよう。

$vagrant snapshot restore sample2
==> default: Restoring the snapshot 'sample2'...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: A newer version of the box 'ubuntu/trusty64' is available! You currently
.
.
.

起動した。リストしてみると。

$vagrant snapshot list
sample1
sample2
sample3

うん。削除されてない。

popしたらどうなるんだろう?

と思って。ここでpop。

$vagrant halt
==> default: Attempting graceful shutdown of VM...
$
$vagrant snapshot pop
==> default: No pushed snapshot found!
==> default:
==> default: Use `vagrant snapshot push` to push a snapshot to restore to.

ほほー。pushされたのがないよって言われた。pushされたsnapshotかどうかはどうやって判断してるんだろう?名前?

pushされたっぽい名前をsaveでつけてみよう

どうなるかな?

$vagrant snapshot save push_1234567890_9999
==> default: Snapshotting the machine as 'push_1234567890_9999'...
==> 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 snapshot list
push_1234567890_9999
sample1
sample2
sample3

一番上にきてる。これ、もしか名前の昇順なのか?あとで確認しよう。今はこれでpopしたらどうなるかってのを先にやる。

$vagrant snapshot pop
==> default: Restoring the snapshot 'push_1234567890_9999'...
==> default: Deleting the snapshot 'push_1234567890_9999'...
==> default: Snapshot deleted!
==> default: Checking if box 'ubuntu/trusty64' is up to date...
.
.
.
$vagrant snapshot list
sample1
sample2
sample3

いけるんか・・・。プレフィックスかな?じゃあpushって名前だけつけてみよっと。

$vagrant halt
==> default: Attempting graceful shutdown of VM...
$
$
$vagrant snapshot save push
==> default: Snapshotting the machine as 'push'...
==> 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 snapshot list
push
sample1
sample2
sample3

からのpop。wktk

$vagrant snapshot pop
==> default: No pushed snapshot found!
==> default:
==> default: Use `vagrant snapshot push` to push a snapshot to restore to.

ふーん。数字の形式とかも見てるのかな?これぐらいでいっか。

リストの順番

$vagrant snapshot list
push
sample1
sample2
sample3
$
$vagrant snapshot save sample0
==> default: Snapshotting the machine as 'sample0'...
==> 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 snapshot list
push
sample0
sample1
sample2
sample3

スナップショット作った順番とかじゃなくて、単純に文字列の昇順か。

混ぜるな危険って書いてある

ところで公式ドキュメントに「混ぜるな危険」って書いてある

Warning: If you are using push and pop, avoid using save and restore which are unsafe to mix.

ま、別々に使ったほうが良いってことね。

まとめ

push & popの方が楽ちんだけど、popしたあとにsnapshotが削除されてるのを忘れてて、戻そうとして(ノД`)シクシクってなる未来も見えそうだから、save & restoreつかおうかな。

面白かった。