Skip to content

Commit

Permalink
Update WSL2.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sosiristseng committed May 30, 2024
1 parent 4f12d85 commit 691ea56
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions docs/code/windows/WSL2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@ Open powershell with administrator privilege, [run the following command](https:
wsl --install
```

or install the components [manually](https://learn.microsoft.com/en-us/windows/wsl/install-manual)

```powershell
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
```

After reboot,

```powershell
wsl --update
wsl --set-default-version 2
wsl --install -d Ubuntu # Or another distro
```

## WSL2 post-install (optional) setup

### Move the virtual disk
### Backup/move the virtual disk

If you want to move the WSL virtual disk file to another disk (in this example, `D:\`), run the following commands in Windows[^export-import][^movedrive]:

!!! tip
The following example will move the current WSL virtual disk to `D:\Ubuntu\ext4.vhdx`. You can change the distribution name (Ubuntu) and filesystem paths if necessary.

```powershell
cd D:\
mkdir WSL
cd WSL
mkdir Ubuntu
wsl --export Ubuntu .\Ubuntu\ext4.tar
wsl --unregister Ubuntu
wsl --import Ubuntu .\Ubuntu\ .\Ubuntu\ext4.tar
wsl --import Ubuntu D:\Ubuntu\ .\Ubuntu\ext4.tar
```

[^export-import]: https://learn.microsoft.com/zh-tw/windows/wsl/basic-commands#import-and-export-a-distribution
Expand Down Expand Up @@ -88,13 +95,15 @@ wsl --update

### Reclaim virtual disk space

#### Optimize-VHD
To reclaim disk space from virtual hard disks (VHDs), run the following commands with administrator privileges [^optimize-vhd]:

```powershell
wsl --shutdown
Optimize-VHD -Path %path-to.vhdx% -Mode Full
```

#### diskpart
Alternatively, use `diskpart` (if `Optimize-VHD` is not found) [^vhd-diskpart]

```powershell
Expand All @@ -108,6 +117,8 @@ detach vdisk
exit
```

#### Export and re-import

Alternatively, export the VHD as a tar file and reimport it again.

[^optimize-vhd]: https://blog.miniasp.com/post/2023/05/14/Shrink-your-WSL2-Virtual-Disks-and-Docker-Images-and-Reclaim-Disk-Space
Expand Down

0 comments on commit 691ea56

Please sign in to comment.