Skip to content

Instantly share code, notes, and snippets.

@criscunhasantos
Forked from PatrickLang/WSL2-recovery.md
Created September 30, 2024 00:28
Show Gist options
  • Save criscunhasantos/dc6f427eba01d280d52c79d5db8e867f to your computer and use it in GitHub Desktop.
Save criscunhasantos/dc6f427eba01d280d52c79d5db8e867f to your computer and use it in GitHub Desktop.
How to restore a WSL2 distribution and your data after a Windows Reset

I had some bad driver issues that led to me need to 'reset' Windows from the local recovery partition. This recovered my machine and got it bootable again. Afterwards, WSL2 wasn't installed, so I tried to install it. It rebooted, etc, then fetched Ubuntu 22.04 from the Microsoft Store as usual. However, instead of keeping my previous WSL2 content it started a new user:

Ubuntu is already installed.
Launching Ubuntu...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username:

Sure enough, the VHDX was new, only 1GB:

    Directory: C:\Users\patrick.lang\AppData\local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        11/20/2023   9:03 PM     1200619520 ext4.vhdx

I know that user profiles are temporarily stored in c:\Windows.old\Users so I went for a delve there. I found a LocalState folder of the same name, with a VHDX much bigger than that at C:\Windows.old\Users\patrick.lang\AppData\Local\packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState

I finished up setting up the "new" Ubuntu instance, then ran these steps

cd C:\Users\patrick.lang\AppData\Local\packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState
wsl.exe --shutdown
rm .\ext4.vhdx
mv 'C:\Windows.old\Users\patrick.lang\AppData\Local\packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx' .

After that, running "Ubuntu" via the Windows Terminal menu started up the instance with my old data intact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment