currently I am using wsl2 and I notice a significant amount of disk space is required for ext4.vhdx, like I know it's Ubuntu's file management system, but I don't deeply understand what's inside, that's why I want to look inside and delete all unnecessary files, how can I do it? like I know wsl2 doesn't release used disk space back to the host system (I am using windows 10), so that's why I want to see what disk storage does ubuntu holds.
2 Answers
@thomas-ward's answer is correct, but it seems to me that there is an additional "question inside your question" that still needs answered.
To start with, to re-word the other answer, you can most easily see the entire contents of ext4.vhdx
by simply running WSL and looking at the files. For instance:
sudo find / -xdev | less
That will find all files on the ext4
filesystem mounted as root (/
), but not any files on your mounted NTFS/Windows drives (the -xdev
excludes other filesystems).
You can also see the storage being used by those same files with:
sudo du -hdx
If that amount differs from the size of the ext4.vhdx
file, then yes, as you already suspect, you likely have unreclaimed space. As you mentioned in your question, WSL will grow that file as needed, but it will not automatically shrink it when files are removed.
To manually shrink the file, you can follow any of the steps I mention at this Super User answer. Here's one of them, copied from a referenced Github comment:
wsl --shutdown
# At this point, it is recommended that you back up your ext.vhdx and delete the backup once you have confirmed the other commands are successful.
diskpart
# open window Diskpart
select vdisk file="C:\WSL-Distros\…\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit
Note: An alternative way of accessing the vhdx
directly would be to spin up a new Linux-based VM on Hyper-V and add that virtual drive (or a copy of it) to the instance. It isn't possible to examine it directly in Windows since Windows doesn't understand the ext4 filesystem.
Examining the VHDX directly is not advised on Windows because it contains an ext4
filesystem.
Essentially, what the VHDX holds is the entire Ubuntu operating system on it, plus any additional files/programs/libraries/etc. you've installed within WSL onto it. It is the entire 'hard disk' of the system that WSL2 uses with virtualization to run the Ubuntu WSL environment. Basically, any file you can navigate to in WSL and see (/usr/lib/...
for example) is what is in the VHDX.
-
so VHDX contains a copy of my whole computer's disk? i don't really understand. what does it contain? Apr 29, 2021 at 16:36
-
1@datonefaridze It contains *all the files that WSL2 needs to execute and run, all the Linux executables, all the packages installed inside WSL2 by
apt
, etc.). It does *not contain "all the files on your computer" - only the Ubuntu specific files, which is basically the Ubuntu operating system as it runs inside WSL2. Apr 29, 2021 at 16:59