[Vagrant & VirtualBox] Vagrant: package, plugin

続きです。

[markdown]

> * [Vagrant: network, synced_folder, provisioning | deadwood](https://www.d-wood.com/blog/2013/09/20_4691.html)

## 独自のBoxを作成する

### vagrant package

今の状態の仮想マシンからBox(テンプレート)を作成する。

“`
% vagrant package
[default] Attempting graceful shutdown of VM…
[default] Clearing any previously set forwarded ports…
[default] Creating temporary directory for export…
[default] Exporting VM…
[default] Compressing package to: /Users/myuser/projects/VB_CentOS2/package.box
% ls
Vagrantfile package.box provision.sh
% vagrant box add my_box package.box
Downloading or copying the box…
Extracting box…te: 10.5M/s, Estimated time remaining: 0:00:01)
Successfully added box ‘my_box’ with provider ‘virtualbox’!
% ls ~/.vagrant.d/boxes
centos64/ my_box/ precise32/
% rm package.box
remove package.box? y
“`

Box(テンプレート)が作成できました。

> * [Vagrant: package で独自の Box を作成する | deadwood](https://www.d-wood.com/blog/2014/10/09_7007.html)

### vagrant box remove

不要になったBoxを削除する。

“`
% vagrant box list
centos64 (virtualbox)
my_box (virtualbox)
precise32 (virtualbox)
% vagrant box remove my_box
Removing box ‘my_box’ with provider ‘virtualbox’…
% vagrant box list
centos64 (virtualbox)
precise32 (virtualbox)
“`

## pluginを利用する

Sandboxモードを有効にする sahara をインストールしてみる。

“`
% vagrant plugin -h
Usage: vagrant plugin []
Available subcommands:
install
license
list
uninstall
update
For help on any individual command run `vagrant plugin COMMAND -h`
% vagrant plugin install sahara
Installing the ‘sahara’ plugin. This can take a few minutes…
Installed the plugin ‘sahara (0.0.15)’!
“`

### vagrant sandbox on

sandbox モードを on

“`
% vagrant sandbox on
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
% vagrant sandbox status
[default] Sandbox mode is on
“`

### vagrant sandbox rollback

作業後、ロールバックで取り消します。

“`
% vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 20 05:54:18 2013 from 10.0.2.2
vagrant@precise32:~$ touch testfile
vagrant@precise32:~$ ls
postinstall.sh testfile
vagrant@precise32:~$ exit
logout
Connection to 127.0.0.1 closed.
% vagrant sandbox rollback
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
% vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 20 05:54:18 2013 from 10.0.2.2
vagrant@precise32:~$ ls
postinstall.sh
“`

### vagrant sandbox commit

sandbox での作業を確定(commit)させる。
ただし、仮想マシンを動かしたままだと時間がかかるため、事前に vagrant suspend などで止めておく。

“`
vagrant@precise32:~$ touch commitfile
vagrant@precise32:~$ ls
commitfile postinstall.sh
vagrant@precise32:~$ exit
logout
Connection to 127.0.0.1 closed.
% vagrant suspend
[default] Saving VM state and suspending execution…
% vagrant sandbox commit
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
“`

### vagrant sandbox off

sandbox モードを終了する。
上記と同じ理由で、仮想マシンを止めてから実行する。

“`
% vagrant sandbox off
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
% vagrant sandbox status
[default] Sandbox mode is off
“`

> * [VagrantをPluginで拡張する | Ryuzee.com](http://www.ryuzee.com/contents/blog/4310)
> * [僕が使っている vagrant plugins – Hack like a rolling stone](http://tk0miya.hatenablog.com/entry/2013/03/29/190116)

[/markdown]