VagrantのproviderにHyper-Vを使う
やりたいこと
いままでVagrantはOracle Virtual Box上で起動させていたが、DockerをWindows上で動かしたい。併用はできないので、VagrantをHyper-Vで起動させるように変更します。
TL;DR
VirtualBoxはそのまま残しておいて、Boxのイメージと vagrant up
する際に --provider=hyperv
を付与する、または環境変数に VAGRANT_DEFAULT_PROVIDER=hyperv
を追加するとよいです。
試していたこと
Oracle VirtualBox のアンインストール
アプリと機能からOracle VirtualBoxをアンインストールします。
vagrant box list
で確認するとboxにゴミが残っていたので不要なboxを削除しようとしますが、アンインストールしてしまったのでcliから削除できませんでした。
D:\Vagrant\precise64>vagrant box list centos/7 (virtualbox, 1901.01)
D:\Vagrant\precise64>vagrant box remove centos/7 The provider 'virtualbox' that was requested to back the machine 'master' is reporting that it isn't usable on this system. The reason is shown below: Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed. Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires this to be available on the PATH. If VirtualBox is installed, please find the `VBoxManage` binary and add it to the PATH environmental variable.
ということでファイルシステム上のファイルを直接削除しました。
Windowsの場合は C:\Users\${MyUser}\.vagrant.d\boxes
にあります。${MyUser}
は各々異なります。
ファイルシステムのファイルを削除した後、vagrant box list
で確認すると以下のように削除されたことが分かります。
D:\Vagrant\precise64>vagrant box list There are no installed boxes! Use `vagrant box add` to add some.
Boxのダウンロード
D:\Vagrant\precise64>vagrant box add hashicorp/precise64 ==> box: Loading metadata for box 'hashicorp/precise64' box: URL: https://vagrantcloud.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://vagrantcloud.com/hashicorp/boxes/precise64/versions/1.1.0/providers/hyperv.box box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com box: Progress: 100% (Rate: 11.5M/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'hashicorp/precise64' (v1.1.0) for 'hyperv'! D:\Vagrant\precise64>vagrant box list hashicorp/precise64 (hyperv, 1.1.0)
Vagrantfileの修正
以下のように Vagrantfile
を修正します。
Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.box_version = "1.1.0" end
起動しない...
D:\Vagrant\precise64>vagrant up --provider=hyperv Bringing machine 'default' up with 'hyperv' provider... Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed. Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires this to be available on the PATH. If VirtualBox is installed, please find the `VBoxManage` binary and add it to the PATH environmental variable.
デバッグログを出力して調査
VAGRANT_LOG=debug vagrant up
でデバッグログを出力させるようにしました。ログを確認するとエラーになっている原因が推測できました。DEBUG base: Windows, checking for VBoxManage on PATH first
とあるようにWinodwsだとデフォルトで VBoxManage
のパスをチェックするようです。このパスが存在しないことが原因でエラーになっていると考えられます。
よって、Oracle VirtualBox
を再度インストールすることで vagrant up --provider=hyperv
で起動させることができました。
D:\Vagrant\precise64>VAGRANT_LOG=debug vagrant up INFO interface: info: Bringing machine 'default' up with 'hyperv' provider... Bringing machine 'default' up with 'hyperv' provider... INFO batch_action: Enabling parallelization by default. INFO batch_action: Disabling parallelization because provider doesn't support it: hyperv INFO batch_action: Batch action will parallelize: false INFO batch_action: Starting action: #<Vagrant::Machine:0x0000000005238980> up {:destroy_on_error=>true, :install_provider=>false, :parallel=>true, :provision_ignore_sentinel=>false, :provision_types=>nil} INFO machine: Calling action: up on provider Hyper-V (new) DEBUG environment: Attempting to acquire process-lock: machine-action-65facac4d5faa720c79dfc299d95a150 DEBUG environment: Attempting to acquire process-lock: dotlock INFO environment: Acquired process lock: dotlock INFO environment: Released process lock: dotlock INFO environment: Acquired process lock: machine-action-65facac4d5faa720c79dfc299d95a150 INFO interface: Machine: action ["up", "start", {:target=>:default}] INFO runner: Preparing hooks for middleware sequence... DEBUG base: Windows, checking for VBoxManage on PATH first INFO base: VBoxManage path: VBoxManage INFO environment: Released process lock: machine-action-65facac4d5faa720c79dfc299d95a150 INFO environment: Running hook: environment_unload INFO runner: Preparing hooks for middleware sequence... INFO runner: 1 hooks defined. INFO runner: Running action: environment_unload #<Vagrant::Action::Builder:0x00000000052ec8e0> ERROR vagrant: Vagrant experienced an error! Details: ERROR vagrant: #<Vagrant::Errors::VirtualBoxNotDetected: Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed. Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires this to be available on the PATH. If VirtualBox is installed, please find the `VBoxManage` binary and add it to the PATH environmental variable.> ERROR vagrant: Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed. Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires this to be available on the PATH. If VirtualBox is installed, please find the `VBoxManage` binary and add it to the PATH environmental variable. ERROR vagrant: C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/plugins/providers/virtualbox/driver/meta.rb:51:in `rescue in block in initialize'
起動させる
D:\Vagrant\centos7>vagrant up Bringing machine 'server1' up with 'hyperv' provider... ==> server1: Verifying Hyper-V is enabled... ==> server1: Verifying Hyper-V is accessible... server1: Configuring the VM... ==> server1: Starting the machine... ==> server1: Waiting for the machine to report its IP address... server1: Timeout: 120 seconds server1: IP: 192.168.20.84 ==> server1: Waiting for machine to boot. This may take a few minutes... server1: SSH address: 192.168.20.84:22 server1: SSH username: vagrant server1: SSH auth method: private key ==> server1: Machine booted and ready! ==> server1: Setting hostname... ==> server1: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> server1: flag to force provisioning. Provisioners marked to run always will still run.
D:\Vagrant\centos7>vagrant ssh Last login: Sun Apr 7 08:48:58 2019 from 192.168.20.81 [vagrant@server1 ~]$ cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
まとめ
Hyper-VでVagrantを扱う場合でもVirtualBoxのインストールは必要であることが分かりました。Hyper-Vを使う場合でもVirtualBoxが必要であることは盲点でした。。。