Vagrant

since 2014-02-10

VirtualBox をコマンドラインから管理するツールを、Ruby でラップして使いやすくしたもの。

プロビジョニングという機能を使うと、Linux 仮想マシンを作って、必要な初期設定、必要なパッケージのインストールを行う処理をコード化(自動化)できる。

Windowsで使う

Windows 8.1 x64 にVagrantをインストールする。最近のバージョンは ruby を別途インストールする必要がない。

VirtualBox-4.3.6-91406-Win.exe

"C:\Program Files\Oracle\VirtualBox" に PATH を通す。

コマンドプロンプトから vboxmanage を実行できることを確認。

Vagrant_1.4.3.msi

C:\HashiCorp\Vagrant\ から C:\Vagrant\ に変更。

再起動。

C:\>which vagrant
C:\Vagrant\bin\vagrant
C:\Vagrant\bin\vagrant.bat

C:\>vagrant -v
Vagrant 1.4.3

sahara を入れる:

c:\>vagrant plugin install sahara
Installing the 'sahara' plugin. This can take a few minutes...
Installed the plugin 'sahara (0.0.16)'!

vagrant-vbox-snapshot を入れる:

c:\>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.4)'!

Ubuntu Precise64 をためす

Box を探す:

http://www.vagrantbox.es/

Ubuntu を up してみる:

http://qiita.com/bojovs/items/d560e675e4c1e349af34

cd ubuntu
vagrant box add precise64 http://files.vagrantup.com/precise64.box
Downloading box from URL: http://files.vagrantup.com/precise64.box
Box download is resuming from prior download progress
Extracting box...ate: 1361k/s, Estimated time remaining: --:--:--)
Successfully added box 'precise64' with provider 'virtualbox'!
vagrant init precise64
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.
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'precise64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[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...
DL is deprecated, please use Fiddle
[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
prevent things such as shared folders from working properly. If you see
shared folder errors, please make sure the guest additions within the
virtual machine match the version of VirtualBox you have installed on
your host and reload your VM.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.3
[default] Mounting shared folders...
[default] -- /vagrant
vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$

そのままパスワードなしで sudo apt-get update, sudo apt-get upgrade できる。

Configuring grub-pc で止まったので /dev/sda だけをチェックしておいた。

https://github.com/mitchellh/vagrant/issues/289

終了したら sudo reboot して vagrant ssh しなおす:

Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Mon Feb 10 04:53:37 2014 from 10.0.2.2

別のコマンドプロンプトで同じディレクトリに移動して halt する:

cd c:\work\vagrant\ubuntu
vagrant halt
[default] Attempting graceful shutdown of VM...
DL is deprecated, please use Fiddle

"DL is deprecated, please use Fiddle" は ruby が出しているメッセージらしいが無視しておく。。

実際の仮想マシンのイメージは "C:\Users\(ユーザ名)\VirtualBox VMs" にある。

Windows から ssh できるようにする

Vagrantfile をいじる:

config.vm.network の行をコメントアウトはずす

vagrant down, vagrant up やりなおす。

vagrant ssh して ifconfig する:

eth0      Link encap:Ethernet  HWaddr 08:00:27:88:0c:a6
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe88:ca6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:478 errors:0 dropped:0 overruns:0 frame:0
          TX packets:345 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:60712 (60.7 KB)  TX bytes:42147 (42.1 KB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:89:33:51
          inet addr:192.168.33.10  Bcast:192.168.33.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe89:3351/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:45 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4554 (4.5 KB)  TX bytes:468 (468.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

コマンドプロンプトから ssh してみる:

ssh vagrant@192.168.33.10

パスワード vagrant でログインできた:

vagrant@192.168.33.10's password:
Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Thu Feb 13 00:24:51 2014 from 10.0.2.2
vagrant.txt · 最終更新: 2014/02/13 12:20 by Takuya Nishimoto
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0