Windows7にVagrantをインストールして動かしてみる

業務都合でWindows7Vagrantをインストールして動かしてみた。

バージョン

インストール

まず、最新版のWindows向けVagrantをサイトからダウンロード。

Download Vagrant - Vagrant

インストーラーを起動するとセットアップウィザードの準備が始まる。

f:id:heroween:20140604121821p:plain

f:id:heroween:20140604121828p:plain

そこそこ時間のかかる準備が終わるとインストールできるようになる。

f:id:heroween:20140604121838p:plain

ライセンスに同意し、

f:id:heroween:20140604121853p:plain

インストールディレクトリ(デフォルトはC:¥HashiCorp¥Vagrant)を決定し、

f:id:heroween:20140604121912p:plain

インストール開始。

f:id:heroween:20140604121924p:plain

f:id:heroween:20140604121931p:plain

3分くらいでインストールが完了。

f:id:heroween:20140604121946p:plain

完了したらWindowsの再起動をする。

f:id:heroween:20140604121953p:plain

再起動後にコマンドプロントを開いてvagrantコマンドが叩けるか確認。

f:id:heroween:20140604122001p:plain

バージョンが表示されて確認完了。
セットアップウィザード通りに進めれば簡単にインストールできる。

動かしてみる

それでは、Vagrantのインストールが完了したので、今度は仮想マシンを起動するところまで動かしてみる。
なお、プロバイダにはVirtualBox 4.3.12を、BoxにはBentoからダウンロードしてきたCentOS 6.5の64bit版を使用する。

まずは、Boxの追加から。

C:\Users\heroween>vagrant box add C:\HashiCorp\VagrantBox\opscode_centos-6.5_chef-provisionerless.box --name opscode-centos-6.5
==> box: Adding box 'opscode-centos-6.5' (v0) for provider:
    box: Downloading: file://C:/HashiCorp/VagrantBox/opscode_centos-6.5_chef-provisionerless.box
    box: Progress: 100% (Rate: 9870k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'opscode-centos-6.5' (v0) for 'virtualbox'!

C:\Users\heroween>vagrant box list
opscode-centos-6.5 (virtualbox, 0)

次に適当な作業ディレクトリを作って仮想マシンを起動してみる。

C:\Users\heroween\VagrantProject\SampleProject>vagrant init opscode-centos-6.5
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.

C:\Users\heroween\VagrantProject\SampleProject>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'opscode-centos-6.5'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: SampleProject_default_1401933415261_2874
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/heroween/VagrantProject/SampleProject

C:\Users\heroween\VagrantProject\SampleProject>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`.

無事に起動したので仮想マシンに接続する。

C:\Users\heroween\VagrantProject\SampleProject>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/heroween/.vagrant.d/insecure_private_key

と、Windowssshコマンドを入れてない場合はvagrant sshでは接続できない(2014/6/6追記:Windowsにsshコマンド入れる手順を書いた)
取り敢えず今回は既に入ってたPuttyを使ってポートフォワードされたローカルの2222に接続し、

f:id:heroween:20140605115817p:plain

ユーザーとパスワードを入力。

f:id:heroween:20140605120006p:plain

接続完了。Windows上でCentOSが使えるようになった。
接続確認も終えたので、仮想マシンをシャットダウン。

C:\Users\heroween\VagrantProject\SampleProject>vagrant halt
==> default: Attempting graceful shutdown of VM...

C:\Users\heroween\VagrantProject\SampleProject>vagrant status
Current machine states:

default                   poweroff (virtualbox)

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

そして、仮想マシンを破棄する。

C:\Users\heroween\VagrantProject\SampleProject>vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

C:\Users\heroween\VagrantProject\SampleProject>vagrant status
Current machine states:

default                   not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.

以上、特に躓くような事も無く、インストールから簡単な動作確認までできた。

今度はプロビジョニングやPackerなども試してみようと思う。

VirtualBoxでのNested Virtualizationは無理だったという話

タイトルが全てなんだが、遭遇した事の次第をメモとして書き残す。

同僚からUbuntuで試したVagrantによる環境構築を、CentOSでも検証してくれないかと依頼され、自分のマシンに入っていたVirtualBox上のCentOSで作業していたら躓いた。

環境

ホスト1 -> ゲスト1 = ホスト2 -> ゲスト2という入れ子になる感じ。VagrantとPackerは今回遭遇した問題の切っ掛けでしかなく、本質的に関係は無い。

事象

基本的に以下はVirtualBox上のCentOS 6.4(環境のゲスト1 = ホスト2)で行っている作業である。

まず、ゲストのBoxが必要だったので、同僚の作ったPackerのテンプレートをクローンしてきてpacker buildを走らせたが、以下の様に仮想マシン起動後のSSH接続で動作が止まってしまった。

$ packer build --only=virtualbox-iso machine.json
virtualbox-iso output will be in this color.

==> virtualbox-iso: Downloading or copying Guest additions checksums
    virtualbox-iso: Downloading or copying: http://download.virtualbox.org/virtualbox/4.3.12/SHA256SUMS
==> virtualbox-iso: Downloading or copying Guest additions
    virtualbox-iso: Downloading or copying: http://download.virtualbox.org/virtualbox/4.3.12/VBoxGuestAdditions_4.3.12.iso
==> virtualbox-iso: Downloading or copying ISO
    virtualbox-iso: Downloading or copying: http://ftp.riken.jp/Linux/centos/6.5/isos/x86_64/CentOS-6.5-x86_64-minimal.iso
==> virtualbox-iso: Starting HTTP server on port 8081
==> virtualbox-iso: Creating virtual machine...
==> virtualbox-iso: Creating hard drive...
==> virtualbox-iso: Creating forwarded port mapping for SSH (host port 3213)
==> virtualbox-iso: Executing custom VBoxManage commands...
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --memory 512
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso --cpus 1
==> virtualbox-iso: Starting the virtual machine...
    virtualbox-iso: WARNING: The VM will be started in headless mode, as configured.
    virtualbox-iso: In headless mode, errors during the boot sequence or OS setup
    virtualbox-iso: won't be easily visible. Use at your own discretion.
==> virtualbox-iso: Waiting 10s for boot...
==> virtualbox-iso: Typing the boot command...
==> virtualbox-iso: Waiting for SSH to become available...

暫く待っていたが、結局SSHのコネクションがタイムアウトしてビルドは失敗。

SSHに原因があるのかとログを確認するが、タイムアウトした以上の事は分からず、ホストのSSH、Firewallの設定、ゲストのSSH、ポートフォワードの設定を確認するが問題は無い。netstatで見るとポートフォワードされたTCPコネクションは張られているものの、SSHが応答していない感じ。

CUIのメッセージとログはそれだけだったので、一旦、ヘッドレスになっていたPackerのビルダー設定をGUI表示に切り替えてみて、再度ビルドを実行してみたところで原因が判明した。

{
  "builders": [
    {
      "headless": false  ←falseでVirtualBoxGUIが表示される
    }
  ]
}

※当該設定の前後は省略

原因

再ビルドしたら、VirtualBoxが起動した所でこの様なエラーメッセージが出力された。

f:id:heroween:20140527223117p:plain

「ホストマシンの仮想化支援機能(VT-x/AMD-V)が使用できません。64ビット ゲストOSは64ビットCPUを検出できず、起動できません。」

なるほど、ゲストが起動していませんでした。

CUIのメッセージでは、ゲスト起動コマンド実行後にSSHの接続待ちとなっていたが、SSH接続はゲストの起動完了を待たずしてなされていたのである。そりゃあ応答しない訳だ。

で、ホストのハードウェア仮想化支援機能だが、/proc/cpuinfoを見ると確かにflagsにvmx(VT-x)が表示されていない。

$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 42
model name      : Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
stepping        : 7
cpu MHz         : 2673.155
cache size      : 6144 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 5
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc up rep_good pni monitor ssse3 lahf_lm
bogomips        : 5346.31
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

CPUはIntel(R) Core(TM) i7-2620M CPU @ 2.70GHzでflagsにもlmがあるので64ビットである事は間違いないし、VT-xにも対応している。ついでにカーネルも見てみる。

$ uname -rm
2.6.32-358.6.1.el6.x86_64 x86_64

大丈夫だ。問題無い。

それでは、VirtualBoxがホストをどう認識しているのかも確認してみる。

$ VBoxManage list hostinfo
Host Information:

Host time: 2014-05-28T02:55:25.926000000Z
Processor online count: 1
Processor count: 1
Processor online core count: 1
Processor core count: 1
Processor#0 speed: 2807 MHz
Processor#0 description: Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
Memory size: 1877 MByte
Memory available: 1387 MByte
Operating system: Linux
Operating system version: 2.6.32-358.6.1.el6.x86_64

うーん、見た感じは大丈夫そう…。

対応

ホストのハードウェア仮想化支援機能が有効になっていないのであれば、有効にすれば良いじゃない。という事で、CPUはハードウェア仮想化支援機能に対応しているので、BIOSでその設定を有効にする事ができるはずなのだが、対象となるホストはVirtualBox上のマシンであるので、まず、VirtualBoxにハードウェア仮想化支援機能の設定があるのかどうか確認してみた。

f:id:heroween:20140528124727p:plain

すると、仮想マシンの設定 -> システム -> アクセラレーションに「VT-x/AMD-Vを有効化」とまさに該当するものがあった…のだが、既に有効化されている…。

今度は仮想マシンを起動してBIOSを確認するが、ハードウェア仮想化支援機能に該当する設定は見当たらず、BIOSからは設定を確認できないようだ。

f:id:heroween:20140528121830p:plain

あるぇ〜?となって調べてみると、結構昔からあるVirtualBoxfeature-requestを見つけたのだが、それによるとVirtualBoxではNested Virtualizationに対応しておらず、仮想マシン上ではハードウェア仮想化支援機能がまともに機能しないようである。そして、その機能を実現する見通しも無いっぽい。

結論

という訳で、今回やろうとしていた事はそもそもできなかったという結論に到った。尚、VMwareなんかではできるようなのだが、そちらの方は詳しく調べてはいない。

以上、VirtualBoxでのNested Virtualizationは無理だったという話でした。

nokogiriのインストールでハマった件

自宅のMacVagrant弄っていて、プラグインをあれこれ試そうとインストールした際にちょっとハマったので、その時の問題解消までの経緯をメモしとく。

最初に結論を書いておくが、

keg-onlyのformulaのbrew linkを忘れるな!

という事です。以下蛇足。

Vagrantプラグインvagrant-omnibusのインストール時に以下のようなメッセージが出力された。

$ vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...
Building nokogiri using packaged libraries.
Building libxml2-2.8.0 for nokogiri with the following patches applied:
    - 0001-Fix-parser-local-buffers-size-problems.patch
    - 0002-Fix-entities-local-buffers-size-problems.patch
    - 0003-Fix-an-error-in-previous-commit.patch
    - 0004-Fix-potential-out-of-bound-access.patch
    - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
    - 0006-Do-not-fetch-external-parsed-entities.patch
    - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
    - 0008-Improve-handling-of-xmlStopParser.patch
    - 0009-Fix-a-couple-of-return-without-value.patch
    - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
    - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

However, note that nokogiri does not necessarily support all versions
of libxml2.

For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************

メッセージの2行目から分かる通り、VagrantプラグインのインストールにRubyGemsのnokogiriが必要で、テスト用の環境にはまだnokogiriをインストールしていなかった為、nokogiriをビルドし始めた模様。

で、そのnokogiriなんだけど、バージョン1.6から依存ライブラリであるlibxml2(とlibxslt)を同梱するようになっており、インストール時に毎度それらも合わせてビルドされる為、非常に時間がかかる様になった。

メッセージの3行目以降はそれに関連したもので、既にマシンにlibxml2がインストールされている場合は、gemやbundlerのコマンドにオプションを付与する事で、nokogiriに同梱されているものではなく、インストール済みのものを使ってビルドする事ができるよと書いてある(libxml2のバージョンを全てサポートしてる訳じゃないが)。

私のMacには既にhomebrewでlibxml2がインストールされていて、バージョンも問題無かったので一旦vagrant-omnibusのインストールを中断し、改めてgemでnokogiriをインストールしたところ、以下の様なエラーメッセージが出力されてビルドがこけた。

$ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri --no-rdoc --no-ri
Building native extensions.  This could take a while...
Building nokogiri using system libraries.
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

        /usr/local/var/RBENV_ROOT/versions/1.9.3-p547/bin/ruby extconf.rb
Building nokogiri using system libraries.
libxml2 version 2.6.21 or later is required!
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/usr/local/var/RBENV_ROOT/versions/1.9.3-p547/bin/ruby
    --help
    --clean
    --use-system-libraries=true
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
    --with-libxml-2.0-config
    --without-libxml-2.0-config
    --with-pkg-config
    --without-pkg-config
    --with-xslt-dir
    --without-xslt-dir
    --with-xslt-include
    --without-xslt-include=${xslt-dir}/include
    --with-xslt-lib
    --without-xslt-lib=${xslt-dir}/lib
    --with-libxslt-config
    --without-libxslt-config
    --with-exslt-dir
    --without-exslt-dir
    --with-exslt-include
    --without-exslt-include=${exslt-dir}/include
    --with-exslt-lib
    --without-exslt-lib=${exslt-dir}/lib
    --with-libexslt-config
    --without-libexslt-config


Gem files will remain installed in /usr/local/var/RBENV_ROOT/versions/1.9.3-p547/gemsets/global/gems/nokogiri-1.6.2.1 for inspection.
Results logged to /usr/local/var/RBENV_ROOT/versions/1.9.3-p547/gemsets/global/gems/nokogiri-1.6.2.1/ext/nokogiri/gem_make.out

最初に挙げたメッセージと見比べれば分かるが、2行目のメッセージ内容が、

Building nokogiri using packaged libraries.

から、

Building nokogiri using system libraries.

に変わっているので、インストール済みのlibxml2を使う様に指定はされているのだが、libxml2が無いよと言われてビルドできない。

インストール自体はされているのに一体何故だ?と、この時点で普通にlibxml2が有効になっているかどうかを確認すべきだったが、有効になっているものだと思い込んでしまっており、気が付くまでに小一時程無駄にした。

思考と試行の末に漸くシンボリックリンクを疑い始め、brew infoしたら案の定libxml2が有効になっていなかった事を確認。何度か環境を作り直しているマシンだったので、以前構築した際にkeg-onlyのformulaを一部brew linkし忘れていたようだorz

以下のコマンドを実行してから再度nokogiriをビルドしたら無事にインストールできた。

$ brew link --force libxml2

これでやっとVagrantプラグインもインストールできましたとさ。めでたしめでたし。

rbenvのrehashみたいに、この辺を自動的に補完してくれる様なコマンドオプションとかないかなーと思って調べてみたけど無さそうなので、取り敢えず最後にもう一度、

keg-onlyのformulaのbrew linkを忘れるな!

大事な事なので以下略。おわり。

山城さんの送別会に行ってきた

 昨日、山城さんの送別会に行ってきました。

http://ymsr.peatix.com/

 ブログに書くまでがjava-jaという訳で、はてブロ作って書きました。

f:id:heroween:20140215190112j:plain

 

経緯

 山城さんが亡くなった事を知ったのは1月のこと。

 送別会の参加募集ページの方を先に見つけ、内容からjava-jaの悪ふざけだろうと断定し、山城さん海外にでも行くのかな?くらいに思っていたのですが、その後に太一さん経由で事実を知った会社の同僚から、本当に亡くなったそうだと教えられました。

 私は山城さんとは顔見知り程度の仲で、さほど親しかった訳ではありません。しかし、同年代でありながら、java-jaやその他の勉強会で大いに活躍される姿を遠巻きに見ていて、彼に対しては正直なところ憧れを持っていました。

 彼の死というものは、私にとっても少なからず衝撃でありました。

 親しい方が多かったであろう山城さんの送別会への参加は、多少憚られたのですが、募集人数が落ち着いたのを見計らい、最後の方の枠に滑り込ませてもらった次第です。

 

送別会

 送別会は大体以下の様な感じでした。

f:id:heroween:20140215200953j:plain

f:id:heroween:20140215201039j:plain

f:id:heroween:20140215201304j:plain

f:id:heroween:20140215201309j:plain

 ハチャメチャです。

 形見分けの儀においては、こちらの品物を頂きました。

f:id:heroween:20140216001612j:plain

 吉良のネクタイ?

f:id:heroween:20140216001643j:plain

 いいえ、菌です。

 更に詳しくどのような様子であったか知りたい方は、親しかった皆様が沢山書かれているので、そちらをご覧頂ければ分かるかと思います。非常にらしいというかなんというか、山城さんの人柄を偲ばせるような、それはそれは愉快な、そして爽快な送る会でありました。

 ご遺族や太一さんをはじめ、このような機会を与えてくださった関係者の皆々様に厚く御礼申し上げます。

 

思い出

 折角なので、少し山城さんとの思い出も書きます。

 山城さんを初めて知ったのは、ゆろよろさん主催のJVM勉強会での事でした。

 この勉強会は私が初めて参加した勉強会だったのですが、懇親会で一つの事件がありました。懇親会参加予定者が大量にドタキャンしたのです。この時に幹事をされていたのが誰あろう山城さんその人だったのです。

http://togetter.com/li/40354

 私は懇親会に不参加だったので、後にTwitterのTL上でこの騒動を知るのですが、名幹事山城という名を覚えたのがこの時でした。

 

 それから暫くを経て、初java-jaであったjava-ja.js #2でお見かけするなどありつつ、初めてお話する機会となったのが、次の年の197Xパーティーでした。

 この時にどんな会話をしたかは実はあまり覚えていないのですが、初対面ということもあり敬語で話す私に「あのさぁ、マジで敬語とか使わないでよ。堅苦しいの無しでいこうぜ!」と気さくに接してくれたのはよく覚えています。

 これを切っ掛けに顔見知りとなり、2011年はPHPでTDD&CIワークショップPHPカンファレンス2011などで、当時山城さんが取り組まれていた事を教えてもらったり、アジャイルサムライ他流試合では共にスタッフとして働いたりしました。以降も方々の勉強会等でお会いすれば挨拶し、一言二言会話をするような仲となりました。

 しかし、ここ1、2年程は私自身があまり表に出なくなった事や、プライベートな関係もほぼ無かった事でお会いする機会も徐々に減り、ブログのエントリがホッテントリになってるのを見かけたり、どこどこを辞められたという話を人伝に聞いたりする程度になっていました。

 

 そして、久しぶりに飛び込んで来たのが彼の訃報でありました。

 

 振り返ってみると、関係自体は薄かったのですが、勉強会で目にする山城さんは、私にはとても輝いて見えており、憧れの対象の一人でありました。そんな彼が亡くなっていて、その事実を一月以上を経て知る事となり、全く現実味の無い中でその死を確認すべく送別会に参加する事に決めたのです。

 参加してみたら、会場のどこからかひょっこり出てくるのでは?とか、あの妹さんは実は山城さんなのでは?みたいな事を考えたりもしましたが、そんな事も無く、彼はやはり亡くなっておりました。

 

最後に

 私が山城さんに対して感じていたものは、この送別会でより一層強くなりました。不謹慎かもしれませんが、山城さんと彼を慕う人々との関係を目の当たりにし、正直羨ましいとさえ思ったくらいです。そして、初めてその死を実感しました。早過ぎます…。

 突然の死をむかえ、きっとやり残した事や、思い残す事が多々あったでしょう。それを思うと誠に残念でなりません。山城さん、どうか安らかに。ご冥福をお祈り申し上げます。