默认情况下WSL安装的子系统在系统盘下,不过很多时候系统盘的空间是有限的,这时候就有了将WSL子系统安装到其他盘的需求。当然这里也会分两种情况,一种是新的安装,一种是已经安装好了。本篇就结合这两种情况,通过网上查到一些信息进行了验证。

新安装(New install)

Downloading a WSL distro

For illustration purposes, Ubuntu 22.04 will be used.

Using Web Browser

Open your favorite browser and go to https://aka.ms/wslUbuntu2204, this will download the Distro around (400MB+)

Using Windows Powershell

Open PowerShell terminal, and execute the following command

1Invoke-WebRequest -Uri https://aka.ms/wslUbuntu2204 -OutFile Ubuntu.appx -UseBasicParsing

Note: Depending on your internet speed, downloading it through Powershell might take some time compared doing it over the browser.

Installing the downloaded Distro

For illustration purposes it uses D drive (i.e. D:\)

Step 1. Open Powershell terminal, change drive to D:

1PS C:\Users\minemo> D: 
2PS D:\>

Step 2. Create a folder named “WSL” or whatever folder name to denote that it will be a placeholder for WSL distros.

Step 3. Move the downloaded file to D:\WSL, below is an illustration once the file has been moved

Step 4. Rename the downloaded file, move .\Ubuntu.appx .\Ubuntu.zip

1PS D:\WSL> move .\Ubuntu.appx .\Ubuntu.zip
2PS D:\WSL> dir

Step 5. Extract the zip to Ubuntu folder, by executing the Expand-Archive .\Ubunutu.zip

1PS D:\WSL> Expand-Archive .\Ubuntu.zip
2PS D:\WSL> dir

Step 6. Go to Ubuntu folder, then run .\Ubuntu2204.exe. This will take time to install the distro once installed it would prompt for a username and password. Once the username and password is set you will be redirected to Ubuntu 20.04 as shown below

 1PS D:\WSL> cd Ubuntu
 2PS D:\WSL\Ubuntu> .\Ubuntu2004.exe
 3Installing, this may take a few minutes...
 4Please create a default UNIX user account. The username does not need to match your Windows username.
 5For more information visit: https://aka.ms/wslusers
 6Enter new UNIX username: minemo
 7New password:
 8Retype new password:
 9passwd: password updated successfully
10Installation successful!
11To run a command as administrator (user "root"), use "sudo <command>".
12See "man sudo_root" for details.

To go back to windows, simply type exit to end the session in Ubuntu. At this stage Ubuntu distro is successfully installed.

Step 7 Set default distro to Ubuntu. It is possible that there are multiple distro installed prior to the Ubuntu. To set Ubuntu as the default distro, run wsl -s Ubuntu-22.04

 1PS D:\wsl\Ubuntu> wsl -l --all
 2Windows Subsystem for Linux Distributions:
 3docker-desktop (Default)
 4docker-desktop-data
 5Ubuntu-22.04
 6
 7PS D:\wsl\Ubuntu>wsl -s Ubuntu-22.04
 8PS D:\wsl\Ubuntu>wsl -l --all
 9Windows Subsystem for Linux Distributions:
10Ubuntu-22.04 (Default)
11
12PS D:\wsl\Ubuntu> wsl -l -v
13  NAME                   STATE           VERSION
14* Ubuntu-22.04           Running         2

Where are the files installed?

Continuing on Step 7, view the files under D:\WSL\Ubuntu, you can use dir command view or use the windows explorer check

Take a look at the file ext4.vhdx, that is a virtual hard drive that will be use by WSL for Ubuntu

To look into the files on ext4.vhdx go to File Explorer and type-in \\WSL$ in the search bar and press Enter. It would display all the mount drives on the WSL distros in your system. Below is sample screenshot.

Takeaways

已安装的进行迁移(migrate from installed)

The WSL2 remains on drive C, but it is possible to move the distribution (Ubuntu) to another drive (D) and save C space.

Move Ubuntu Installation to Drive D:

  1. Export Ubuntu:

    • Create a backup folder on drive D:

      mkdir D:\backup
      
    • Export the Ubuntu distribution to a tar file in the backup folder:

      wsl --export Ubuntu D:\backup\ubuntu.tar
      
  2. Unregister the Existing Distribution:

    • Remove the existing Ubuntu distribution from drive C:

      wsl --unregister Ubuntu
      
  3. Import Ubuntu to Drive D:

    • Create a new folder on drive D for the WSL installation:

      mkdir D:\wsl
      
    • Import the Ubuntu distribution from the backup tar file to the new folder on drive D:

      wsl --import Ubuntu D:\wsl\ D:\backup\ubuntu.tar
      
  4. Set Default User:

    • By default, Ubuntu may utilize the root user. To revert to the previous user, follow these steps:
      • Open the Ubuntu App Folder. You can do this by running the following command:

        cd %userprofile%\AppData\Local\Microsoft\WindowsApps
        
      • Set the default user by running the following command and replacing <username> with your desired username:

        ubuntu config --default-user <username>
        

Now, your Ubuntu installation is moved to drive D, and the default user is set to the specified username.

_Note: Ensure that you replace <username> with your actual username.