Finding or Recovering your WSL Data

Share on:

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 the ext4.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:

  1. Install the Hyper-V feature (can co-exist with WSL and Docker).
  2. Download the latest Ubuntu CD.
  3. Create a new VM in Hyper-V. Use Generation 1. Attach the ISO as your CD, and your VHDX as the only disk.
  4. Boot the VM with the attached ISO, and select to "Try Ubunutu" (it'll run the OS from the CD instead of installing).
  5. Once at your desktop, open a terminal and run sudo fdisk -l.
  6. 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.
  7. Run mkdir ~/wsl/ and then sudo 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:

  1. In the terminal, run sudo fsck.ext4 -v /dev/sda.
  2. Run sudo mke2fs -n /dev/sda. Select Y to repair, you may need to do this repeatedly.
  3. Try to mount the disk again (see Step 7 above). If this works, stop here.
  4. 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).
  5. 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