📚
note
Blog
  • Initial page
  • JS-notes
    • 使用浏览器书签执行js代码
    • JSON.stringify的小技巧
    • Fisher–Yates shuffle洗牌算法
    • 打印页面
  • Web-notes
    • 在网页中引入在线字体
  • Uniapp-notes
    • swiper-item高度自适应
    • 微信小程序的图片预览兼容性处理
  • VUE-notes
    • vue-note
    • vue3-note
  • VPS-notes
    • CentOs7笔记
    • ssh小记
    • Ubuntu笔记
    • vps安全相关
    • [Google Drive笔记](VPS-notes/Google Drive笔记.md)
  • TypeScript-notes
    • ts热编译项目
    • TypeScript笔记
    • js项目转ts
  • Python-notes
    • Python爬虫笔记
    • Python笔记
  • PHP-notes
    • php笔记
    • php+redis
    • php-codeIgniter
    • php抽奖算法
    • Laravel笔记
  • Mobile-notes
    • 移动端常用样式及兼容相关
  • Linux-notes
    • linux常用指令
  • Game-notes
    • Minecraft-server
  • TelegramBot-notes
    • tg-bot小记
  • Windows-notes
    • window-note
    • node-note
    • WSL-note
  • RaspberryPi-notes
    • RaspberryPi-note
    • 其他玩法
    • Openwrt
    • Ubuntu安装指南
  • Phone-notes
    • ZenFone6-note
  • Cocos-notes
    • Cocos-note
  • Network-notes
    • 单线复用
  • Other-notes
    • 国际化地域标识码
Powered by GitBook
On this page
  • WSL 备份
  • WSL 还原
  • WSL 自动调整磁盘空间
  • WSL Docker 中调用宿主机显卡

Was this helpful?

  1. Windows-notes

WSL-note

Last updated 4 months ago

Was this helpful?

WSL 备份

使用 wsl --export 命令导出镜像,参数依次是:子系统名称、备份路径(一般为tar文件)

wsl --export Ubuntu-24.04 Ubuntu2404.tar
wsl --export Ubuntu-24.04 e:\wsl\Ubuntu2404.tar

WSL 还原

使用 wsl --import 命令还原镜像,参数依次是:子系统名称、安装文件夹、镜像文件路径

wsl --import Ubuntu-24.04 e:\wsl e:\wsl\Ubuntu2404.tar

WSL 自动调整磁盘空间

该小节部分内容引用

关于自动释放 WSL2 虚拟硬盘空间,需要设置稀疏 VHD。

首先在上面的配置里再加一行:

sparseVhd=true

然后运行这个命令切换到稀疏 VHD:wsl --manage 发行版名字 --set-sparse true

比如 wsl --manage Ubuntu --set-sparse true

同时,设置 WSL 的配置文件,增加:

[experimental]
autoMemoryReclaim=gradual # 可以在 gradual 、dropcache 、disabled 之间选择

注意: 如果你在 WSL 里使用 docker,那需要将 autoMemoryReclaim 配置为 dropcache 或者 disabled,然后在 /etc/docker/daemon.json 里添加一句 "iptables": false ,否则你可能无法正常使用 docker。

WSL Docker 中调用宿主机显卡

本小节提到的显卡是 Nvidia

  • 在 WSL 中运行以下命令安装 nvidia-container-toolkit:

  sudo apt-get update
  sudo apt-get install -y nvidia-container-toolkit
  • 在 WSL 中验证 GPU 的可用性:

  nvidia-smi

如果能显示类似下面的内容,则配置成功:

+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 560.35.02              Driver Version: 560.94         CUDA Version: 12.6     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 4060 ...    On  |   00000000:01:00.0 Off |                  N/A |
| N/A   49C    P8              2W /  125W |    1269MiB /   8188MiB |      2%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+

接着,在 Docker 中启用 GPU 的支持

  • 确保 Docker Desktop 已启用 WSL 2 集成:

    • 打开 Docker Desktop 设置。

    • 进入 Settings > Resources > WSL Integration,勾选你的 WSL 2 发行版。

  • 启用 GPU 支持: 编辑 Docker 的 daemon.json 文件,添加以下内容:

{
  "default-runtime": "nvidia",
  "runtimes": {
    "nvidia": {
      "path": "nvidia-container-runtime",
      "runtimeArgs": []
    }
  }
}

然后重启 Docker 服务。

然后运行以下命令检查 Docker 是否能识别 GPU:

docker run --rm --gpus all nvidia/cuda:12.6.3-cudnn-runtime-ubuntu24.04 nvidia-smi

首先去更新驱动并启动 CUDA。

如果提示:Unable to find image 错误,则需要先去找到适合你的系统的镜像,拉取下来。拉取到镜像后,将你拉取的镜像名替换掉上面命令的 nvidia/cuda:12.6.3-cudnn-runtime-ubuntu24.04 这部分,然后重新运行命令。

V2EX网友的贴文
官网
这里