Category: WSL

Compacting WSL hard disk

Like many people I am an extensive user of WSL and Linux under Windows in general. It’s the only real option I have at work and it’s quite a reasonable proposition.

That being said though, the WSL vhdx files can grow as you’re doing Linux work and while you can (and should) clean up side the Linux environment it’s not reflected back to windows as free space.

So how do you compact your WSL file?

  1. Shutdown your WSL system.

    wsl.exe --list --verbose # note the verbose is required to get the state
    Output from wsl.exe to show running WSL environment

    wsl.exe --terminate Ubuntu-24.04
    This shows the output of the wsl command with the instance being terminated
  2. Shrink the disk using diskpart

    diskpart

    The diskpart dialogue is displayed

    You need to select the vhdx file for your WSL instance. The VHDX file is typically found in your AppData folder. In my case it was this.

    Windows explorer show the location of my VHDX file

    I copy the VHDX file location as a path.

    The easiest way to get the full filename + path is to use explorer to copy as path

    DISKPART> select vdisk file="C:\Users\geoff\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"

    Output from selecting the vdisk in diskpart
  3. Compact the vdisk

    DISKPART> compact vdisk

    output of diskpart compact
  4. The results.

    BEFORE

    Explorer vhdx filesize before compact

    AFTER

    Explorer vhdx file size after compact

    As you can see, i’ve freed up nearly 6Gb.

Getting WSL2 just right

Recent changes to WSL2 by Microsoft have made using Linux on Windows even more comfortable. I’ll describe some of the options and their use below.

Firstly, do you have WSL2 installed? If not, then this will help https://learn.microsoft.com/en-us/windows/wsl/install-manual#step-1—enable-the-windows-subsystem-for-linux

In order to best use WSL, you of course need to have a distribution installed. Ubuntu is one of the easiest and most common to install.

wsl --list --online           #check what distros are available
wsl --install -d Ubuntu-24.04 #latest at the time of writing

Now that you have a distro installed, we have something to configure. There are 2 configuration files that customise the distribution experience under WSL2. wsl.conf and .wslconfig

wsl.conf contains per-distribution settings, whereas .wslconfig configures global settings for the WSL2 environment.

wsl.conf is stored in the /etc directory within the distribution.

.wslconfig is stored in your %UserProfile% folder.

.wslconfig

The .wslconfig file is in .ini format with the GA features found under section [wsl2]. There is also an [experimental] section for unreleased options.

Note: All options may not be available to you as they are Windows OS and WSL version dependent. You can reasonably assume if you are running Windows 11, 22H2 or higher that most of the options described below are available to you. This is not the complete list, just the one’s I have found to be quite useful

GA features that I find useful

Accessible via the [wsl2] section of the .wslconfig file

KeyValueNotes
memorymemory size (Mb, Gb)Default is 50% of the windows memory. I find it useful to constrain the memory (in conjunction with the experimental memory release features below)
processorsnumberDefault is the same as present in windows
localhostForwardingtrue/falseDefault is true, this allows your WSL2 application to be accessible via localhost:port
nestedVirtualizationtrue/falseAllow nesting inside WSL2, Windows 11+
networkingModestring, NAT, mirroredThe default is NAT, mirrored turns on mirrored networking mode. Mirrored mode is a great addition for many of my use cases.
firewalltrue / falseHyper-V firewall can filter WSL network traffic
dnsTunnelingfalsesee the experimental section
Some of the GA features

Experimental (though very useful) features

Accessible via [experimental] section of the .wslconfig file.

KeyValueNotes
autoMemoryReclaimdisabledDefault is disabled, but options list gradual and dropcache can dramatically return memory outside wsl2. I default to gradual.
sparseVHDfalseWhen set to true new VHD’s are created as sparse saving considerable disk with all the overprovisioning issues. By default, i’m using sparse, but then again i’ve been using sparse filesystems for many years
useWindowsDnsCachefalseIf you have dnsTunneling turned on then this option allows you to use or ignore what windows dns may’ve cached
hostAddressLoopbackfalseif networkingMode is set to mirrored then the loopback 127.0.0.1 address can be used to access the host and the container depending on where the listening resource may be running – windows or wsl2. This is a great option if you want better sharing between windows and wsl2 distro. For example, i’ve had a mongo client on windows and mongo in wsl2 ubuntu.
Some of the Experimental features

wsl.conf

As I mentioned above the /etc/wsl.conf within the distribution controls some interesting behaviours, especially on distro launch.

[boot]

systemd=true

Solves the problem where you’re reliant on systemd resources within your WSL2 distro. I normally have it turned on.

[automount]

KeyValueNotes
enabledtrue / falseAllows windows fixed drives to be automatically mounted under /mnt (or where the root key points). I have this enabled by default
root/mntWhere the mounts occur for auto mounted systems
mountFsTabtrue/falseAllow the /etc/fstab to be processed at WSL distro boot time. Great to get those SMB/NFS mounts going. I have this set to true as I use a lot of NFS in my test environment.

Some things to note.

Windows disks are mounted using Drvfs and are by default case sensitive. You can override this behaviour for all or single drives. More information is available at Per-directory case sensitivity and WSL – Windows Command Line (microsoft.com)

[network]

KeyValueNotes
generateHoststrue/falsewsl will generate an appropriate /etc/hosts file based on the windows environment. I generally set this to true (the default).
generateResolvConftrue/falsewsl will generate an appropriate list of dns resolvers. I generally set this to true (the default).
hostnamestringThis sets the hostname to be used within the distro. The default is the windows hostname, but this is useful if you run multiple WSL instances.

Navigation