Finding or Recovering your WSL Data
After a recent complete failure of my Windows 11 insider build, I was forced to do a Windows reset. The process promises to preserve all of your files, but should probably be asterisked to note that data stored in WSL won't stick around (although technically is not deleted).
Where's my WSL Data?
Data stored in WSL v2 distros are kept in a VHDX file and is located in %LocalAppData%\Packages\
under a subfolder specific to the distribution. For my Ubuntu install, the folder was CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
. Once in that folder, open LocalState
and you'll find your VHDX file. Don't bother trying to mount this file with Disk Manager or using diskpart
, it's ext4 which Windows won't natively read (outside of WSL).
Windows knows where all of your WSL data is from: HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\<GUID>
.
What happens after a System Reset?
When your system is reset, your files are preserved while applications are removed. Unfortunately, Microsoft seems to consider the Packages
folder something only for applications, so this data is removed. Or, more appriately, and thankfully, just moved. You can find your old user profile under c:\windows.old\users
. Traversing your profile to the same path listed above should lead you to your VHDX file.
Make a copy of this VHDX file in case it gets corrupted while you're repairing!
Getting your data back!
I had a few false starts that, if they worked, would have been an easy way to get back up and running quickly.
- Install the same distribution again, shut it down (
wsl --shutdown
), and then replace theext4.vhdx
with your backup. - Manually create the registry key associated with your distribution (outlined here).
Had these worked, I would have been up and running with all of the same packages installed and data available. Neither of these worked for me, however, I got errors such as these as it seemed my ext4.vhdx had likely been corrupted:
- "No response from virtual machine or container, operation timed out."
- "The operation timed out because a response was not received from the virtual machine or container."
- "Failed to mount (error code: -22)"
I also tried using guestmount
in WSL (but it didn't see it as a valid Operating System).
What ultimately worked for me so that I could at least recover my data was this:
- Install the Hyper-V feature (can co-exist with WSL and Docker).
- Download the latest Ubuntu CD.
- Create a new VM in Hyper-V. Use Generation 1. Attach the ISO as your CD, and your VHDX as the only disk.
- Boot the VM with the attached ISO, and select to "Try Ubunutu" (it'll run the OS from the CD instead of installing).
- Once at your desktop, open a terminal and run
sudo fdisk -l
. - Search the results for the name of your disk. It will likely be
/dev/sda
, but you should be able to tell by the size. - Run
mkdir ~/wsl/
and thensudo mount /dev/sda ~/mnt/
. If this works, stop here, your data is now in~/mnt/
.
If this doesn't work immediately (it didn't for me) because your disk is corrupt, you can try the steps from this post which I include below. Replace sda
with the appropriate name of the disk if necessary:
- In the terminal, run
sudo fsck.ext4 -v /dev/sda
. - Run
sudo mke2fs -n /dev/sda
. SelectY
to repair, you may need to do this repeatedly. - Try to mount the disk again (see Step 7 above). If this works, stop here.
- If the disk will still not mount, run
sudo e2fsck -b block_number /dev/sda
where block_number is the first result from Step 2). - Repeat Step 4 with the next block number until it works succesfully and you can mount the disk.
While this doesn't get you back your original install, you at least have access to your data which you can now copy to a new home.
Looking for how to backup and restore your WSL data? This post has a good step-by-step.
No comments