Data Loss with Windows and WSL

Posted by on November 05, 2022 · 3 mins read

How upgrading Windows can lead to data loss.

So the background to this one, recently I had the grand idea of updating my Win 11 install to Windows 22H2 via the ISO installation. This caused me a whole world of pain…. For those who’ve not tried, when you upgrade via downloading the latest ISO, mounting it and running the install. In this process, Windows will remove all existing applications, although the advice through the prompt is rather cryptic.


How to recover your data in the case of of upgrading to Windows 11

The problem with the WSL and Windows Upgrade process is that WSL Linux distributions save their vhdx files within %AppData% of the runtime user. During the upgrade process, Windows will created a backup of:

C:\Windows

to

C:\Windows.old

As such when you new fresh version of Windows 10/11 boots up, then non of your existing global or userspace applications will currently be accessible through the Windows menu. If like me, you reinstall all the applications you need, turn back on the hyperv and Windows Subsystem for Linux features, reinstall the ubuntu distro you’ll notice that it’s for all intensive purposes a fresh install with all your original data absent. If you find yourself in this situation never fear, here’s what todo next:


Identify Where you’re previous WSL installations drive is:

To find this you’ll need to dig into:

C:\Windows.old\Users\<your_username>\AppData\Local\Packages\<distribution names>\LocalState\ext4.

Windows Explorer showing the backup location of the WSL disk image.

Setup a New Ubuntu Distro

https://ubuntu.com/wsl


install support for vhdx in ubuntu


sudo apt-get install libguestfs-tools
sudo mkdir /mnt/recovery



Mount the drive within the distro


sudo guestmount --add   /mnt/c/Windows.old/Users/<your_username>/AppData/Local/Packages/<distribution names>/LocalState/ext4.vhdx --inspector --ro /mnt/recovery 

computer shell showing the mounted drive.


copy files to CAUTION!!! check before you copy the whole root drive.

Personally do NOT to the below command as depending on your distrro and your previous distro this could cause mayhem. Personally I would just copy the files you need.


ONLY DO this command if you’re copying from and to a identical distro.

sudo cp -R /mnt/recovery/ / 

instead, I’d just copy your profile:

sudo cp -R /mnt/recovery/home/<profile> /home/<profile>



Remember !!!

  • Rule 1. Be patient… Depending on the amount of data this can be minutes or hours.
  • Rule 2. Do not cancel the job, this is non transactional copy, so if you cancel half way through to might risk corrupting the