Moving VM Location

By default, WSL2 VMs are stored on the system partition, which is troublesome especially with a small SSD. Fortunately, it is possible to move VMs to another location.

As of Apr 2021, there isn’t a builtin command that does this easily, so it needs to be done by exporting and reimporting the VM, specifying a new location to store it during the import process.

# if Docker is running, shut it down first
wsl --shutdown
wsl -l -v # check that all VMs are stopped
 
# export the VM into a tar file
wsl --export ubuntu-20.04 "D:\wsl-backups\ubuntu-20.04.tar"
 
# unregisters (deletes) the VM, make sure you have backed it up first
wsl --unregister ubuntu-20.04
 
# import from the tar file created earlier
# in this example, the VMs are stored in D:\documents\wsl2-vms
# in windows explorer, ensure that the folder does not have NTFS compression enabled (hyper-v machines cant start if stored in an NTFS compressed folder)
wsl --import ubuntu-20.024 "D:\documents\wsl2-vms\ubuntu-20.04" "D:\wsl-backups\ubuntu-20.04.tar" --version 2

Once the VM is recreated, the default username that WSL uses to log into it needs to be configured again, otherwise it’ll be defaulting to root:

ubuntu2004 config --default-user <some-user>

Instructions from this issue on Github.

Moving Docker VMs

The process for moving the location of VMs used by the WSL2 backend is identical:

# shut down docker first
# there are 2 VMs to move
 
wsl --export docker-desktop "D:\wsl-backups\docker-desktop.tar"
wsl --export docker-desktop-data "D:\wsl-backups\docker-desktop-data.tar"
 
wsl --unregister docker-desktop
wsl --unregister docker-desktop-data
 
wsl --import docker-desktop "D:\documents\wsl2-vms\docker-desktop" "D:\wsl-backups\docker-desktop.tar" --version 2
wsl --import docker-desktop-data "D:\documents\wsl2-vms\docker-desktop-data" "D:\wsl-backups\docker-desktop-data.tar" --version 2

Extremely annoyingly, the docker-desktop VM is moved back to the previous location on Docker upgrades. Fortunately, this VM is much smaller than the other one (which stores the actual image data).

Compacting WSL2 VMs

Technically this procedure is going to work for any hyper-v VM (.vmdx); it boils down to using diskpart to compact the VM:

diskpart
# in the diskpart prompt
select vdisk file="D:\documents\wsl2-vms\ubuntu\ext4.vhdx"
compact vdisk

Reference: How to Shrink a WSL2 Virtual Disk

Fix Broken Port Forwarding

If a service is running in WSL2 but you can’t access it from the Windows host by doing localhost::

  1. Shut down WSL2: wsl --shutdown
  2. In the start menu, search for “network reset” to access that settings screen, and choose reset now. Note that this will require reconfiguring network settings and re-entering wifi passwords.
  3. Restart the machine. Port forwarding should work properly again.

Found this solution in this Github issue

Slow Performance When Accessing Windows Filesystem

Accessing /mnt from WSL2 is extremely slow.

A workaround is to use Mutagen

Unwanted Zone.Identifier Files

Copying files from the host to the linux VM via windows explorer will generate an extra Zone.Identifier file for each file.

Here’s a temporary workaround:

  1. Open gpedit.msc
  2. Open User Configuration>Administratives Templates>Windows Components>Attachment Manager.
  3. Double-click on “Do not preserve zone information in file attachment” and set it to enabled, then press ok