kvm on wsl2

一、安装教程

​​​​​​​Accelerated KVM guests on WSL 2 (boxofcables.dev)
​​​​​​​

二、执行sudo modprobe kvm_intel失败的解决方案(原因是WSL2是虚拟化没有开启)

开启WSL2虚拟化的方法如下:

Nested Virtualization for WSL2 VM · Issue #4193 · microsoft/WSL · GitHub

 run-wsl.bat:

REM Ensure vmcompute.exe is running
wsl.exe -e true
REM Listen for and Intercept utility vm creation
start Windbgx.exe -pn vmcompute.exe -c "bp vmcompute!Marshal::JsonParser::JsonParser;g;.scriptrun %CD%\script.js;.scriptrun %CD%\script.js;.scriptrun %CD%\script.js;.detach;qq"
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

 script.js:

"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();
}

在Microsoft store中搜索安装WinDbg:

以管理员身份运行powershell,  在powershell中运行run-wsl.bat文件,会打开wsl2 ubuntu的控制台,在控制台中执行以下命令检查虚拟化是否已经开启:

如果该命令输出一个非零的数,表示WSL2中虚拟化已经开启。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值