Running Virtualbox+Vagrant inside of WSL2 with nested virtualization

Alice Knag
2 min readJul 2, 2021

--

Virtualbox running natively inside of WSL2 with GUI support via WSLg

WSL2 and Virtualbox has always been a pain point. If you run Virtualbox in windows with WSL2 enabled, you are required to use the hyper-v paravirtualization engine, which doesn’t play nicely with vagrant. This simple limitation has restricted me to using WSL1 for more than a year.

I have discovered recently that you can use nested virtualization in WSL2 to bypass this limitation, running virtualbox inside WSL2 without any paravirtualization limitation, so long as you have the right kernel build set up with the right kernel modules.

To do this I build a custom kernel for WSL2 and got virtualbox running directly in WSL2 ( as opposed to inside a KVM box like I had set up before )

it required some combination of

CONFIG_MODULES=y
CONFIG_MODULE_SIG=n
CONFIG_SECURITY_LOADPIN=n
CONFIG_SECURITY_LOCKDOWN_LSM=n

config_module_sig kept getting re-enabled so I manually removed the if check so the module_sig_check function would always return 0 in kernel/module.c:2786

Then build the kernel and install the modules to /lib/modules

sudo make -j $(nproc)
sudo make -j $(nproc) modules_install

and once that was done just updated %userprofile%\.wslconfig to use the new kernel image, installed virtualbox-6.1, and was done

I have a branch of vagrant that disables the checks for WSL

to set that up I had to follow the building instructions for vagrant and also this instruction

cd ~
git clone https://github.com/askalice/vagrant
cd vagrant
bundle install
bundle — binstubs exec
export PATH="$(dirname ~/vagrant/exec)/exec:$PATH"

for parallel vagrant installs against windows you can set

export PATH="$(dirname ~/vagrant/exec)/exec:$PATH"
export VAGRANT_WSL_NESTED_VIRTUALIZATION=true
export VAGRANT_HOME=~/.vagrant-wsl2
export VAGRANT_DOTFILE_PATH=.vagrant-wsl2

in your .bashrc

--

--

No responses yet