Skip to content

Latest commit

 

History

History
128 lines (85 loc) · 3.33 KB

RECOVER.md

File metadata and controls

128 lines (85 loc) · 3.33 KB

Recover files from old WSL2 installs

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.

Enable Hyper-V

  1. Open Powershell with Administrator privileges

  2. Enable Hyper-V

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All
  3. Reboot

Mount the drive

  1. Open Powershell with Administrator privileges

  2. 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
    
    
  3. 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.

  4. Show the filename

    echo $disk
  5. Mount the drive and show the path

    Write-Output "\\.\PhysicalDrive$((Mount-VHD -Path $disk -PassThru | Get-Disk).Number)"

    Example output:

    \\.\PhysicalDrive3
  6. 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
    
  7. 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'.
    
  8. 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
  9. Change to your old home directory

    cd /mnt/wsl/PHYSICALDRIVE?/$HOME
  10. Copy out files as required

  11. Move out of the file mount

    cd ~
  12. Return to Powershell

  13. Unmount

    wsl --unmount \\.\PHYSICALDRIVE3