Closed turbotimon closed 7 months ago
This is good timing @turbotimon ...there hasn't been an easy way to reduce the VHD size after expanding, but the WSL dev team just added a new sparseVhd
command in the experimental features: https://devblogs.microsoft.com/commandline/windows-subsystem-for-linux-september-2023-update/#automatic-disk-space-clean-up-set-sparse-vhd#automatic-disk-space-clean-up-set-sparse-vhd
If you're on the latest version of WSL 2 (may want to run wsl --update to make sure), you should be able to use this command to enable the experimental sparseVhd
setting: wsl --manage <distro> --set-sparse <true/false>
You'll want to make sure that you aren't running the distro that you are reducing (use wsl --shutdown
to shut it down). Then use that command with the name of your distro and set sparse to true
. This should automatically shrink the size of your VHD.
Let us know how it goes! *This is also a good reminder for me to add a link in the Disk Management doc.
Hi @mattwojo and thanks for your quick help!
I was able to compact the disk with the already existing command compact vdisk
in diskpart
. So now the ext4.vhdx
file is about the size of used disk space in my wsl ubuntu. I think adding information about compact vdisk
to the documentation would already help a lot of people. But i will try out sparseVhd
as well.
However, i was not able do reduce the maximum available disk space, meaning what wsl is allowed to use. Example: The disk is shown with 251G which i want to reduce to say 100G. I'm not sure if just sudo resize2fs /dev/sdb <sizeInMegabytes>M
would be enough?
Filesystem Size Used Avail Use% Mounted on
/dev/sdc 251G 21G 218G 9% /
Hi @turbotimon yes you could run resize2fs and it would be enough to show the disk size as smaller than its maximum if you so wish.
compact vdisk
didn't work for me - size didn't change. Due to various Docker experiments size of the ext4.vhdx has grown to 90GB.
WSL2 2.0.9.0 + Ubuntu.
Is sudo fstrim -av
necessary in current WSL2 before compact vdisk ...
? I always do it, and compaction works as expected.
If i set sparseVhd=True
, will the performance of wsl be reduced?
@yangyang233333 , for me performance was about the same, but it is still experimental feature, and I've got host & guest filesystem corruption after using it for some time (and calling fstrim
inside VM). So I'd stick to the old method for the time being...
Here below:
However, i was not able do reduce the maximum available disk space, meaning what wsl is allowed to use. Example: The disk is shown with 251G which i want to reduce to say 100G. I'm not sure if just
sudo resize2fs /dev/sdb <sizeInMegabytes>M
would be enough?Filesystem Size Used Avail Use% Mounted on /dev/sdc 251G 21G 218G 9% /
@turbotimon would you kindly provide more information regarding how to reduce the maximum available disk space wsl is allowed to use? I would like to do the same operation but I do not understand where and with which arguments you run the commands to resize it.
For anyone coming to this thread from google please consider this issue: https://github.com/microsoft/WSL/issues/10609 before setting your distribution to sparse as you may be setting yourself up for a catastrophic failure.
Documentation Issue
The documentation has a section about "How to expand the size of your WSL 2 Virtual Hard Disk" but not about "How to reduce the size of your WSL 2 Virtual Hard Disk". Background: I expanded it for a project according to this documentation, now i need to shrink it because i don't need it anymore.
Link to documentation page
https://learn.microsoft.com/en-us/windows/wsl/disk-space
Suggested Improvements
Add section about "How to reduce the size of your WSL 2 Virtual Hard Disk"