Move WSL File System to another Drive

Camilo Martinez - Nov 27 '21 - - Dev Community

Language: [🇪🇸] Español - [🇺🇸] English


If the C: drive is running out of space or do you like to have your files on a different disk than your Operating System, we can move our WSL2 file system to another drive in a few steps.

Prerequisites

If we are using a username different than root, we need to know it running the command (WSL side):

# Linux Terminal
whoami
Enter fullscreen mode Exit fullscreen mode

who am I

Also, we need to know the distro name and WSL version, running the command on PowerShell (Windows side):

#PowerShell
wsl --list --verbose
Enter fullscreen mode Exit fullscreen mode

wsl list

Knowing the username: equiman, the linux distribution: Ubuntu, and confirming that we are using the WSL 2 version, we are ready to...

moving day

Move

All next steps 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 command.

# PowerShell
wsl --shutdown
Enter fullscreen mode Exit fullscreen mode

Next, we are going to create a backup from our Ubuntu linux distribution.

# PowerShell
mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

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.

waiting

When finished, first of all, and more important ensure that the D:\backup\ubuntu.tarfile exists. Then we need to unregister the linux distribution from the 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!

# PowerShell
wsl --unregister Ubuntu
Enter fullscreen mode Exit fullscreen mode

And then, import it again on our new disk. I'll use the D: disk as an example.

# PowerShell
mkdir D:\WSL
wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Use the " (double-quote) character to surround the paths if they have blank spaces

By default Ubuntu will use root as the default user, to switch to our user we need to run these commands.

# PowerShell
cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
ubuntu config --default-user equiman
Enter fullscreen mode Exit fullscreen mode

Ensure replacing equiman with yours.

Restart WSL by running the command:

# PowerShell
wsl --distribution Ubuntu
Enter fullscreen mode Exit fullscreen mode

Recover

If something goes wrong in the process, don't worry, we can restore our WSL disk from the backup.

Didn't You skip the backup step? right?

right

Just repeat the steps after backup.


That’s All Folks!
Happy Coding 🖖

beer

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player