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中虚拟化已经开启。

 

 

似乎存在一些混淆。通常情况下,WSL是在Windows操作系统上安装并使用的工具,它允许用户在不修改主机操作系统的前提下运行Linux环境。对于希望在Ubuntu中实现类似功能的需求,实际上应该考虑的是在一个已有的Linux环境中设置对其他虚拟化技术的支持,而不是反过来尝试在Linux里安装旨在为Windows提供Linux兼容性的组件。 如果目标是让Windows能够支持多种Linux发行版(比如Ubuntu),那么应当按照如下方式配置WSL: ### 在Windows环境下安装带有Ubuntu的WSL #### 启用开发者模式和可选功能 为了能够在Windows系统内启动WSL以及后续安装特定版本的Linux分发包如Ubuntu, 需要在PowerShell以管理员身份执行命令来开启必要的特性[^1]: ```powershell dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart ``` #### 安装适用于Linux的Windows子系统(WSL) 继续在同一窗口输入以下指令完成WSL本身的部署: ```powershell wsl --install ``` 这一步骤将会自动下载最新的长期稳定(LTS)版本的Ubuntu镜像文件,并将其设为默认使用的Linux分布[^3]. 然而,由于询问的内容涉及到了错误的方向——试图在Ubuntu上安装原本设计给Windows平台的软件模块,因此上述指导并不完全适用于此情境。相反,如果有意探索如何使现有的Ubuntu更加完善或增强其跨平台开发能力,则可以关注Docker容器解决方案、KVM/QEMU等全硬件辅助虚拟机方案或是Proxmox这样的开源服务器虚拟化管理平台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

denglin12315

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

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

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

打赏作者

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

抵扣说明:

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

余额充值