0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows 11でHyper-VをVagrantのプロバイダとして使う

0
Posted at

What's?

VagrantのプロバイダにHyper-Vが選べるようなので、導入してみようかなと。

環境

今回の環境はこちらです。

PS > [System.Environment]::OSVersion

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.26200.0 Microsoft Windows NT 10.0.26200.0


PS > $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      26100  8115


Vagrantをインストールする

Vagrantはwingetでインストールします。

PS > winget install Hashicorp.Vagrant

バージョン。

PS > vagrant version
Installed Version: 2.4.9

Vagrant was unable to check for the latest version of Vagrant.
Please check manually at https://www.vagrantup.com

Hyper-Vプロバイダを使う

VagrantのHyper-Vプロバイダのドキュメントはこちら。

最初にHyper-Vを有効にします。

PS > Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

Windowsを再起動。

この操作を完了するために、今すぐコンピューターを再起動しますか?
[Y] Yes  [N] No  [?] ヘルプ (既定値は "Y"):

Rocky Linux 9で試してみましょう。

PS > vagrant init generic-x64/rocky9

--provider hypervでプロバイダを指定して起動。

PS > vagrant up --provider hyperv

実行には管理者権限が必要で、一般ユーザで実行していると以下のようにHyper-Vプロバイダは管理者権限を要求すると怒られます。

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

The Hyper-V provider requires that Vagrant be run with
administrative privileges. This is a limitation of Hyper-V itself.
Hyper-V requires administrative privileges for management
commands. Please restart your console with administrative
privileges and try again.

管理者権限で実行すると、仮想マシンが起動しました。

Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
==> default: Verifying Hyper-V is accessible...
==> default: Box 'generic-x64/rocky9' could not be found. Attempting to find and install...
    default: Box Provider: hyperv
    default: Box Version: >= 0
==> default: Loading metadata for box 'generic-x64/rocky9'
    default: URL: https://vagrantcloud.com/api/v2/vagrant/generic-x64/rocky9
==> default: Adding box 'generic-x64/rocky9' (v4.3.12) for provider: hyperv (amd64)
    default: Downloading: https://vagrantcloud.com/generic-x64/boxes/rocky9/versions/4.3.12/providers/hyperv/amd64/vagrant.box
    default:
    default: Calculating and comparing box checksum...
==> default: Successfully added box 'generic-x64/rocky9' (v4.3.12) for 'hyperv (amd64)'!
==> default: Importing a Hyper-V instance
    default: Creating and registering the VM...
    default: Successfully imported VM
    default: Configuring the VM...
    default: Setting VM Enhanced session transport type to disabled/default (VMBus)
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
    default: Timeout: 120 seconds
    default: IP: 172.29.97.185
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 172.29.97.185:22
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    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!

SSHログインして確認。

PS > vagrant ssh

OKですね。

$ cat /etc/redhat-release
Rocky Linux release 9.3 (Blue Onyx)

デフォルトのプロバイダをHyper-Vにしたい場合は、環境変数VAGRANT_DEFAULT_PROVIDERhypervに設定します。
$PROFILEあたりに設定するとよいでしょう。

$Env:VAGRANT_DEFAULT_PROVIDER = "hyperv"

これで--provider hypervをつけなくてもよくなります。

PS > vagrant up

Box利用時のHyper-Vプロバイダの設定はこちらを参照。

こんな感じで設定するんですね。

config.vm.provider "hyperv" do |h|
  h.vm_integration_services = {
    guest_service_interface: true,
    CustomVMSRV: true
  }
end
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?