WSL icon indicating copy to clipboard operation
WSL copied to clipboard

WSL Sparse does not work at all.

Open PeterTucker opened this issue 1 year ago • 15 comments

Windows Version

Microsoft Windows [Version 10.0.19045.4894]

WSL Version

2.2.4.0

Are you using WSL 1 or WSL 2?

  • [X] WSL 2
  • [ ] WSL 1

Kernel Version

5.15.153.1-microsoft-standard-WSL2

Distro Version

Ubuntu 22.04

Other Software

No response

Repro Steps

Sparse does not work.

Ubuntu disk space (.vhdx file) shows 118gbs in windows 10. In Ubuntu: /ncdu / --exclude /mnt shows 64gbs

Powershell(admin): notepad "$env:USERPROFILE/.wslconfig" .wslconfig:

[experimental]
sparseVhd=true

Powershell(admin):

wsl --list # list distros
wsl --manage <distro> --set-sparse true # enable sparse on vdisk
wsl --shutdown # restart wsl
wsl # restart wsl 

Then when I check the size, it has not changed at all, still 118gbs.

SOLUTION (MANUAL SHRINKING):

for anyone who finds this in the future. Here is a way to resize the disk manually.

Powershell (admin):

FIRST BACKUP YOUR VHDX FILE
wsl --list # list distros
wsl --shutdown # Be patient and wait a bit for wsl to shutdown. It will eventually shutdown.
wsl --manage <DISTRO_NAME> --set-sparse false
optimize-vhd -Path "<VHDX_FILE>" -Mode full
wsl --manage <DISTRO_NAME> --set-sparse true

Then test if the VHDX file (wiztree) has shrunk and your distro can run.

Expected Behavior

Reclaim disk space in Windows 10.

Actual Behavior

The sparseVhd setting does not reclaim disk space in Windows 10.

Diagnostic Logs

No response

PeterTucker avatar Sep 29 '24 18:09 PeterTucker

Logs are required for review from WSL team

If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'. Otherwise please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.

How to collect WSL logs

Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:

Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1

The script will output the path of the log file once done.

If this is a networking issue, please use collect-networking-logs.ps1, following the instructions here

Once completed please upload the output files to this Github issue.

Click here for more info on logging If you choose to email these logs instead of attaching to the bug, please send them to [email protected] with the number of the github issue in the subject, and in the message a link to your comment in the github issue and reply with '/emailed-logs'.

View similar issues

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar Sep 29 '24 18:09 github-actions[bot]

/question

PeterTucker avatar Sep 29 '24 18:09 PeterTucker

Why does it not work?

PeterTucker avatar Sep 29 '24 18:09 PeterTucker

Diagnostic information
Found '/question', adding tag 'question'

github-actions[bot] avatar Sep 29 '24 18:09 github-actions[bot]

@PeterTucker after wsl --manage <DISTRO_NAME> --set-sparse false do a wsl --shutdown again, and it should work.

Notice the FileSize before and after running the command:

PS C:\Users\elsaco> Get-VHD -Path "G:\vms\sid\ext4.vhdx"

ComputerName            : ELEVEN
Path                    : G:\vms\sid\ext4.vhdx
VhdFormat               : VHDX
VhdType                 : Dynamic
FileSize                : 1081081856
Size                    : 549453824000
MinimumSize             :
LogicalSectorSize       : 512
PhysicalSectorSize      : 4096
BlockSize               : 1048576
ParentPath              :
DiskIdentifier          : 40545B0A-6EE6-483B-AE7D-B64B4EF39DB1
FragmentationPercentage : 100
Alignment               : 1
Attached                : False
DiskNumber              :
IsPMEMCompatible        : False
AddressAbstractionType  : None
Number                  :

PS C:\Users\elsaco> Optimize-VHD -Path "G:\vms\sid\ext4.vhdx" -Mode full
PS C:\Users\elsaco> Get-VHD -Path "G:\vms\sid\ext4.vhdx"

ComputerName            : ELEVEN
Path                    : G:\vms\sid\ext4.vhdx
VhdFormat               : VHDX
VhdType                 : Dynamic
FileSize                : 1067450368
Size                    : 549453824000
MinimumSize             :
LogicalSectorSize       : 512
PhysicalSectorSize      : 4096
BlockSize               : 1048576
ParentPath              :
DiskIdentifier          : 40545B0A-6EE6-483B-AE7D-B64B4EF39DB1
FragmentationPercentage : 100
Alignment               : 1
Attached                : False
DiskNumber              :
IsPMEMCompatible        : False
AddressAbstractionType  : None
Number                

elsaco avatar Sep 30 '24 01:09 elsaco

~It wasn't working for me on stable. After updating to the latest pre-release (2.4.8), it seems to work as expected: deleting files inside wsl shrinks the vdhx (after wsl shuts down).~

Edit: Doesn't seem to work reliabily. The size doesn't go down when files are deleted.

tusharsnx avatar Jan 24 '25 16:01 tusharsnx

@tusharsnx Well, that's good to hear. I just transferred all my stuff to Cygwin. Might have to do a test with docker tonight. Thanks for the update!

PeterTucker avatar Jan 27 '25 00:01 PeterTucker

Just tried 2.4.10, works partially. Ubuntu, ext4.

  • wsl --shutdown & wsl --manage Ubuntu --set-sparse true

test1

  • start wsl
  • create large files -> host free space reduced
  • remove files
  • wsl --shutdown -> host free space restored ok

test2

  • start wsl
  • crete large files -> host free space reduced
  • wsl --shutdown
  • start wsl
  • remove files
  • wsl --shutdown -> host free space is not restored

dporf avatar Feb 03 '25 17:02 dporf

wsl 2.5.9.0 still the same

dporf avatar Jul 31 '25 12:07 dporf

It doesn't work for me either. I had to use sudo fstrim -av (in Linux terminal) first, then disable sparse mode (wsl --manage Debian --set-sparse false) and then optimize-vhd -Path "<vhdx_file>" -Mode full to reduce the disk image from 211 GB to 20 GB.

SultanovIlnur avatar Aug 06 '25 14:08 SultanovIlnur

There hasn't been any confirmation from the WSL team for why sparse vhds are failing to shrink on its own. Is something wrong with our setup? Or is --set-sparse just broken as of today?

Can we get a simple manual command to optimize WSL vhdx files (that works for Win Home users)? For example, wsl --manage Ubuntu --optimize-vhd

tusharsnx avatar Sep 23 '25 12:09 tusharsnx

Does anyone know which part of code may do the job? Is it in this or some other repo? As far as I understand WSL should hook trim/discard kernel calls and make FSCTL_SET_ZERO_DATA request

dporf avatar Nov 11 '25 11:11 dporf

Devices needs to be mounted with discard option enabled. (Seems to be default for Ubuntu, not sure if this is default for Debian.) In addition sudo fstrim -av can trigger trimming manually.

Both works for me more or less with WSL-Version 2.6.1.0, Ubuntu 22.04 and 24.04. Also I noticed that not all space seems to be freed. Used space reported by df -h is 191G, ext4.vhdx size is 312GB. But after deleting large files, docker containers or the like ext4.vhdx shrinks - at least a bit.

slo617 avatar Nov 19 '25 09:11 slo617

@slo617

can trigger trimming manually

For me it just triggers vhd purge on wsl shutdown in some cases. What is really should do is to force host system to mark regions of vhd as empty and free allocated space imediately, this is what sparse files for.

Current behaviour may be easily tested with command fsutil file layout ext4.vhdx (may require admin rights). Just compare file size and allocated size before and after deleting files + fstrim. Allocated size should change immediately after executing fstrim but it does not.

You can mark some regions as empty manually (BE CAREFUL! IT ERASES DATA REGIONS!):

# set sparse flag
fsutil sparse setflag "1.bin"
# print file layout
fsutil file layout 1.bin
# mark a region as empty
fsutil sparse setrange 1.bin 0 999999999
# pring file layout again
fsutil file layout 1.bin

So WSL should catch BLKDISCARD ioctl in VM and execute symmetrical FSCTL_SET_ZERO_DATA ioctl in the host system.

dporf avatar Nov 19 '25 15:11 dporf

this does not work for me either wsl --shutdown & wsl --manage Ubuntu --set-sparse true also vhd opmtize not available on windows 11 home

zillur01 avatar Nov 30 '25 19:11 zillur01