Software & AppsOperating SystemLinux

Where Did My Disk Space Go? Understanding WSL2 Storage Allocation

Ubuntu 19

When working with Windows Subsystem for Linux 2 (WSL2), you may have noticed a discrepancy between the disk space reported by Windows and the disk space allocated within WSL2. This can lead to confusion and the question, “Where did my disk space go?” In this article, we will delve into the details of WSL2 storage allocation to help you understand this better.

Quick Answer

When working with WSL2, the disk space allocated within the Linux environment does not directly affect the available disk space reported by Windows. The virtual disk used by WSL2 is "sparse" and grows as needed but does not automatically release space when files are deleted within WSL2. If you need to reclaim space on your Windows drive, you can manually shrink the virtual disk image.

WSL2 and Its Sparse Virtual Disk

WSL2 utilizes a virtual disk (ext4.vhdx) that is “sparse”. This means that while it appears to the underlying Windows operating system as its maximum available size, it only takes up as much space on the host disk as it currently needs. This virtual disk file can be found in Windows at %userprofile%\AppData\Local\Packages\Canonical..\LocalState\ext4.vhdx.

Understanding Disk Space Allocation

When you set up a Linux distribution, such as Ubuntu 20.04, using WSL2, the disk space allocated within the Linux environment does not directly affect the available disk space reported by Windows. For instance, you might see that Ubuntu has 250 GB of disk space. However, Windows still shows the same amount of free space as before.

This is because the allocated space for WSL2 is not immediately reflected in the available space reported by Windows. When you create files or use disk space within WSL2, the size of the ext4.vhdx file will grow accordingly. However, when you delete files within WSL2, the space is released within the Linux environment but not automatically reclaimed on the Windows side.

Reclaiming Disk Space

If you notice that your Windows drive is running out of space, you may need to manually shrink the ext4.vhdx file. You can do this by running a Linux command to zero out the free space within the Linux environment. This command is fallocate -z -l <size> <file>, where <size> is the amount of space to free and <file> is the file to create. After running this command, you can then compact the ext4.vhdx file from Windows.

Here is an example:

# Zero out free space
fallocate -z -l 1G ~/dummyfile
# Remove the dummy file
rm ~/dummyfile

After running these commands in the Linux environment, you can then compact the ext4.vhdx file from Windows using the Optimize-VHD PowerShell command:

Optimize-VHD -Path <path to ext4.vhdx> -Mode Full

Conclusion

In summary, the allocated disk space within WSL2 does not affect the available space reported by Windows. The virtual disk used by WSL2 is “sparse” and grows as needed but does not automatically release space when files are deleted within WSL2. If you need to reclaim space on your Windows drive, you can manually shrink the virtual disk image.

Understanding how WSL2 manages disk space can help you better manage your system’s resources and avoid potential issues. Remember to regularly monitor your system’s disk usage and take appropriate actions when necessary to ensure optimal performance.

Why is there a discrepancy between the disk space reported by Windows and the disk space allocated within WSL2?

The discrepancy is because WSL2 utilizes a sparse virtual disk (ext4.vhdx). This means that it only takes up as much space on the host disk as it currently needs, while appearing as its maximum available size to the Windows operating system.

Where can I find the virtual disk file used by WSL2?

The virtual disk file (ext4.vhdx) can be found in Windows at %userprofile%\AppData\Local\Packages\Canonical..\LocalState\ext4.vhdx.

Does the disk space allocated within WSL2 directly affect the available disk space reported by Windows?

No, the allocated disk space within WSL2 does not directly affect the available space reported by Windows. The allocated space for WSL2 is not immediately reflected in the available space reported by Windows.

How can I reclaim disk space on my Windows drive when using WSL2?

To reclaim disk space on your Windows drive, you can manually shrink the ext4.vhdx file. This can be done by running a Linux command to zero out the free space within the Linux environment, and then compacting the ext4.vhdx file from Windows using the Optimize-VHD PowerShell command.

Can you provide an example of how to zero out free space and compact the `ext4.vhdx` file?

Sure! Here is an example:

# Zero out free space
fallocate -z -l 1G ~/dummyfile
# Remove the dummy file
rm ~/dummyfile

After running these commands in the Linux environment, you can then compact the ext4.vhdx file from Windows using the Optimize-VHD PowerShell command:

Optimize-VHD -Path <path to ext4.vhdx> -Mode Full

Leave a Comment

Your email address will not be published. Required fields are marked *