Skip to content

Instantly share code, notes, and snippets.

@afmiguez
Last active November 17, 2024 19:18
Show Gist options
  • Save afmiguez/44c50ea146ea27f94f0696664afc8128 to your computer and use it in GitHub Desktop.
Save afmiguez/44c50ea146ea27f94f0696664afc8128 to your computer and use it in GitHub Desktop.
Script to enable KVM in WSL2
#Fully based on https://boxofcables.dev/accelerated-kvm-guests-on-wsl-2/
if [ -z "$1" ]
then
echo "Must supply your Windows 10 username"
exit
fi
WIN_USERNAME=$1
#package updates and installations
sudo apt update && sudo apt -y upgrade
sudo apt -y install build-essential libncurses-dev bison flex libssl-dev libelf-dev cpu-checker qemu-kvm aria2
#download WSL2 Kernel and backup config file
cd ~
aria2c -x 10 https://github.com/microsoft/WSL2-Linux-Kernel/archive/4.19.104-microsoft-standard.tar.gz
tar -xf WSL2-Linux-Kernel-4.19.104-microsoft-standard.tar.gz
cd ./WSL2-Linux-Kernel-4.19.104-microsoft-standard/
cp ./Microsoft/config-wsl .config
#add properties to avoid using make menuconfig command (CONFIG_VHOST_NET and CONFIG_VHOST are already in the file)
echo 'KVM_GUEST=y
CONFIG_KVM=y
CONFIG_KVM_INTEL=m' >> .config
#build the kernel
make -j 8
#install kernel modules
sudo make modules_install
#install the new wsl2 kernel
cp arch/x86/boot/bzImage /mnt/c/Users/$WIN_USERNAME/bzImage
echo '[wsl2]
nestedVirtualization=true
kernel=C:\\Users\\'$WIN_USERNAME'\\bzImage' > /mnt/c/Users/$WIN_USERNAME/.wslconfig
#create kvm-nested.conf file and moves to /etc/modprobe.d/ folder
echo 'options kvm-intel nested=1
options kvm-intel enable_shadow_vmcs=1
options kvm-intel enable_apicv=1
options kvm-intel ept=1' > kvm-nested.conf
sudo mv kvm-nested.conf /etc/modprobe.d/
exit
@krad246
Copy link

krad246 commented Mar 12, 2024

Thanks for this script.

@skyzea1
Copy link

skyzea1 commented Apr 13, 2024

Do you have script to enable wsl2 on kvm

@strarsis
Copy link

strarsis commented Oct 28, 2024

A compilation error occurs (Ubuntu LTS 24.04) on Windows 10:

In file included from help.c:12:
subcmd-util.h: In function ‘xrealloc’:
subcmd-util.h:58:31: error: pointer ‘ptr’ may be used after ‘realloc’ [-Werror=use-after-free]
   58 |                         ret = realloc(ptr, 1);
      |                               ^~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to ‘realloc’ here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~
subcmd-util.h:56:23: error: pointer ‘ptr’ may be used after ‘realloc’ [-Werror=use-after-free]
   56 |                 ret = realloc(ptr, size);
      |                       ^~~~~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to ‘realloc’ here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~
  HOSTLD  scripts/mod/modpost
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/pager.o
cc1: all warnings being treated as errors
mv: cannot stat '/home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/.help.o.tmp': No such file or directory
make[4]: *** [/home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/build/Makefile.build:97: /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/help.o] Error 1
make[4]: *** Waiting for unfinished jobs....
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/builtin-check.o
make[3]: *** [Makefile:58: /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/libsubcmd-in.o] Error 2
make[2]: *** [Makefile:58: /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/libsubcmd.a] Error 2
make[2]: *** Waiting for unfinished jobs....
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/builtin-orc.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/check.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/orc_gen.o
  LD       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/arch/x86/objtool-in.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/orc_dump.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/elf.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/special.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/objtool.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/libstring.o
  CC       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/str_error_r.o
  LD       /home/user/WSL2-Linux-Kernel-4.19.104-microsoft-standard/tools/objtool/objtool-in.o
make[1]: *** [Makefile:63: objtool] Error 2
make: *** [Makefile:1650: tools/objtool] Error 2
  DEPMOD  4.19.104-microsoft-standard
Warning: modules_install: missing 'System.map' file. Skipping depmod.
cp: cannot stat 'arch/x86/boot/bzImage': No such file or directory
./wsl-kvm.sh: line 36: /mnt/c/Users/<redacted>/.wslconfig: No such file or directory

uname -r gives me this WSL kernel: 5.15.153.1-microsoft-standard-WSL2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment