C: drive is running out of space or do you like to have your files in a different disk than your Operating System, we can move our WSL2 file system to another drive in a few steps.
If we are using a username different than root, we need to know it running the
whoami command (WSL side):
Also we need to know the distro name and WSL version, running the
wsl --list --verbose command on PowerShell (Windows side):
Knowing the username:
equiman, the linux distribution:
Ubuntu, and confirm that we are using the
WSL 2 version, we are ready to...
All next steep need to be done on PowerShell (Windows side).
First, we need to close all terminals that are using WSL, and then shut it down to avoid data corruption running the
wsl --shutdown command.
Next we are going to create a backup from our
Ubuntu linux distribution.
mkdir D:\backup wsl --export Ubuntu D:\backup\ubuntu.tar
Don't worry if you do not see progress indicators. This can take a while depending on how much data you have. Maybe it's a good idea to take a rest.
When finish, we need to to unregister the linux distribution from
C: drive. It will also release the disk space taken by WSL.
⚠️ Beware that everything that was stored on the WSL drive will be lost, and the command won't ask for a confirmation, so make sure you backup everything you need first!
wsl --unregister Ubuntu
And then, import again on our new disk. I'll use
D: disk as an example.
mkdir D:\WSL wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
By default Ubuntu will use
root as default user, to switch to our user we need to run these commands.
cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps ubuntu config --default-user equiman
Restart WSL running
wsl command and it's done.
That’s All Folks!
Happy Coding 🖖