Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Last active April 13, 2023 14:26
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save offlinehacker/b1d96515f87a47bd0b0bea574eab5583 to your computer and use it in GitHub Desktop.
Save offlinehacker/b1d96515f87a47bd0b0bea574eab5583 to your computer and use it in GitHub Desktop.
WSL
[wsl2]
kernel=C:\\Users\\JAKA\\vmlinux

WSL notes

Rebuilding kernel with kvm support

This instructions are based on https://microhobby.com.br/blog/2019/09/21/compiling-your-own-linux-kernel-for-windows-wsl2/

  1. Clone source
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
cd WSL2-Linux-Kernel
  1. Reconfigure kernel

Enable kvm, as described here https://wiki.gentoo.org/wiki/QEMU#Kernel

make menuconfig KCONFIG_CONFIG=Microsoft/config-wsl
  1. Build kernel
make KCONFIG_CONFIG=Microsoft/config-wsl -j8
  1. Copy kernel
cp vmlinux /mnt/c/Users/<seuUser>/
  1. Copy .wslconfig file to user home

Starting WSL with KVM support

  1. Copy run-wsl.bat, attach.wdbg and patch_wsl_nested.js to same folder
  2. Fix path in attach.wdbg
  3. Open admin powershell in that folder
  4. Run ./run-wsl.bat and follow instructions
bm vmcompute!Marshal::JsonParser::JsonParser ".scriptrun C:\\Users\\JAKA\\Documents\\patch_wsl_nested.js"
#! /bin/sh
### BEGIN INIT INFO
# Provides: nix-daemon
# Required-Start:
# Required-Stop:
# X-Stop-After: sendsigs
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Starts the nix daemon
# Description: This is a daemon which enable the multi-user mode
# of the nix package manager.
### END INIT INFO
#
# Author: Michael Biebl <biebl@debian.org>
#
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="nix-daemon"
NAME=nix-daemon
NIX_DAEMON=nix-daemon
NIX_DAEMON_OPTIONS=--daemon
DAEMON=/nix/var/nix/profiles/per-user/root/profile/bin/nix-daemon
PIDFILE=/run/nix-daemon.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Define LSB log_* functions.
. /lib/lsb/init-functions
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# other if daemon could not be started or a failure occured
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON --background -- $NIX_DAEMON_OPTIONS
}
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# other if daemon could not be stopped or a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON
}
sendsigs_omit() {
OMITDIR=/run/sendsigs.omit.d
mkdir -p $OMITDIR
ln -sf $PIDFILE $OMITDIR/nix-daemon
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NIX_DAEMON"
do_start
case "$?" in
0) sendsigs_omit
log_end_msg 0 ;;
1) log_progress_msg "already started"
log_end_msg 0 ;;
*) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NIX_DAEMON"
do_stop
case "$?" in
0) log_end_msg 0 ;;
1) log_progress_msg "already stopped"
log_end_msg 0 ;;
*) log_end_msg 1 ;;
esac
;;
restart|force-reload)
$0 stop
$0 start
;;
try-restart)
$0 status >/dev/null 2>&1 && $0 restart
;;
status)
status_of_proc -p $PIDFILE $DAEMON $NIX_DAEMON && exit 0 || exit $?
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|try-restart|status}" >&2
exit 3
;;
esac
:
"use strict";
function initializeScript()
{
return [new host.apiVersionSupport(1, 3)];
}
function continueExecution() {
var cmd = "g";
host.diagnostics.debugLog(cmd);
var lines = host.namespace.Debugger.Utility.Control.ExecuteCommand(cmd)
for (var line of lines) host.diagnostics.debugLog(" ", line, "\n");
}
function invokeScript()
{
/* bp vmcompute!Marshal::JsonParser::JsonParser */
var cmd;
var lines;
// 1. Check if WSL
var magic = host.memory.readWideString(host.currentThread.Registers.User.rdx, 14);
if (magic == '{"Owner":"WSL"') {
host.diagnostics.debugLog("IS WSL\n");
} else {
host.diagnostics.debugLog("IS NOT WSL request\n");
return continueExecution();
}
// dump length and read machine spec json
var len = host.currentThread.Registers.User.r8;
host.diagnostics.debugLog("String length: ", len, " dumping memory: ", host.currentThread.Registers.User.rdx, "\n");
var jsonString = host.memory.readWideString(host.currentThread.Registers.User.rdx, len);
host.diagnostics.debugLog("Before: ", jsonString, "\n");
// parse and modify machine spec json
var machineSpec = JSON.parse(jsonString);
machineSpec.VirtualMachine.ComputeTopology.Processor.ExposeVirtualizationExtensions = true;
var machineSpecJson = JSON.stringify(machineSpec);
host.diagnostics.debugLog("After: ", JSON.stringify(machineSpec), "\n");
var newLen = "0x" + machineSpecJson.length.toString(16);
// allocate memory
cmd = ".dvalloc 4096"
lines = host.namespace.Debugger.Utility.Control.ExecuteCommand(cmd)
var addr = lines[0];
var addrParts = addr.split(" ");
var freeMem = addrParts[addrParts.length-1].replace("`", "");
host.diagnostics.debugLog("Allocated ", freeMem, "\n");
// write memory
host.diagnostics.debugLog("Writing memory ", freeMem, " length: ", newLen, "\n");
cmd = "eu " + freeMem + ' "' + machineSpecJson.split("\\").join("\\\\").split('"').join('\\"') + '"';
lines = host.namespace.Debugger.Utility.Control.ExecuteCommand(cmd)
for (var line of lines) host.diagnostics.debugLog(" ", line, "\n");
// patch rdx with new memory
var cmd = "r @rdx = " + freeMem;
host.diagnostics.debugLog(cmd);
var lines = host.namespace.Debugger.Utility.Control.ExecuteCommand(cmd)
for (var line of lines) host.diagnostics.debugLog(" ", line, "\n");
// patch r8 with new memory size
var cmd = "r @r8 = " + newLen;
host.diagnostics.debugLog(cmd);
var lines = host.namespace.Debugger.Utility.Control.ExecuteCommand(cmd)
for (var line of lines) host.diagnostics.debugLog(" ", line, "\n");
return continueExecution();
}
REM Ensure vmcompute.exe is running
wsl.exe --shutdown
wsl.exe -e true
REM Listen for and Intercept utility vm creation
start Windbgx.exe -pn vmcompute.exe -c "$$<attach.wdbg;g"
REM Ensure WSL Utility VM is not running (hopefully windb starts up fast enough...)
net stop LxssManager
net start LxssManager
echo "Press Enter if the debugger is running"
pause
REM Start WSL
start wsl.exe
echo "Detach debugger from process, check if you have kvm support using kvm-ok command"
@ThaDaVos
Copy link

ThaDaVos commented May 22, 2020

EDIT: Nevermind, it switched the kernel... neofetch is showing the wrong info - my ubuntu image used a different kernel, my debian one doesn't
I followed all these steps, but after changing the kernel and running neofetch it shows the exact same kernel, even though I changed the name in the menu

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