跳到主要内容

关于WSL上玩Ollama把我C盘写满了的事

· 阅读需 2 分钟
RibomBalt
CTF enthusiastist, GeoPhysics PhD, Amateur coder

TL;DR

  • WSL的磁盘文件在C盘,我这里默认位置是C:\Users\<username>\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
  • WSL磁盘空间会自动增长但不会收缩,可以用多种方法回收空间
  • 把C盘写满了会报错退出。
  • 可以用wsl --manage Ubuntu-22.04 --move /d/WSL/Ubuntu-22.04把WSL的磁盘文件移动到D盘。
  • 也可以用wsl --export Ubuntu-22.04 /d/WSL/Ubuntu-22.04.tar导出到tar文件,然后用wsl --import Ubuntu-22.04 /d/WSL/Ubuntu-22.04 /d/WSL/Ubuntu-22.04.tar导入到D盘。

ref

shrink_wsl.sh (run by msys2)

#!/bin/bash

echo "Shutting Down WSL... This may take a few minutes. Please close all WSL instances before proceeding."
read -p "Are you sure you want to shut down WSL? (y/n): " confirm

if [[ "${confirm,,}" != "y" ]]; then
echo "Operation cancelled."
exit 0
fi

# shutdown WSLService to prevent restarting
net stop WSLService
echo "WSL is shutting down..."

wsl --shutdown
echo "Running Diskpart to shrink WSL filesystem..."
diskpart -s "/d/path/to/scripts/shrink_wsl/shrink_wsl_diskpart.sh"
echo "Diskpart completed. WSL should now be shrunk."

net start WSLService
echo "WSLService restarted."
echo "Please restart WSL to apply changes."

diskpart scripts shrink_wsl_diskpart.sh

select vdisk file="D:\WSL\Ubuntu-22.04\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit