SolvedWSL how to move the vhdx of wsl2 to other disk

I install wsl2 , i learn that wsl2 use vhdx as store file, because C disk is small, so I want to move the vhdx file to other disk.

please tell me how to do it. thx

29 Answers

✔️Accepted Answer

Here is exactly what I did on 19035.1 and it worked without a reboot or errors.

  1. Run powershell.exe as Administrator
PS C:\WINDOWS\system32> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

# mkdir S:\ISOs\

PS C:\WINDOWS\system32> wsl --export Ubuntu S:\ISOs\ubuntu-wsl.tar

# mkdir w:\VMs

PS C:\WINDOWS\system32> cd w:\VMs
PS W:\VMs> mkdir ubuntu-wsl
PS W:\VMs> wsl --unregister Ubuntu
Unregistering...
PS W:\VMs> wsl --import Ubuntu W:\VMs\ubuntu-wsl S:\ISOs\ubuntu-wsl.tar
PS W:\VMs> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

Other Answers:

You can do this using the wsl --import feature. Here's an example of me doing this setup.

image

If you run into any more issues please reopen this issue, but otherwise I believe your question is solved so I'll close this out for you. Thank you!

If anybody is interested: I wrote a little bash and batch script for automating these steps.
https://github.com/pxlrbt/move-wsl

My steps is a combination of @rekaerst, @Neurrone, and @mxplusb

  1. Backup current vhdx file
    from %LOCALAPPDATA%\Packages\<PackageFamilyName>\LocalState\ext4.vhdx
    to D:\ext4.vhdx
    (Most important step. If you can't find correct vhdx file, you shouldn't go ahead)
  2. Remove current distro, like wsl --unregister Ubuntu-20.04
  3. Reinstall distro from Microsoft Store
  4. Export and import
    wsl --export Ubuntu-20.04 D:\wsl.tar
    wsl --unregister Ubuntu-20.04
    wsl --import Ubuntu-20.04 D:\Ubuntu-20.04 D:\wsl.tar
  5. Overwrite original vhdx file
    copy D:\ext4.vhdx D:\Ubuntu-20.04\ext4.vhdx
  6. Set default user
    ubuntu2004.exe config --default-user <username>

wsl --import --export command made a lot of disk io, can I just modify the registry of lxss?
I changed the registry key at

Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{your linux uuid}\BasePath 

from default to \\?\D:\Path\to\ext4.vhdx , then move ext4.vhdx to that path, reboot WSL2, and it seems work well.