[Vagrant & VirtualBox] vagrant box の update と 不要な box の削除
最新版への update と、使われていない box の削除方法。
[markdown]
以下の環境で確認しています。
“`prettyprinted
% vagrant -v
Vagrant 2.0.1
“`
## vagrant box を update したい
### 状況
– `ubuntu/trusty64` の最新版がありそう。
– 古いバージョンは消したい。
“`prettyprinted
% vagrant box list
bento/centos-6.7 (virtualbox, 2.2.7)
centos64 (virtualbox, 0)
miya0001/vccw (virtualbox, 2.19.0)
ubuntu/trusty64 (virtualbox, 20160323.0.0)
ubuntu/trusty64 (virtualbox, 20160621.0.0)
ubuntu/trusty64 (virtualbox, 20160627.0.0)
vccw-team/xenial64 (virtualbox, 20161209)
“`
### update
`vagrant box update –box
> * [vagrant box updateでboxファイルをアップグレードする](https://hnakamur.github.io/blog/2015/04/25/vagrant-box-update/)
“`prettyprinted
% vagrant box update –box ubuntu/trusty64
Checking for updates to ‘ubuntu/trusty64’
Latest installed version: 20160627.0.0
Version constraints: > 20160627.0.0
Provider: virtualbox
Updating ‘ubuntu/trusty64’ with provider ‘virtualbox’ from version
‘20160627.0.0’ to ‘20171115.1.2’…
Loading metadata for box ‘https://atlas.hashicorp.com/ubuntu/trusty64’
Adding box ‘ubuntu/trusty64’ (v20171115.1.2) for provider: virtualbox
Downloading: https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/20171115.1.2/providers/virtualbox.box
Successfully added box ‘ubuntu/trusty64’ (v20171115.1.2) for ‘virtualbox’!
“`
このようにアップデートされています。
“`prettyprinted
% vagrant box list
bento/centos-6.7 (virtualbox, 2.2.7)
centos64 (virtualbox, 0)
miya0001/vccw (virtualbox, 2.19.0)
ubuntu/trusty64 (virtualbox, 20160323.0.0)
ubuntu/trusty64 (virtualbox, 20160621.0.0)
ubuntu/trusty64 (virtualbox, 20160627.0.0)
ubuntu/trusty64 (virtualbox, 20171115.1.2)
vccw-team/xenial64 (virtualbox, 20161209)
“`
### prune
不要になった box を削除します。
`vagrant box prune` を実行します。
`-n` で dry-run します。
“`prettyprinted
% vagrant box prune
The following boxes will be kept…
bento/centos-6.7 (virtualbox, 2.2.7)
centos64 (virtualbox, 0)
miya0001/vccw (virtualbox, 2.19.0)
ubuntu/trusty64 (virtualbox, 20171115.1.2)
vccw-team/xenial64 (virtualbox, 20161209)
Checking for older boxes…
Removing box ‘ubuntu/trusty64’ (v20160323.0.0) with provider ‘virtualbox’…
Removing box ‘ubuntu/trusty64’ (v20160621.0.0) with provider ‘virtualbox’…
Removing box ‘ubuntu/trusty64’ (v20160627.0.0) with provider ‘virtualbox’…
“`
## 古い box を削除して良いかを調べる
### 状況
– `vccw-team/xenial64` の古いバージョンは不要ではないか?
“`prettyprinted
% vagrant box list
bento/centos-6.7 (virtualbox, 2.2.7)
centos64 (virtualbox, 0)
miya0001/vccw (virtualbox, 2.19.0)
ubuntu/trusty64 (virtualbox, 20171115.1.2)
vccw-team/xenial64 (virtualbox, 20161209)
vccw-team/xenial64 (virtualbox, 20170902)
“`
### prune コマンド実行時に自動チェックしてくれる
`vagrant box prune` を実行します。
削除対象の box が使われていると警告を表示してくれます。
“`prettyprinted
% vagrant box prune
The following boxes will be kept…
bento/centos-6.7 (virtualbox, 2.2.7)
centos64 (virtualbox, 0)
miya0001/vccw (virtualbox, 2.19.0)
ubuntu/trusty64 (virtualbox, 20171115.1.2)
vccw-team/xenial64 (virtualbox, 20170902)
Checking for older boxes…
Box ‘vccw-team/xenial64’ (v20161209) with provider ‘virtualbox’ appears
to still be in use by at least one Vagrant environment. Removing
the box could corrupt the environment. We recommend destroying
these environments first:
foo.dev (ID: xxxxx)
deadwood2.dev (ID: xxxxx)
Are you sure you want to remove this box? [y/N] n
“`
ひとまず該当プロジェクトを確認した方が良さそうです。
## そもそも不要な box はないのかを調べる
### 状況
– `centos64 ` は、そもそも不要ではないか?
“`prettyprinted
% vagrant box list
bento/centos-6.7 (virtualbox, 2.2.7)
centos64 (virtualbox, 0)
miya0001/vccw (virtualbox, 2.19.0)
ubuntu/trusty64 (virtualbox, 20171115.1.2)
vccw-team/xenial64 (virtualbox, 20161209)
vccw-team/xenial64 (virtualbox, 20170902)
“`
### find で設定を探す
Vagrantfile 内の `config.vm.box` に設定が書かれているはず。
これを `find` で検索します。
> * [使ってないVagrant Boxを削除する – Qiita](https://qiita.com/mochizukikotaro/items/52f4434c3f69c4ba1f54)
“`prettyprinted
% find ./projects_dir -name ‘Vagrantfile’ | xargs grep centos64
./projects_dir/VMs/Dotinstall/Vagrantfile: config.vm.box = “centos64”
./projects_dir/VMs/Dotinstall/Vagrantfile: # the file centos64.pp in the manifests_path directory.
“`
1ファイル見つかったようです。
[/markdown]