[Vagrant & VirtualBox] Vagrant: + VirtualBox で仮想マシンを作成する

Vagrant + VirtualBoxのインストールと、Linux/Windows の仮想マシン(Virtual Machine)を取得する。

[markdown]

最近まで Vargrant だと思ってました。。。

> * [Vagrant Documentation – Documentation – Overview](http://docs-v1.vagrantup.com/v1/docs/index.html)
> * [Vagrant開発者 Mitchell Hashimoto氏に聞いた:「Vagrant」って何ぞ?(・o・) – @IT](http://www.atmarkit.co.jp/ait/articles/1307/22/news076.html)

こちらを参考に進めました。

> * [Vagrant入門 (全13回) – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_vagrant)

## インストール

公式からパッケージをインストールします。
gemでインストールする方式は非推奨になったそうです。

> * [Vagrant](http://www.vagrantup.com/)
> * [Oracle VM VirtualBox](https://www.virtualbox.org/)

バージョンは以下の通り。

“`
% vagrant –version
Vagrant 1.3.1
% VirtualBox –help
Oracle VM VirtualBox Manager 4.2.18
(C) 2005-2013 Oracle Corporation
All rights reserved.
“`

## 仮想マシン Box(テンプレート)を取得する

### vagrant add

vagrant add で取得する。

#### Ubuntu

公式で配布されている。Ubuntu 12.04 でした。

“`
$ vagrant box add precise32 \
http://files.vagrantup.com/precise32.box
“`

#### CentOS

有志の配布している Box も取得してみる。

> * [A list of base boxes for Vagrant – Vagrantbox.es](http://www.vagrantbox.es/)

“`
% vagrant box add centos64 \
http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box
“`

通常は運用環境がそれぞれ異なるはずなので、自分で作るようです。

#### Windows

Vagrant で、Windows 仮想マシンを扱うのは問題もあるらしい。

> * [[Mac]modern.IEに公開されているWindows仮想マシンをVagrant経由で起動する | hello-world.jp.net](http://blog.hello-world.jp.net/?p=621)

追記:2014/01/27
VirtualBox への登録までは、下記にまとめました。

> * [MacにIEテスト環境を作る(VirtualBox 編) | deadwood](https://www.d-wood.com/blog/2014/01/27_5343.html)

## Box(テンプレート) から仮想マシンを作成・起動する

### vagrant box list

vagrant box add した Box は、以下のコマンドで確認でき、実体は ~/.vagrant.d/boxes/ に存在する。

“`
% vagrant box list
centos64 (virtualbox)
precise32 (virtualbox)
% ls ~/.vagrant.d/boxes/
centos64/ precise32/
“`

ちなみに通常 VirtualBox に登録した仮想マシンは、~/VirtualBox VMs 以下に配置される。

“`
% ls ~/VirtualBox\ VMs
Windows 8.1 Preview/
“`

### vagrant init

初期化。Vagrantfile が作成される。

“`
% mkdir myCentOSVM
% cd myCentOSVM
% vagrant init centos64
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.
% ls
Vagrantfile
“`

### vagrant up

Box(テンプレート) から仮想マシンを作成・起動する。
ちなみにテンプレートからいくつでも仮想マシンを作ることができる。

“`
% vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
[default] Importing base box ‘centos64’…
[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 up 後、実体ファイルが配置されていることが分かる。

“`
% ls ~/VirtualBox\ VMs
Windows 8.1 Preview/ myCentOSVM_default_1379511092/
“`

vagrant up すると、仮想マシンは VirtualBox 上で起動(実行中)ステータスとなる。

## 仮想マシンの起動・停止・再起動

### vagrant status

ステータスを確認できる。

“`
% 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`.
“`

表示される説明のとおり操作が可能。

### vagrant suspend / resume

サスペンド/レジューム

“`
% vagrant suspend
[default] Saving VM state and suspending execution…
% vagrant resume
[default] Resuming suspended VM…
[default] Booting VM…
“`

### vagrant halt / up

停止/起動

“`
% vagrant halt
[default] Attempting graceful shutdown of VM…
% 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] Mounting shared folders…
[default] — /vagrant
“`

### vagrant 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!
[default] Mounting shared folders…
[default] — /vagrant
“`

## 仮想マシンの削除

### vagrant destroy

不要になった仮想マシンを削除するコマンド。

“`
% vagrant destroy
Are you sure you want to destroy the ‘default’ VM? [y/N] y
[default] Forcing shutdown of VM…
[default] Destroying VM and associated drives…
% ls ~/VirtualBox\ VMs
Windows 8.1 Preview/
“`

## 仮想マシンに接続する

### vagrant ssh

“`
% vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$
“`

### vagrant ssh-config

sshの設定を確認できる。

“`
% vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/myuser/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
“`

> * [vagrant + virtualbox 仮想サーバ構築 – Qiita [キータ]](http://qiita.com/nobu_blast/items/828cf7c002100a0fdeb2)

すごい世の中になったもんだなぁ。

[/markdown]