VagrantのSahara(sandboxモード)を試してみる

概要

VagrantのSaharaプラグインを使うと仮想マシンに対しての変更をいつでも巻き戻すことが出来ます。

動作検証用のマシンは様々なことを試していると
ふとまっさらな状態に戻したかったり、
ちょっと一時的にこのソフト試したいんだけど環境汚したくないんだよなー
みたいなことがしょっちゅうあります。

最近だとChefのレシピを試しに適用して気に入らなかったら元に戻したりとかですかね。
色々あると思います。

そんなときにはこのVagrantのsandboxモードの出番です。

ちょっとソフトをインストールして試した後、仮想マシンをまた元の状態に戻すことが簡単に出来ます。

実際にやってみるのが手っ取り早いです。

Saharaのインストール
vagrant plugin install sahara


表示例

Installing the 'sahara' plugin. This can take a few minutes...
Building nokogiri using packaged libraries.
Building libxml2-2.8.0 for nokogiri with the following patches applied:
     - 0001-Fix-parser-local-buffers-size-problems.patch
     - 0002-Fix-entities-local-buffers-size-problems.patch
     - 0003-Fix-an-error-in-previous-commit.patch
     - 0004-Fix-potential-out-of-bound-access.patch
     - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
     - 0006-Do-not-fetch-external-parsed-entities.patch
     - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
     - 0008-Improve-handling-of-xmlStopParser.patch
     - 0009-Fix-a-couple-of-return-without-value.patch
     - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
     - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

However, note that nokogiri does not necessarily support all versions
of libxml2.

For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************
Building libxslt-1.1.28 for nokogiri with the following patches applied:
     - 0001-Adding-doc-update-related-to-1.1.28.patch
     - 0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch
     - 0003-Initialize-pseudo-random-number-generator-with-curre.patch
     - 0004-EXSLT-function-str-replace-is-broken-as-is.patch
     - 0006-Fix-str-padding-to-work-with-UTF-8-strings.patch
     - 0007-Separate-function-for-predicate-matching-in-patterns.patch
     - 0008-Fix-direct-pattern-matching.patch
     - 0009-Fix-certain-patterns-with-predicates.patch
     - 0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch
     - 0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch
     - 0014-Fix-for-bug-436589.patch
     - 0015-Fix-mkdir-for-mingw.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxslt.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install
************************************************************************
Installed the plugin 'sahara (0.0.17)'!

無事インストールが完了しました。

sandboxモードを有効化

vagrant upした後、有効化します。

vagrant sandbox on


表示例

[default] Starting sandbox mode...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%…100%


試しにパッケージをインストールしてみる

vagrant sshして仮想マシンにログイン後、
flacというパッケージを試しにインストールしてみます。

このflacというパッケージはこの手順の参考にした
Chef実践入門 ~コードによるインフラ構成の自動化 (WEB+DB PRESS plus)
でもサンプルとして紹介されていたパッケージです。
オーディオコーデックのパッケージのようです。

========================================================================================================================
 Package                      Arch                      Version                           Repository               Size
========================================================================================================================
Installing:
 flac                         x86_64                    1.3.0-4.el7                       base                    566 k
Installing for dependencies:
 flac-libs                    x86_64                    1.3.0-4.el7                       base                    169 k
 libogg                       x86_64                    2:1.3.0-7.el7                     base                     24 k

Transaction Summary
========================================================================================================================
Install  1 Package (+2 Dependent packages)


インストールされました。

[root@localhost ~]# rpm -qa | grep flac
flac-1.3.0-4.el7.x86_64
flac-libs-1.3.0-4.el7.x86_64


[root@localhost ~]# rpm -qi flac-1.3.0-4.el7.x86_64
Name        : flac
Version     : 1.3.0
Release     : 4.el7
Architecture: x86_64
Install Date: Tue 12 Aug 2014 05:01:35 PM JST
Group       : Applications/Multimedia
Size        : 1588232
License     : BSD and GPLv2+ and GFDL
Signature   : RSA/SHA256, Fri 04 Jul 2014 10:21:23 AM JST, Key ID 24c6a8a7f4a80eb5
Source RPM  : flac-1.3.0-4.el7.src.rpm
Build Date  : Tue 10 Jun 2014 03:31:14 PM JST
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.xiph.org/flac/
Summary     : An encoder/decoder for the Free Lossless Audio Codec
Description :
FLAC stands for Free Lossless Audio Codec. Grossly oversimplified, FLAC
is similar to Ogg Vorbis, but lossless. The FLAC project consists of
the stream format, reference encoders and decoders in library form,
flac, a command-line program to encode and decode FLAC files, metaflac,
a command-line metadata editor for FLAC files and input plugins for
various music players.

This package contains the command-line tools and documentation.


ロールバックして元に戻す

先程インストールしたflacパッケージはオーディオコーデックでサーバーには不要と分かったので
ロールバックして先程のインストールを無かったことにします。

vagrant sandbox rollback


表示例

[default] Rolling back the virtual machine...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%…100%

これでロールバック完了です。
結構時間がかかります。
一度vagrant haltするなどして仮想マシンを停止してから行うと少し早く完了します。

もう一度ログインしてパッケージを見てみる

ロールバックが完了しましたので、
vagrant sshして仮想マシンにログイン後、
本当に先ほどのインストール作業が無かったことになっているか確認してみます。

[root@localhost ~]# rpm -qa | grep flac
[root@localhost ~]#

想定通りインストールされていません。
無かったことになっています。

コミットを行う

sandboxモードが有効な状態で必要なパッケージを入れた時はその状態を保ち続けたい。
そんな時もあります。
その時にはコミットを行うと保存され、ロールバックしても状態が保持され続けます。

例えばnmapをインストールしてその状態をコミットしてみます。

========================================================================================================================
 Package                      Arch                      Version                           Repository               Size
========================================================================================================================
Installing:
 nmap                         x86_64                    2:6.40-4.el7                      base                    3.9 M
Installing for dependencies:
 nmap-ncat                    x86_64                    2:6.40-4.el7                      base                    200 k

Transaction Summary
========================================================================================================================
Install  1 Package (+1 Dependent package)


インストールされました。

[root@localhost ~]# rpm -qa | grep nmap
nmap-ncat-6.40-4.el7.x86_64
nmap-6.40-4.el7.x86_64
[root@localhost ~]#


[root@localhost ~]# rpm -qi nmap-6.40-4.el7.x86_64

Name        : nmap
Epoch       : 2
Version     : 6.40
Release     : 4.el7
Architecture: x86_64
Install Date: Tue 12 Aug 2014 05:08:05 PM JST
Group       : Applications/System
Size        : 16916758
License     : GPLv2 and LGPLv2+ and GPLv2+ and BSD
Signature   : RSA/SHA256, Fri 04 Jul 2014 12:57:32 PM JST, Key ID 24c6a8a7f4a80eb5
Source RPM  : nmap-6.40-4.el7.src.rpm
Build Date  : Tue 10 Jun 2014 07:51:39 AM JST
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://nmap.org/
Summary     : Network exploration tool and security scanner
Description :
Nmap is a utility for network exploration or security auditing.  It supports
ping scanning (determine which hosts are up), many port scanning techniques
(determine what services the hosts are offering), and TCP/IP fingerprinting
(remote host operating system identification). Nmap also offers flexible target
and port specification, decoy scanning, determination of TCP sequence
predictability characteristics, reverse-identd scanning, and more. In addition
to the classic command-line nmap executable, the Nmap suite includes a flexible
data transfer, redirection, and debugging tool (netcat utility ncat), a utility
for comparing scan results (ndiff), and a packet generation and response analysis
tool (nping).

[root@localhost ~]#


何もせずにロールバックするとこれも無かったことになってしまいます。
今度はこのnmapをインストールした状態を保存したいのでコミットします。

vagrant sandbox commit


表示例

[default] Committing the virtual machine...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%…100%

コミットが完了しました。
これも結構時間がかかります。
一度vagrant haltするなどして仮想マシンを停止してから行うと少し早く完了します。

コミット後、ロールバックを行ってみる

コミットが完了したらロールバックしてみます。
ロールバック完了後、nmapがインストールされた状態なら成功です。

vagrant sandbox rollback


表示例

[default] Rolling back the virtual machine...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

ロールバックが完了しました。


ログインしてnmapがインストールされたままか確認してみます。

[root@localhost ~]# rpm -qa | grep nmap
nmap-ncat-6.40-4.el7.x86_64
nmap-6.40-4.el7.x86_64
[root@localhost ~]#

インストールされたまま状態が保持されています!

sanboxモードを無効化する

sandboxモードが不要になったら以下のコマンドで無効化出来ます。

vagrant sandbox off


表示例

[default] Stopping sandbox mode...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%…100%


sandboxモードの状態を確認

今現在sandboxモードがon/offなのか確認出来ます。

vagrant sandbox status


表示例

[default] Sandbox mode is off


ちなみに

sandboxモードを無効化した今、もう一度ログインしてみると先程コミットしたnmapはどうなっているだろう。

[root@localhost ~]# rpm -qa | grep nmap
nmap-ncat-6.40-4.el7.x86_64
nmap-6.40-4.el7.x86_64
[root@localhost ~]#

ちゃんとインストールされたままです。
当たり前か。