在windows10的WSL里使用vagrant+Hyper-V

在windows10的WSL里使用vagrant+Hyper-V

在上一篇文章里Win10 Hyper-V和VirtualBox冲突的问题,提到了Win10的Hyper-V和VirtualBox冲突的问题,对于比较习惯于使用VirtualBox,或者Vagrant+VirtualBox的同学来说,最简单的就是关掉Hyper-V,但是关掉以后估计将来使用WSL2会是个问题。

今天在公司折腾了一下午,在win10的WSL里安装了Vagrant,并且启动Hyper-V作为Vagrant的provider来创建虚拟机,再加上VScode的远程开发(VScode remote WSL),真的是非常方便。如果将来升级到WSL2,原生能在WSL里安装docker就更好了。下面我就把安装配置的过程记录一下。

1. 安装WSL

没什么好说的,按照微软的官方文档即可,我选择的是Ubuntu 18.04。

https://docs.microsoft.com/en-us/windows/wsl/install-win10

2. 打开 Hyper-V

以管理员身份打开PowerShell,并且执行下面的命令把当前用户添加到Hyper-V的管理员组。

([adsi]”WinNT://./Hyper-V Administrators,group”).Add(“WinNT://$env:UserDomain/$env:Username,user”)

然后打开 Hyper-V:

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

按照系统提示,需要重启电脑

3. 安装 vagrant

进入WSL,从Vagrant的下载页面下载最新的Debian 64-bit版: https://www.vagrantup.com/downloads.html

CD到deb文件(比如vagrant_2.2.4_x86_64.deb)所在的目录,然后执行:

sudo dpkg -i vagrant_2.2.4_x86_64.deb

默认情况下,Vagrant是无法访问Windows主机里的Hyper-V特性的,首先通过的下面的命令让Vagrant能够access windows。

echo 'export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"' >> ~/.bashrc

然后设置Hyper-V为vagrant默认的provider。

echo 'export VAGRANT_DEFAULT_PROVIDER="hyperv"' >> ~/.bashrc

4. 测试 Vagrant

首先初始化一个Centos7的Vagrantfile

penxiao@WPU8L0042502:~$ mkdir centos
penxiao@WPU8L0042502:~$ cd centos/
penxiao@WPU8L0042502:~/centos$ ls
penxiao@WPU8L0042502:~/centos$ vagrant init centos/7
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.
penxiao@WPU8L0042502:~/centos$ ls
Vagrantfile
penxiao@WPU8L0042502:~/centos$ vagrant status
Current machine states:

default                   not_created (hyperv)

修改Vagrantfile

增加一行 config.vm.box_download_insecure = true 保存退出

config.vim.box = "centos/7"
config.vim.box_download_insecure = true

启动,SSH进入,测试。

penxiao@WPU8L0042502:~/centos$ vagrant up
Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
==> default: Verifying Hyper-V is accessible...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: hyperv
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v1905.1) for provider: hyperv
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/hyperv.box
    default: Download redirected to host: cloud.centos.org
==> default: Successfully added box 'centos/7' (v1905.1) for 'hyperv'!
==> default: Importing a Hyper-V instance
    default: Creating and registering the VM...
    default: Successfully imported VM
    default: Configuring the VM...
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
    default: Timeout: 120 seconds
    default: IP: 192.168.145.30
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 192.168.145.30: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!
penxiao@WPU8L0042502:~/centos$ vagrant status
Current machine states:

default                   running (hyperv)


penxiao@WPU8L0042502:~/centos$ vagrant ssh
[vagrant@rn05h1c12 ~]$
[vagrant@rn05h1c12 ~]$
[vagrant@rn05h1c12 ~]$
[vagrant@rn05h1c12 ~]$
[vagrant@rn05h1c12 ~]$
[vagrant@rn05h1c12 ~]$
[vagrant@rn05h1c12 ~]$
[vagrant@rn05h1c12 ~]$ exit
logout
Connection to 192.168.145.30 closed.
penxiao@WPU8L0042502:~/centos$ ls
Vagrantfile
penxiao@WPU8L0042502:~/centos$ ls -la
total 4
drwxrwxr-- 1 penxiao penxiao 4096 Aug 12 13:09 .
drwxr-xr-x 1 penxiao penxiao 4096 Aug 12 13:21 ..
drwxrwxr-- 1 penxiao penxiao 4096 Aug 12 13:05 .vagrant
-rw-rw-rw- 1 penxiao penxiao 3055 Aug 12 13:09 Vagrantfile
penxiao@WPU8L0042502:~/centos$

5. VScode remote WSL

https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl

Discussion