Moving the WSL2 vhdx

There are a lot of chances that your WSL2 environment size is going to blow up. It is very easy for it to reach 100GB in size. In these cases it is better to move the vhdx file from the system drive to an external ssd or some other non system drive.

Although there are commands provided by WSL2 for these purposes with the flags --import and --export, these are not exactly stable.

The --import command has a bug wherein it needs ram, the size of vhdx to be imported correctly. Now obviously most of us dont have huge amounts of ram lying around.

Im going to show how you can do this an easy way without spending time doing import/export and waiting for those operations to blow up.

  1. Shutdown wsl using wsl --shutdown command.

  2. Find the path to your vhdx location. Refer Here

  3. Now copy this vhdx file to the place where you want to migrate to.

  4. Create a file with extension .reg and add these contents

    Windows Registry Editor Version 5.00
    [HKEY_USERS\SID\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{UUID}]
    "State"=dword:00000001
    "DistributionName"="distribution name"
    "Version"=dword:00000002
    "BasePath"="vhdx folder path"
    "Flags"=dword:0000000f
    "DefaultUid"=dword:000003e8

    • the vhdx folder path should be in the following format \\\\?\\E:\\WSL\\ubuntu-20
    • SID can be found by typing the following in command prompt whoami /USER
    • UUID can be generated randomly using anything. i used this website
    • distribution name can be any string identifier.
  5. Double click the file and let it add content to your registry.

  6. Run wsl --list and you should be able to see the new wsl entry created.

  7. Clear the older entry by using wsl --unregister command.

NOTE:

  1. If you get permission denied error, right click the .vhdx file, go the security and transfer the ownership of that file to the current user or the SID displayed above.
  2. If the path in above format does not work, you can try the folowing format \\?\E:\WSL\ubuntu-20\

Thanks for reading and i hope this helped some of you guys facing this problem.

Subscribe to Blog

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe