Status Code 303 - See Other

サーバサイド、iOS・アンドロイドアプリ、インフラレベルの話まで幅広くやってます。情報の誤りや指摘・意見などは自由にどうぞ。

VirtualBox+Vagrant 環境構築(Windows 7+Cygwin) 苦戦記

概要

この記事で記述すること

Windows+Cygwin 上で Vagrant 環境を作成したときに苦戦した内容をメモったもの。

環境

今回は、Windows + Cygwin 環境で行う。
※ 構築 PC の CPU が仮想化技術をサポートしていない場合、多分できません。(下記参照)

ソフトウェアについて

VirtualBox

仮想化環境を提供するソフトウェア。その他、有名なソフトウェアには VMWare がある。
Oracle が提供している。基本的に無料で使える。

Vagrant

Virtual Box を使いやすくするためのソフトウェア。OSS であるため無料で使える。

Cygwin

Windows 上に Linux のコンソールっぽい環境を提供するソフトウェア。DOS が使えないので、結構使ってる。

Windows

た・・大変申し訳ありませんが、多分その方にはこの記事の内容はおそらく理解できないです。(^^;

なんのため?

  • 本番を想定したテスト環境を作成したり、その上で簡単なテストしたりするため。
  • 継続的デリバリー(CD)を実現するため。

 (参考:DevOps時代の開発者のための構成管理入門(終):継続的デリバリ/デプロイを実現する手法・ツールまとめ (1/2) - @IT)

環境構築

今回使用環境

Operating System

  • Windows 7 Professional Service Pack1(64bit)

Console

  • Cygwin (mintty 2.2.2)
    • 英語だけど文献いっぱいあるからなんとかなる。(cygwin 導入 - Google 検索)
    • 個人的に使いやすいと思ってるから使ってる

Vagrant を使うのに必要なアプリケーション

導入

構築手順

Cygwin Install

Cygwin にアクセスし、下記画面の場所からダウンロードする。
f:id:kouki_hoshi:20160610001654p:plain
ダウンロードできたら、起動してインストールする。ココで注意点としては、Choose Downloaded Sites で
末尾が【.jp】のものを選ぶこと。他でやるとネットワークが遅くて時間がかかってしまう。
f:id:kouki_hoshi:20160610001835p:plain
あとは、インストールした後にパッケージで sshrsync を Skip ステータスから変更してインストールする。
f:id:kouki_hoshi:20160610002033p:plainf:id:kouki_hoshi:20160610002030p:plain

で。事前準備は完了

VirtualBox + Vagrant Install

これらの環境セッティングは世の中にいっぱいあるので、それらに任せる。
注意点として、Ruby のパッケージ管理ツール gem からの Vagrant 提供は終了しているらしいので、
インストーラでインストールすること。(参考:Vagrant 1.1.x のインストール - oooooooo)

なお、私はこのサイトを参考にさせてもらった。操作も少なく、かなり簡単。
Windows上でVirtualBox+Vagrant+CentOSによる仮想環境構築 - Qiita

起動失敗

仮想化設定

問題なくインストールは終了したのだが、起動しても仮想マシンが起動しない。
「仮想化支援機能(VT-z/AMD-V)を有効化できません。・・・・」ってアラートが出る。
(同一事象:仮想化支援機構(VT-x/AMD-V)を有効化できません | Futurismo)

調べてみると、マザボBIOS から CPU の仮想化設定を有効にしないといけないそうだ。
システム設定 | VirtualBox Mania
CPU は Intel core i7-3770K。仕様を調べるとサポートしてた。(Intel® Core™ i7-3770K Processor (8M Cache, up to 3.90 GHz) 仕様)
f:id:kouki_hoshi:20160610005349p:plain

で。BIOS から探し出して有効化すると起動。
もし、サポートしてない場合は・・諦めるか、CPU 取り替えましょう。

rsync 実行が失敗

インストール後は Cygwin のコマンドで仮想化などの操作を実行できる。

また、仮想化用の OS は box と呼ばれており、以下のサイトで入手できる。

私は、Raspberry Pi の OS が raspbian jessie なので、そのテスト環境 Debian jessie を入れることに。
(実際は少し違うかもしれないけど)

仮想マシン起動コマンド

$ vagrant box add "debian-jessie" https://github.com/holms/vagrant-jessie-box/releases/download/Jessie-v0.1/Debian-jessie-amd64-netboot.box
$ vagrant init debian-jessie
$ vagrant up

で、エラーがでると。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'debian/jessie64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
GuestAdditions 5.0.20 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Rsyncing folder: /cygdrive/e/cygwin/home/hoshikouki/ => /vagrant
There was an error when attempting to rsync a synced folder.
Please inspect the error message below for more info.

Host path: /cygdrive/e/cygwin/home/hoshikouki/
Guest path: /vagrant
Command: rsync --verbose --archive --delete -z --copy-links --chmod=ugo=rwX --no-perms --no-owner --no-group --rsync-path sudo rsync -e ssh -p 2222 -o ControlMaster=auto -o ControlPath=C:/Program Files/cygwin64/tmp/ssh.363 -o ControlPersist=10m -o StrictHostKeyChecking=no -o IdentitiesOnly=true -o UserKnownHostsFile=/dev/null -i 'E:/cygwin/home/hoshikouki/.vagrant/machines/default/virtualbox/private_key' --exclude .vagrant/ /cygdrive/e/cygwin/home/hoshikouki/ vagrant@127.0.0.1:/vagrant
Error: ssh: Could not resolve hostname files/cygwin64/tmp/ssh.363: Name or service not known
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.2]

rsync コマンドが失敗したみたい。
「ControlPath=C:/Program Files/cygwin64/tmp/ssh.363」で半角スペースが混入してるのが問題になっている。
どうやら cygwin 環境で実行すると起こる問題らしく、ソースコードを直接編集して対処。
Windows + vagrant 1.8.1 + rsync 3.1.1 で発生する、config.vm.synced_folderの失敗の直し方 - Qiita

ruby のアプリケーションはインタプリタだから最悪手で調整できるのはありがたい。
もし、壊したら再度インストールすればいいんだし。(^-^;

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'debian/jessie64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
GuestAdditions 5.0.20 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Rsyncing folder: /cygdrive/e/cygwin/home/hoshikouki/ => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

以下コマンドで動作確認。

$ vagrant ssh

rsync を使って共有する場所は、デフォルト設定では、カレントディレクトリになるっぽい。
実際に共有するフォルダは独自に設定したいので カレントディレクトリにある Vagrantfile に以下の設定を追加する。

  config.vm.synced_folder "./vagrant_data/", "/vagrant"

Basic Usage - Synced Folders - Vagrant by HashiCorp

さらなる応用的情報

ドキュメント

英語だけど、本家ということもあり最も信用できる情報。
Documentation - Vagrant by HashiCorp

プラグイン sahara

OS の現在の状態を管理できる Vagrantプラグイン
これを使えば途中の状態をセーブできるため、便利らしい。(まだ使ったことがない・・)