Moving your WSL2 file system to another drive can be a smart move for several reasons. It helps free up space on your primary drive, especially if it’s a smaller SSD, and can improve overall system performance by reducing the load on your main drive. This process involves exporting your current WSL2 instance and importing it to a new location, ensuring you maintain all your configurations and data.
To move your WSL2 file system to another drive, follow these prerequisites:
Software and Tools:
Permissions:
Preparatory Steps:
wsl --list --verbose
whoami
wsl --shutdown
mkdir D:\backup
wsl --export <DistroName> D:\backup\<DistroName>.tar
wsl --unregister <DistroName>
mkdir D:\WSL
wsl --import <DistroName> D:\WSL\ D:\backup\<DistroName>.tar
cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
<DistroName> config --default-user <YourUsername>
Ensure you replace <DistroName>
and <YourUsername>
with your actual distribution name and username.
Here’s a detailed, step-by-step guide to move your WSL2 file system to another drive:
Open PowerShell and list your current WSL2 distributions:
wsl --list --verbose
!Step 1
If the distribution you want to move is running, stop it:
wsl --terminate <DistroName>
Replace <DistroName>
with the name of your distribution (e.g., Ubuntu-20.04
).
Export your WSL2 distribution to a file. For example, to export Ubuntu-20.04
to D:\wsl_export\ubuntu.tar
:
wsl --export Ubuntu-20.04 D:\wsl_export\ubuntu.tar
!Step 3
Unregister the distribution from WSL2:
wsl --unregister Ubuntu-20.04
!Step 4
Create a directory on the new drive where you want to move the distribution. For example, D:\wsl_import\ubuntu
:
mkdir D:\wsl_import\ubuntu
Then import the distribution:
wsl --import Ubuntu-20.04 D:\wsl_import\ubuntu D:\wsl_export\ubuntu.tar
!Step 5
If needed, set the default user for the distribution:
ubuntu2004.exe config --default-user <YourUsername>
Replace <YourUsername>
with your actual username.
Start the distribution to ensure everything works correctly:
wsl -d Ubuntu-20.04
!Step 7
That’s it! Your WSL2 file system should now be moved to the new drive.
Here are the steps to verify that your WSL2 file system has been successfully moved to another drive:
Check WSL2 Installation:
wsl --list --verbose
to ensure your distribution is listed and running from the new location.Verify File Integrity:
.tar
files using Get-ChildItem -Path <path_to_tar> | fl @{Label="SizeGb"; Expression={$_.Length / 1Gb}}
in PowerShell.Get-FileHash <path_to_tar>
) to ensure the files are identical.Check System Recognition:
HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{UUID}\BasePath
.wsl --shutdown
and then wsl
to restart and verify the system recognizes the new location.Test Functionality:
These steps should help you confirm that the move was successful and that your WSL2 environment is functioning correctly on the new drive.
Here are some troubleshooting tips for common issues when moving the WSL2 file system to another drive:
Permission Errors:
icacls
to adjust permissions if needed.Missing Files:
wsl --export <distro> <path>
) completes successfully and the .tar
file is intact.wsl --import <distro> <path> <tar-file>
).Registry Issues:
HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\<UUID>\BasePath
to point to the new location.WSL Not Starting:
wsl --shutdown
to stop all instances and then restart your WSL distribution.Disk Space Issues:
Optimize-VHD -Path <path-to-vhd> -Mode Full
to reduce the size of the VHD file.To move your WSL2 file system to another drive, you’ll need to export it from its current location, import it into the new location, and then verify that everything is working correctly. This process can be beneficial for several reasons: it allows you to free up space on your original drive, improve performance by moving the file system to a faster drive, or even upgrade to a larger storage capacity.
First, export your WSL2 distribution using the command `wsl –export
wsl --export <distro> <path>
This will create a `.tar` file containing your entire file system. Next, import this file into the new location using `wsl –import
wsl --import <distro> <path> <tar-file>
Make sure to replace `
After importing the file system, verify that everything is working correctly by checking the WSL2 installation, verifying file integrity, checking system recognition, and testing functionality. You can do this by running `wsl –list –verbose`, comparing the size of the original and moved `.tar` files using PowerShell, ensuring the new path is correctly set in the registry, and opening your WSL2 distribution to run a few commands.
wsl --list --verbose
If you encounter any issues during this process, there are several troubleshooting tips you can follow. For permission errors, make sure you’re running as an administrator and check file permissions on the destination drive. If files are missing, verify that the export command completed successfully and double-check the paths used during import. Registry issues can be resolved by updating the registry key to point to the new location, while WSL not starting can be fixed by restarting WSL or checking logs for error messages. Finally, disk space issues can be addressed by optimizing the VHD file using PowerShell.
By following these steps carefully and troubleshooting any issues that arise, you should be able to successfully move your WSL2 file system to another drive and enjoy the benefits of improved performance, increased storage capacity, or simply freeing up space on your original drive.