Moving WSL to Another Drive in Windows Ranjan.info

Windows Subsystem for Linux ,WSL2) is widely used by developers, administrators and common users to run various Linux distros (Ubuntu, Debian, openSUSE, Kali Linux, Alpine), tools and apps in Windows without using virtualization and dual boot. In this article, we will show how to move files from an existing WSL installation to a different drive or computer.

When you install WSL in Windows, all environment files are saved in the system drive C:\. If your system drive is not large enough (for example, an SSD), the size of the WSL file system can increase significantly and you may need to move the WSL to another drive or computer. You can transfer your WSL installation in several ways.

First, get the current size of the WSL file system on your current drive. List the WSL distributions installed on your computer:

wsl --list --verbose

In this example, only Ubuntu-20.04 is installed. Run the following PowerShell command to get the name of the UWP app with your Linux distro:

Get-AppxPackage -Name "*Ubuntu20*" | Select PackageFamilyName

In my example, the UWP package name is at CanonicalGroupLimited.Ubuntu20.04Windows_79rhkp1fndgsc, The Linux image’s WSL file system is located in ext4.vhdx vhd file %USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx,

To get the file size using PowerShell, run the command below:

$path= $env:USERPROFILE+ "\AppData\Local\Packages\" + (Get-AppxPackage -Name "*Ubuntu20*").PackageFamilyName + "\LocalState\ext4.vhdx"
Get-ChildItem -Path $path | fl @{Label="SizeGb"; Expression={$_.Length / 1Gb}}

get wsl file system image - ext4.vhdx

The path of the VHD file of the WSL distribution is stored in basepath Registry parameter under Registry key HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{UUID},

You can stop WSL, move ext4.vhdx to another drive, and change the path of the directory in the registry. but this is the scenario not recommended,

In current Windows 10 and 11 builds, you can correctly migrate your WSL environment using the built-in export and import process.

Run the WSL environment and check the username:

wsl
whoami

In our example, it is sysops,

wsl:get username whoami

Quit all apps running in your Linux environment and the WSL console:

wsl --shutdown

To back up (export) your WSL environment and save it to a drive E, run the command:

mkdir e:\backup
wsl --export Ubuntu-20.04 e:\backup\ubuntu.tar

Wait for the WSL export to finish (this may take longer). A TAR archive with your WSL ext4 file system will appear in the target directory.

move wsl file system to another drive

You can then extract the WSL files to the source disk:

wsl --unregister Ubuntu-20.04

Create a directory for your Linux image on a new drive and import the TAR archive into WSL using this command:
mkdir E:\WSL
wsl --import Ubuntu-20.04 E:\WSL\ E:\backup\ubuntu.tar

The default login in Ubuntu is root. To change it to a different username (we got this earlier), run:

cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
.\ubuntu2004.exe config --default-user sysops

Start the WSL environment:

wsl -d Ubuntu-20.04

start wsl

tip. You can start your WSL distro from Windows Terminal tab. run wsl from windows terminal

You can use the same scenario to move the configured WSL image to other computers.

You can use a third-party utility LX RunOffline (Available on GitHub – https://github.com/DDoSolitary/LxRunOffline) to transfer WSL to Windows. You can install it using Chocolatey:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
choco install lxrunoffline

List available WSL images:

lxrunoffline list

Terminate all WSL processes:

wsl --shutdown

To get the current directory, the WSL image is located in:

lxrunoffline get-dir -n Ubuntu-20.04

wsl.  Using lxrunoffline to move

To move a WSL image to another disk:

lxrunoffline move -n Ubuntu-20.04 -d d:\wsl2\Ubuntu-20.04

After the migration is complete, start WSL using this command:

wsl -d Ubuntu-20.04

either

lxrunoffline run -n Ubuntu-20.04 -w

Leave a Comment