Vagrant+Virtualbox使い方(Mac PCにて)

Mac-PCにて、Vagrant Virtualboxのインストール、仮想サーバ起動方法、スナップショットの管理方法を纏めたメモです。

以下リンクより、VirtualBox、Vagrantをそれぞれインストールする。
VirtualBox
Vagrant

Vagrantのバージョンが表示されること。

$ vagrant -v
Vagrant 1.7.4

方法その① hashicorpの純正品?で、インストール

▼設定ファイル作成
$ mkdir test_centos
$ cd test_centos
$ vim Vagrantfile

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "bento/centos-6.7"  #← ※1 https://atlas.hashicorp.com/bento/より名前を確認した。
  config.vm.network "private_network", ip: "192.168.33.11"  #← ※2 任意のローカルIPを指定。
end

※1 config.vm.boxに入れる名前は、hashicorp で確認すること。
※2 任意のローカルIPを指定。

▼実行
(サーバ立ち上げ) 数分かかる。
$ vagrant up
▼SSHログインする
vagrantユーザーでログインされる
$ vagrant ssh
▼スイッチ
サーバへログイン後、以下でrootユーザーになれる。
$ sudo su -
[vagrant@localhost ~]$ id
uid=500(vagrant) gid=500(vagrant) 所属グループ=500(vagrant) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[vagrant@localhost ~]$ sudo su -
[root@localhost ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

方法その② Boxをダウンロードして、立てる (こちらで純正品以外でもインストールが可能。)

▼Boxをダウンロードして、立てる
以下からURLを取得する
http://www.vagrantbox.es/
または
https://github.com/chef/bento
▼Boxを追加する
コマンド例:

$ vagrant box add {title} {url}

titleは任意名、urlは各サイトよりURLを確認する

以下からURLを取得する
http://www.vagrantbox.es/
入れるbox:
CentOS 7.0 x64 (Minimal, VirtualBox Guest Additions 4.3.28, Puppet 3.8.1
ダウンロード等があるので、数分かかる。

$ vagrant box add centos7test https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box

または、
以下からURLを取得する
https://github.com/chef/bento
入れるbox:
centos-6.7 x86_64
ダウンロード等があるので、数分かかる

$ vagrant box add centos67test http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.7_chef-provisionerless.box

▼box一覧表示 boxが追加されたことを確認できる

$ vagrant box list

centos67test (virtualbox, 0)

実際の追加されたboxファイルは ~/.vagrant.d/boxes/ 配下に追加される
▼box削除方法

例:

$ vagrant box remove centos67test

Vagrantfile作成

▼初期化 (Vagrantfileが作成される)

$ vagrant init {title}

例:

$ vagrant init centos67test

titleを指定しないと、雛形のVagrantfileが作成される。
▼Vagrantfileの中身を確認 (コメントアウト改行を除外して表示)

$ grep -v "^#" Vagrantfile | grep -v "^ #" | grep -v "^$"

以下しか設定されていない

Vagrant.configure(2) do |config|
 config.vm.box = "centos67test"
 end

▼IPの設定
Vagrantfileを確認するとコメントアウトで、以下の記述がある。

 # config.vm.network "private_network", ip: "192.168.33.10" 

コメントアウトを解除し、任意のIPを設定する。

$ vim Vagrantfile

Vagrantfile

:
config.vm.network "private_network", ip: "192.168.33.44"
                                         ↑任意のIPを設定する
:

仮想サーバ起動

$ vagrant up

▼仮想マシンの保存場所は以下にある。
~/VirtualBox VMs/
▼httpで接続する場合、以下でアクセスできる。
http://192.168.33.44
その他Vagrantコマンド

▼仮想サーバ 停止

vagrant halt

▼仮想サーバ削除

vagrant destroy

▼Vagrantfileの雛形の作成コマンド

vagrant init

デフォルトのVagrantfileが作成される。
▼仮想サーバ再起動

vagrant reload

(vagrant halt して、 vagrant upしたのと同じ。)
▼プロビジョニング。サーバ起動後に実行される処理を指定できる。chefやansibleなどを実行させる。当然、Shellも実行できる。

 vagrant provision 

Vagrantfileに以下のように記述しておけば、vagrant upしたとき、または、vagrant provisionを実行することでbox内で実行することができる。

:
config.vm.provision :shell, :path => "./provision-script/start-setting.sh",:privileged   => true
                       ※ ↑privileged => true はrootユーザーとして実行するためのオプション。
:

仮想サーバ側では、以下に保存される。
/vagrant/provision-script/start-setting.sh
start-setting.shの内容

#!/bin/bash

echo "TEST" > ~/test.txt

exit 0

※ rootユーザーで実行されるので、test.txtは/root/test.txt に作成されるので注意。

Vagrantのスナップショットがとれるようにする

プラグインvagrant-vbox-snapshotのインストール

$ vagrant plugin install vagrant-vbox-snapshot
Installing the 'vagrant-vbox-snapshot' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbox-snapshot (0.0.9)'!

以下コマンドを投げ、以下のように表示されれば、インストールOK

$ vagrant snapshot
Usage: vagrant snapshot <command></command> []

Available subcommands:
back
delete
go
list
take

For help on any individual command run `vagrant snapshot <command></command> -h`

スナップショット操作方法

※ 以下操作はVagrantfileがあるディレクトリ内で、実行すること!
▼スナップショット作成

$ vagrant snapshot take 任意のスナップショット名

例:

$ vagrant snapshot take initial-test
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Snapshot taken. UUID: fbae8bda-f678-4bc2-b339-40abc444db28

▼スナップショット一覧確認

$ vagrant snapshot list

例:

$ vagrant snapshot list
Listing snapshots for 'default':
   Name: initial-test (UUID: fbae8bda-f678-4bc2-b339-40abc444db28) *

▼最新のスナップショットから復元させる

$ vagrant snapshot back

※ 自動で仮想サーバは再起動されます。
例:

$ vagrant snapshot back
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Restoring snapshot fbae8bda-f678-4bc2-b339-40abc444db28
Starting restored VM
==> default: Checking if box 'bento/centos-6.7' is up to date...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!

▼任意のスナップショットから復元させる

$ vagrant snapshot go 任意のスナップショット名

※ 自動で仮想サーバは再起動されます。
例:

$ vagrant snapshot go initial-test
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Restoring snapshot fbae8bda-f678-4bc2-b339-40abc444db28
Starting restored VM
==> default: Checking if box 'bento/centos-6.7' is up to date...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!

▼スナップショット削除

$ vagrant snapshot delete 任意のスナップショット名

例:

$ vagrant snapshot delete initial-test
0%...
10%...
20%...
30%...
40%...
50%...
60%...
70%...
80%...
90%...
100%

 

本記事は、Qiitaで過去投稿しています。
http://qiita.com/arai/items/3f1ca3a162cad9b91839