The latest versions of WSL2 include a new wsl --import --vhd
option to directly copy the existing VHD (virtual SDD drive for your WSL2 distribution) to another location. Note that this will only work with WSL2 distributions, not WSL1.
Also note that it is important to pick a new, different name for the copied distribution. For instance, if your distribution is "Ubuntu", the new name could be "my_Ubuntu" (I call mine "ntd_Ubuntu").
Exit WSL (and, if you use it, shut down Docker Desktop), then from PowerShell (a regular, non-admin terminal):
wsl --shutdown
# Find the location of the existing distribution:
Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss\ |
ForEach-Object {
(Get-ItemProperty $_.PSPATH) | Select-Object DistributionName,BasePath
}
# Copy the <BasePath> of the distribution you want to copy
# to the clipboard so that you can paste it into the --import command
# below.
# Create a directory on the other drive for the distro
mkdir d:\WSL\distros\<new_distro_name>
wsl --import <new_distro_name> d:\WSL\distros\<new_distro_name> <BasePath>\ext4.vhdx --vhd
WSL will create a new distribution named <new_distro_name>
in the directory on D:
.
You can start this distribution with:
wsl ~ -d <new_distro_name>
Because it is a new distribution, WSL needs to be told what the default username is. In the meantime, it will start as the root user. Use this answer to create a /etc/wsl.conf
file to specify the default user.
When you are satisfied that the new distribution is working the same as the old, you can remove the old with:
# Warning: This is a destructive and unrecoverable operation
# Please ensure the correct distribution name
wsl --unregister <old_distro_name>
You can set the new distribution as default with:
wsl --set-default <new_distro_name>
docker-desktop-data
distro on D:\ :)