Vagrant(ベイグラント)の備忘録

準備

仮想マシンのセットアップとブート

以下、Cygwin上で作業しました。

Boxの追加

$ vagrant box add centos https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
Downloading or copying the box...
Extracting box...ate: 402k/s, Estimated time remaining: --:--:--)
Successfully added box 'centos' with provider 'virtualbox'!

Boxの一覧

$ vagrant box list
centos (virtualbox)

仮想マシンの初期化

  • Usage: vagrant init [box-name]
  • 初期化が終わるとディレクト内に Vagrantfile が生成されます
$ mkdir ./centos
$ cd ./centos/

$ vagrant init centos
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

仮想マシンのブート

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the 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!
[default] Mounting shared folders...
[default] -- /vagrant

仮想マシンの状態確認

$ 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`.

ログインして起動を確認

  • Usage: vagrant ssh
  • poderosaなどのターミナルアプリケーションから接続する場合
    • vagrant ユーザが用意されている様です。
    • 接続ポートは 2222 みたいです。
    • 鍵ファイルは C:\Users\ユーザ名\.vagrant.d\insecure_private_key です。
$ vagrant ssh
[vagrant@vagrant-centos65 ~]$ uname -a
Linux vagrant-centos65.vagrantup.com 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

[vagrant@vagrant-centos65 ~]$ whoami
vagrant

[vagrant@vagrant-centos65 ~]$ exit
logout
Connection to 127.0.0.1 closed.

仮想マシンのシャットダウン

Usage: vagrant halt

$ vagrant halt
[default] Attempting graceful shutdown of VM...


$ vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

【トラブルシュート】vagrant up でエラーが出た場合

環境変数「VBOX_INSTALL_PATH」が定義されていない場合、次の様なエラーが出ました。
→ VBOX_MSI_INSTALL_PATH と同じ値を設定します。

$ env | grep -i VBOX
VBOX_INSTALL_PATH=D:\VirtualBox\

$ vagrant up
Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires
this to be available on the PATH. If VirtualBox is installed, please find the
`VBoxManage` binary and add it to the PATH environmental variable.

※ 環境変数 VBOX_INSTALL_PATH を設定 (システム > システムの詳細設定 > 環境変数)

$ env | grep -i VBOX
VBOX_INSTALL_PATH=D:\VirtualBox\
VBOX_MSI_INSTALL_PATH=D:\VirtualBox\

$ vagrant up
...
  • VBoxManage.exe を探してこれが見つからないと「VirtualBoxを検出できません」とする様です。
  • Vagrant/embedded/gems/gems/vagrant-1.3.5/templates/locales/en.yml に前述のエラーメッセージが定義されています。
en:
  vagrant:
    errors:
      virtualbox_not_detected: |-
        Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
        Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires
        this to be available on the PATH. If VirtualBox is installed, please find the
        `VBoxManage` binary and add it to the PATH environmental variable.
if Vagrant::Util::Platform.windows? || Vagrant::Util::Platform.cygwin?
@logger.debug("Windows. Trying VBOX_INSTALL_PATH for VBoxManage")

# On Windows, we use the VBOX_INSTALL_PATH environmental
# variable to find VBoxManage.
if ENV.has_key?("VBOX_INSTALL_PATH")
  # Get the path.
  path = ENV["VBOX_INSTALL_PATH"]
  @logger.debug("VBOX_INSTALL_PATH value: #{path}")