LoginSignup
1
2

More than 5 years have passed since last update.

Hyper-V で立てたUbuntuにvirtualbox + vagrant でUbuntu を立ててみる

Last updated at Posted at 2018-12-29

はじめに

チームで開発をしているときに、VirtualBox + Vagrant + Ansibleで開発環境をローカルに構築していて、自分だけWindowsだったので開発環境を構築する際に工夫が必要だった(Ansible はWindows をコントロールマシンとしてはサポートしていないのと、VirtualBox はNested Virtualizationをサポートしていない)。

その時は、ホスト(Windows) に入れたVirtualBox + Vagrant をWSLから操作しつつ、WSLでAnsible を実行していた。

もっといい方法があるだろうなと思って、
Windows + Hyper-V で立てたVM内で VirtualBox + Vagrant を試してみた。

最終的には以下のような構成を目指す。

            -------------------------------
           | Ubuntu (VirtualBox + Vagrant) |
        ---------------------------------------
       |          Ubuntu (Hyper-V)             |
     ----------------------------------------------
    |                  Windows                     |
    ------------------------------------------------

環境

ホスト側

Windows10 Pro
Hyper-v
Intel Core i7-3770k

ゲスト側

Ubuntu 16.04
VirtualBox 6.0.0
Vagrant 2.2.2

VirtualBox -> Hyper-V に変更

VirtualBox と Hyper-Vマネージャから利用するVMは共存することができない。
仮想化に利用されている技術が違うからだ。(INTEL VT-x とHyper-V)

Docker for Windows ではHyper-Vを利用しているので
VirtualBox を利用しているときにDocker for Windows を入れてしまうとVirtualBox からVMを立ち上げたときにブルースクリーンになってしまう。仮想化に使われている技術が違うことに起因するブルースクリーンである。

Hyper-V 有効化についてはMicrosoft さんが紹介している。

Hyper-V を利用してUbuntu をたてる

Hyper-V を利用してUbuntu を入れる。
ここもMicrosoftさんが説明してくれている。

インストールが済んだあとVirtualBox とVagrant を導入する。

Vagrant up してみる

ハマりポイント1

vagrant init ubuntu/xenial64
vagrant up

すると

The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

となり、起動できない。

原因はHyper-Vマネージャの設定 > ハードウェア > セキュリティ の"セキュアブートが有効" にチェックがついていることが原因だった。
なので、この設定を無効にする。

ハマりポイント2

VMを再び起動して、

vagrant init 
vagrant up

すると

There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "f59d4b5a-ecdb-4755-9918-77db8ed472d3", "--type", "headless"]

Stderr: VBoxManage: error: VT-x is not available (VERR_VMX_NO_VMX)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

となる。
MV(Ubuntu)にインストールしたVirtualBox の設定を見てもVT-xは有効になっている。
キャプチャ.JPG

となると、VMのマシンとなっている側の設定が疑わしいと思って調べてみると、
Microsoftさんに入れ子になった仮想化による仮想マシンでの Hyper-V の実行 というのがあるのでやってみる。

Windows側でアドミン権限でPowerShell を起動し、

PS C:\WINDOWS\system32> Set-VMProcessor -VMName <VMName> -ExposeVirtualizationExtensions $true

を実行してみる。

再びVMを起動して、

vagrant up

してみると、無事にVMが立ち上がる。

まとめ

もともとWindows を使わなければよいのではという話ですが、Windowsでも最近はWSLなどおもしろいことができるので試してみました。
前にやっていたパターンよりシンプルにVM on VMができると思います。
WindowsはWSLを含め、開発にフレンドリーな機能を提供しているので、色々と触ってみるのもいいかもしれません。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2