How can I preserve the WSL linux environment while doing a Windows 10 system "reset?"
This post mentions simply restoring the WSL directory partially restored WSL, but with some functionality issues. This post gives generic information for how to back up and recover a linux installation. This post says that %appdata% is in fact moved into your Windows.old folder, which is created after reset and contains your "old" files.
)
What information might have gotten lost in the first posters experiment? Are there some registry settings or configuration files that would allow simply restoring the Linux root directory (%USERPROFILE%/AppData/Local/lxss
) to work?
EDIT
My question was marked as a duplicate of How to backup (WSL) Linux Subsystem for Windows 10, before system reset or reinstall?.
However that question was only about how to back up the installation--basically tar it and save it somewhere. I explicitly ask about how to restore it.
Very old question that got bumped today, and things have changed a lot in the 4 years since the question was asked. I actually did a "Reset" on my daily-driver a few years ago and had a similar situation.
Back then, the best you could really do would be to copy any data out of the old filesystem. When this question was asked, that filesystem was typically (as you mention in the question) somewhere under %USERPROFILE%\AppData\Local\lxss
. I'll take your word for it, since I don't recall exactly, and I don't have any installations that old around any longer.
At some point, probably when WSL started supporting multiple distributions, the WSL1 folder changed to %USERPROFILE%\AppData\Local\Packages\<PackageName>\LocalState\rootfs
. When I did a reset on my PC several years ago, I did tar
up that directory and then restored individual files (but not the entire filesystem, as you ask) after the Reset and WSL re-installation. I didn't consider the Windows.old
, but that would have been a valid way to get at those files as well.
Nowadays, yes, before doing a reset, a wsl --export
can be performed as mentioned in the other answer here.
But if the user doesn't think to do this, it may still be possible to completely restore the filesystem after the Reset using Windows.old
. There are two scenarios:
-
WSL2 makes this fairly easy, as the entire filesystem is stored in a virtual drive in
%USERPROFILE%\AppData\Local\Packages\<PackageName>\LocalState\rootfs\ext4.vhdx
. See this answer for details, but you can basically just copy thatext4.vhdx
out ofWindows.old
and into a new installation. -
With WSL1, I'm 90% confident that you can recover a WSL installation from
Windows.old
using the following method.-
First, install a distribution from the Store. I know, you want to recover your old install, but we're going to use this new install to do that. You can always uninstall the Store distribution when we are done.
-
Launch that distribution.
-
Create
/etc/wsl.conf
with the following:[automount] options = "metadata"
-
Next,
cd /mnt/C/Windows.old/Users/<yourusername>/AppData/Local/Packages/<PackageName>/LocalState/rootfs
<PackageName>
will depend on your distribution, but will be start withCanonical...
for Ubuntu distributions. If your WSL install is old enough, it may be/mnt/C/Windows.old/Users/<yourusername>/AppData/Local/lxss
. When you dols
there, you want to see the/
directory of that installation. -
From within that directory,
tar cvf recovery.tar .
. It will take a while for a WSL1 installation. -
sudo mv recovery.tar <somewhere>
else on theC:
drive; something like/mnt/c/Users/<youruser>/Documents/wsl
(it's close to what I use for this). This will be the new location of the distribution when we're done. -
Exit WSL
-
From PowerShell, navigate to the directory where you placed
recovery.tar
. -
mkdir OldUbuntu
(or whatever you want to call it) -
wsl --import OldUbuntu .\OldUbuntu .\recovery.tar
. The command also optionally takes a--version 1/2
(at the end) to force the WSL version. -
If desired, make this your default WSL installation with
wsl --set-default OldUbuntu
. -
Launch the distribution with
wsl ~ -d OldUbuntu
. The-d/--distribution
argument is only required if you didn't set it as default above. -
Create a
/etc/wsl.conf
with the following contents:[user] default=username
Edit
username
to be whatever your username was in the distribution before you Reset Windows. -
Optionally uninstall the distribution that we installed and used for creating the tarball.
That should be it. I've actually tested this with a slightly different scenario. I installed a WSL1 instance, then I went through the process above on it from another instance. I was able to successfully
--import
it after going through the above steps. File/directory ownership and permissions look to be correct, as do symlinks. My only concern is whether a really old WSL distribution may use different metadata.Warning: Please only try this on a WSL1 filesystem if you have no other way to recover it. Accessing files in a WSL1 filesystem through
%AppData%
(or equivalent) is known to cause corruption. That's why I created a throwaway instance myself in order to try this out.Side note: You might think about
tar
'ing the files inWindows.old
from within Windows, rather than going through the hassle of installing another WSL instance. My concern here is that this will not capture the Linux ownership and permissions, which will likely result in a corrupted installation whenwsl --import
'ing. -