Vagrantにお勧めしたいプラグイン

2013年12月07日 21時52分

201312072152VVAW00.png

はじめに

Vagrantは、開発者にとって仮想マシンを簡単に作成できる仮想化支援ツールとして、多くの開発者から支持されています。今回は、デフォルトでも便利なVagrantをさらに便利に使えるプラグインについて解説します。

前提条件

作業の前提条件は、下記のとおりです。

ソフトウエアバージョン
OSWindows 7 Ultimate 32bit
CygwinSetup Version 2.831
VirtualBox4.3.4 r91027
Vagrant1.3.5

お勧めプラグイン

Vagrantを使用する上で、お勧めしたいプラグインは、下記のとおりです。

#プラグイン名機能
1vagrant-vbguestVagrant BoxにVirtualBoxゲストアドインをインストールします。
2vagrant-reloadプロビジョニング中に、Boxを再起動します。
3vagrant-omnibusBoxにChef-Soloをインストールします。
4vagrant-global-statusBoxの動作状況を表示します。

vagrant-vbguest

vagrant-vbguestは、BoxにインストールされたVirtualBoxゲストアドインのバージョンアップを自動で行えるプラグインです。

VirtualBoxのバージョンを上げると、以前のバージョンで作成したBoxをvagrant upコマンドで起動する際に、VirtualBoxゲストアドインのバージョンが違うという警告が表示されていまいます。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.18
VirtualBox Version: 4.3
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
201312072152VVAW01.png

プラグインをインストールすると、Boxにインストールされているゲストアドインのバージ>ョンを調べて、自動的にアップデートしてくれます。もちろん、Boxにゲストアドインがインストールされていない場合でも、自動でインストールしてくれます。

$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.10.0)'!
201312072152VVAW02.png

インストール後、Boxを起動するとゲストアドインがバージョンアップできます。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
GuestAdditions versions on your host (4.3.4) and guest (4.2.18) do not match.
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Including mirror: ftp.iij.ad.jp
Including mirror: www.ftp.ne.jp
Including mirror: ftp.jaist.ac.jp
Including mirror: ftp.nara.wide.ad.jp
Including mirror: ftp.tsukuba.wide.ad.jp
Including mirror: mirror.fairway.ne.jp
Including mirror: ftp.riken.jp
 * base: ftp.iij.ad.jp
 Including mirror: ftp.iij.ad.jp
 Including mirror: www.ftp.ne.jp
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 4.3.4 - guest version is 4.2.18
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.4 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.2.18 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[  OK  ]
Building the shared folder support module[  OK  ]
Building the OpenGL support module[  OK  ]
Doing non-kernel setup of the Guest Additions[  OK  ]
You should restart your guest to make sure the new modules are actually used

Installing the Window System drivers[失敗]
(Could not find the X.Org or XFree86 Window System.)
An error occurred during installation of VirtualBox Guest Additions 4.3.4. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
201312072152VVAW03.png
201312072152VVAW04.png

Boxを停止して、もう一度起動するとVirtualBoxのバージョンに一致するゲストアドインがインストールされているため、ゲストアドインのインストールは行われません。

$ vagrant halt
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
GuestAdditions 4.3.4 running --- OK.
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
201312072152VVAW05.png

vagrant-reload

vagrant-reloadは、Boxのプロビジョニング中に、Boxの再起動を行うプラグインです。

起動したBoxを再起動するコマンドとしては、reloadコマンドが利用できます。

$ vagrant reload
[default] Attempting graceful shutdown of VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
GuestAdditions 4.3.4 running --- OK.
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
201312072152VVAW06.png

プラグインをインストールすると、Boxの起動中に再起動を行うことができます。

$ vagrant plugin install vagrant-reload
Installing the 'vagrant-reload' plugin. This can take a few minutes...
Installed the plugin 'vagrant-reload (0.0.1)'!
201312072152VVAW07.png

次のように、シェルプロビジョニングの後、システムの再起動が必要なった場合、再起動を実行することができます。

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # BOX名
  config.vm.box = "test-box2"

  # BOXのダウンロードURL
  config.vm.box_url = "file://d:/temp/test-box2.box"

  # ブリッジネットワークの指定
  config.vm.network :public_network

  # シェルプロビジョニングの実行
  config.vm.provision :shell, inline: <<-EOC
    service rsyslog stop
    cp /dev/null /var/log/messages
    service rsyslog start
  EOC

  # Box再起動
  config.vm.provision :reload
end

Vagrant 1.2までは、up実行時に毎回プロビジョニングが行われていましたが、1.3系では--provisionを付けないと、起動時にプロビジョニングを実行しない仕様に変わっています。ただし、新規に追加するBoxの場合、最初の起動時にプロビジョニングが行われますので、--provisionオプションの指定は不要です。

$ vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
GuestAdditions 4.3.4 running --- OK.
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] Running provisioner: shell...
[default] Running: inline script
システムロガーを停止中: [  OK  ]
システムロガーを起動中: [  OK  ]
[default] Running provisioner: reload...
[default] Attempting graceful shutdown of VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
GuestAdditions 4.3.4 running --- OK.
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
201312072152VVAW08.png
201312072152VVAW09.png

vagrant-omnibus

vagrant-omnibusは、vagrant-vbguestと同様に、BoxにChef-Soloがインストールします。

Chef-SoloによるBoxプロビジョニングで用いるため、詳細は後日アップします。

vagrant-global-status

vagrant-global-statusは、起動中のBoxの状態(停止、起動中)を表示するプラグインです。

Vagrantには、Boxの状態を表示するコマンドとしてstatusコマンドがありますが、global-statusは、全てのBoxの状態を表示できます。

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
201312072152VVAW10.png
$ vagrant plugin install vagrant-global-status
Installing the 'vagrant-global-status' plugin. This can take a few minutes...
Installed the plugin 'vagrant-global-status (0.1.2)'!
201312072152VVAW11.png

実行すると、Boxの状態を表示してくれます。
ただし、表示されるのはプラグインをインストールした後に起動、停止されたBoxですので、インストール直後に実行しても何も表示されません。なので、Boxを一旦停止し、再度起動します。

$ vagrant halt
$ vagrant up
$ vagrant global-status -a
D:/TEMP/test-box2
  default      running      (virtualbox)   2013-12-07 09:48:37 +0100
201312072152VVAW12.png