OK, so you've refreshed Windows and then remembered that you've lost a specific file or directory from your old WSL2 environment. Here's how to find the file and mount it in WSL.
This needs Hyper-V enabled.
-
Open Powershell with Administrator privileges
-
Enable Hyper-V
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All
-
Reboot
-
Open Powershell with Administrator privileges
-
Find the old ext4.vhdx file
Get-ChildItem -Path C:\Windows.old\Users\$([Environment]::UserName)\AppData\Local\Packages -Include ext4.vhdx -File -Recurse -ErrorAction SilentlyContinue
Example output:
Directory: C:\Windows.old\Users\richeney\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 25/01/2022 09:24 34395389952 ext4.vhdx
-
Set a filename variable
$disk = (Get-ChildItem -Path C:\Windows.old\Users\$([Environment]::UserName)\AppData\Local\Packages -Include ext4.vhdx -File -Recurse -ErrorAction SilentlyContinue).FullName
This command works on the basis that you have found only one file. If you have discover than one then set manually or change the path value to ensure it is unique.
-
Show the filename
echo $disk
-
Mount the drive and show the path
Write-Output "\\.\PhysicalDrive$((Mount-VHD -Path $disk -PassThru | Get-Disk).Number)"
Example output:
\\.\PhysicalDrive3
-
Show the drives
GET-CimInstance -query "SELECT * from Win32_DiskDrive"
Example output
DeviceID Caption Partitions Size Model -------- ------- ---------- ---- ----- \\.\PHYSICALDRIVE0 SAMSUNG MZVLB1T0HBLR-000MV 4 1024203640320 SAMSUNG MZVLB1T0HBLR-000MV \\.\PHYSICALDRIVE3 Microsoft Virtual Disk 0 274872407040 Microsoft Virtual Disk
-
Mount in WSL
wsl --mount \\.\PHYSICALDRIVE3
Change to your drive path if different.
Example output:
The disk \\.\PHYSICALDRIVE3 was successfully mounted under the name 'PHYSICALDRIVE3'. The mountpoint can be found under the path pointed to by the automount setting (default: /mnt/wsl). To unmount and detach the disk, run 'wsl --unmount \\.\PHYSICALDRIVE3'.
-
In your WSL distro
df -k --type=ext4 `` Example output: ```text Filesystem 1K-blocks Used Available Use% Mounted on /dev/sdc 263174212 5007124 244728932 3% / /dev/sdd 263174212 27652224 222083832 12% /mnt/wsl/PHYSICALDRIVE3
-
Change to your old home directory
cd /mnt/wsl/PHYSICALDRIVE?/$HOME
-
Copy out files as required
-
Move out of the file mount
cd ~
-
Return to Powershell
-
Unmount
wsl --unmount \\.\PHYSICALDRIVE3