【備忘録】Windows10にVagrant環境を構築する
子育てに追われている間に前の記事から1年ほど経ってしまった上に、Windows10にアップグレードしてしまった。
せっかくなので、Hyper-VをバックエンドにしてVagrant環境を構築しなおす。
Hyper-Vとは
Microsoftが提供するハイパーバイザ型の 仮想化システム。
元々はWindows Serverの一機能だったが、Windows8(Pro以上)からはコンシューマ向けOSでも利用できるようになった。
Hyper-Vのインストールと準備
まず以下の手順に従い、現在のPCでHyper-Vが利用可能か確認する。
対応していることが確認できたら、以下の手順に従ってHyper-Vをインストールする。
Windows 10 上に Hyper-V をインストールする
インストールが完了したらHyper-Vマネージャを起動し、仮想スイッチマネージャより外部仮想スイッチを作成する。
Vagrantのインストールと準備
Vagrantのインストールについては前回の記事を参照(執筆時点のバージョンは1.8.5)。
インストールが完了したら任意のディレクトリで以下のコマンドを実行し、Vagrantfileのテンプレートを生成する。
後々必要になるので、コマンドプロンプトは管理者権限で実行しておくこと。
>vagrant init 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.
boxの登録
Vagrantで仮想マシンを起動するにはboxを登録する必要がある。
boxにはOSのディスクイメージが含まれており、Vagrantはこれを元に仮想マシンを作成・起動する。
まずは以下のコマンドを実行してみる。
>vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
すると、「まだboxが1つも登録されていないので、`vagrant box add`コマンドでboxを追加すべし!」というメッセージが表示される。
boxを取得・登録するにはいくつかの方法があるが、今回はVagrantの開発元のHashicorp社のboxリポジトリ「Atlas」から取得することにする。
Discover Vagrant Boxes | Atlas by HashiCorp
以下のコマンドを実行し、Atlas上のUbuntu 12.04 LTS 64bit版のboxを登録する。途中で仮想化システムの種類を確認されるので、ここではHyper-Vを選択する。
>vagrant box add hashicorp/precise64
==> box: Loading metadata for box 'hashicorp/precise64'
box: URL: https://atlas.hashicorp.com/hashicorp/precise64
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) virtualbox
3) vmware_fusion
Enter your choice: 1
==> box: Adding box 'hashicorp/precise64' (v1.1.0) for provider: hyperv
box: Downloading: https://atlas.hashicorp.com/hashicorp/boxes/precise64/versions/1.1.0/providers/hyperv.box
box: Progress: 100% (Rate: 1527k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'hashicorp/precise64' (v1.1.0) for 'hyperv'!
boxの取得・登録が完了した後で再び以下のコマンドを実行すると、登録されたboxの一覧が表示される。
>vagrant box list
hashicorp/precise64 (hyperv, 1.1.0)
boxの登録が完了したら、Vagrantfileを以下のように編集する。
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.box_version = "1.1.0"
end
仮想マシンの起動
これで仮想マシンを起動する準備ができたので、以下のコマンドを実行して仮想マシンを起動する。
>vagrant up --provider hyperv
Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
==> default: Importing a Hyper-V instance
default: Cloning virtual hard drive...
default: Creating and registering the VM...
default: Successfully imported a VM with name: precise64
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
default: Timeout: 120 seconds
default: IP: 192.168.2.104
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 192.168.2.104:22
default: SSH username: vagrant
default: SSH auth method: password
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Preparing SMB shared folders...
default: You will be asked for the username and password to use for the SMB
default: folders shortly. Please use the proper username/password of your
default: Windows account.
default:
default: Username: [Windowsのユーザ名]
default: Password (will be hidden): [Windowsのパスワード]
==> default: Mounting SMB shared folders...
default: [Vagrant作業ディレクトリ] => /vagrant
ここで注意しなくてはいけないことは、Hyper-Vをバックエンドとして使用するには
--provider hyperv
オプションをつけて実行しなくてはならないということ。
これを忘れてしまうとVirtualBoxをダウンロードしようとしてしまうので、落ち着いてCtrl+Cでキャンセルしよう。
とはいえ、毎回オプションをつけるのは面倒なので、もしHyper-Vをメインに使用するのであれば、環境変数「VAGRANT_DEFAULT_PROVIDER」に「hyperv」と設定しておくことをおすすめする。
こうしておけば、オプションを省略することができる。
仮想マシンの終了
うまく仮想マシンを起動することができたら、以下のコマンドを実行して仮想マシンを終了する。
>vagrant halt ==> default: Attempting graceful shutdown of VM...
【備忘録】Windows7にVagrant環境を構築する(その1)
OSを再インストールしてからVagrant環境を作ってなかったので、構築するついでに備忘録。
Vagrantとは
VMwareやVirtualBox、Hyper-Vといった仮想化技術をバックエンドに、統一されたコマンドラインインタフェースを提供する仮想環境構築ソフトウェア。
VirtualBoxのインストール
以下のページからVirtualBoxのインストーラを入手する(執筆時点のバージョンは5.0.2)。
Downloads – Oracle VM VirtualBox
今回はWindows7なので「VirtualBox 5.0.2 for Windows hosts」を選択する。
ついでに「VirtualBox 5.0.2 Oracle VM VirtualBox Extension Pack」もダウンロードしておく。
あとはインストーラに従いインストールを進める。途中で「Windowsセキュリティ」ダイアログが表示された場合は、「インストール」をクリックする。
Vagrantのインストール
以下のページからVagrantのインストーラを入手する(執筆時点のバージョンは1.7.4)。
今回はWindows7なので「Windows Universal (32 and 64-bit)」 を選択する。
あとはインストーラに従いインストールを進める。最後にPCの再起動を求められるので、再起動する。
インストール確認
コマンドプロンプトを開いて以下のコマンドを実行し、バージョンが表示されればインストール完了。
>vagrant -v Vagrant 1.7.4
次回は実際に仮想環境を構築してみる。