Skip to content

WSL2#

Mount Ext4 Disks Into WSL2#

  • Open the PowerShell as Administrator
  • Run the command below to list all the disks.
1
GET-CimInstance -query "SELECT * from Win32_DiskDrive"
  • Then we can see the result as below.
DeviceID           Caption              Partitions Size          Model
--------           -------              ---------- ----          -----
\\.\PHYSICALDRIVE0 WDC WD40EFPX-68C6CN0 0          4000784417280 WDC WD40EFPX-68C6CN0
\\.\PHYSICALDRIVE3 KINGSTON SNV2S500G   1          500105249280  KINGSTON SNV2S500G
\\.\PHYSICALDRIVE1 WDC WD40EFPX-68C6CN0 0          4000784417280 WDC WD40EFPX-68C6CN0
\\.\PHYSICALDRIVE2 KINGSTON SNV2S500G   3          500105249280  KINGSTON SNV2S500G
  • Then use the DeviceID with the command below to mount the Disk into the WSL2
1
wsl --mount \\.\PHYSICALDRIVE0
  • The open the WSL and run the command below to check the Disk is mounted successfully or not.
1
lsblk
  • Then we should see the result.
1
2
3
4
5
6
7
8
9
NAME
    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda   8:0    0 388.4M  1 disk
sdb   8:16   0   186M  1 disk
sdc   8:32   0     8G  0 disk [SWAP]
sdd   8:48   0     1T  0 disk /mnt/wslg/distro
                              /
sde   8:64   0   3.6T  0 disk
sdf   8:80   0   3.6T  0 disk /mnt/wsl/PHYSICALDRIVE0

WSL Config Files#

  • There are 2 types of WSL2 configuration files. The WSL2 global config file which is located in the Windows host system and the WSL2 local config file which is contained in the WSL2 machine.
  • The WSL2 global config file will have the name as .wslconfig and it is located at C:\Users\<your-user>. By default it is not created automatically by the Windows system. So if you want to configure something, you need to create the .wslconfig file manually following the path above.
  • The global WSL2 config file will allow you to configures global settings that affect all distributions (all WSL2 machines), such as memory limits, CPU count, swap size, etc.
  • For example:
1
2
3
4
5
[wsl2]
memory=4GB
processors=2
swap=2GB
localhostForwarding=true
  • After configuring the .wslconfig then you should stop the WSL2 machines and wait for 8 seconds to make sure all instances have been stopped completely (8 seconds rule). Then start your WSL2 machines again then you can see all distributions has been applied with your configurations.
  • The WSL2 local config file will have the name as wsl.conf and it is located at /etc/wsl.conf. By default this is created automatically inside the WSL2 machine and it will be read everytime at WSL2 startup. This WSL2 local config file is used for controlling per-distro behavior such as mount options, user login defauts or windows drives are mounted.
  • For example:
1
2
3
[network]
hostname=duc-server
generateHosts=false
  • After configuring the wsl.conf then you should stop the WSL2 machine and wait for 8 seconds to make sure the instance have been stopped completely (8 seconds rule). Then start your WSL2 machine again then you can see your specific distribution has been applied with your configurations.

Change WSL Hostname#

  • Why do we need to change the WSL Hostname?

    • By default the hostname of the WSL machine will be the same with the hostname of the windows host machine. So if you use VPN such as tailscale for your windows host machine and WSL also then you may get issues with DNS which can make you get errors when you use browsers on Windows host machine.
  • For WSL machine you can't use normal way to set the hostname such as:

sudo hostnamectl set-hostname <your-host-name>
  • Because your hostname will be reset to default after you restart your WSL machine. So you need following this way to set your hostname permanently.
  • Firstly, let's open the wsl.conf file for editing.
1
sudo gedit /etc/wsl.conf
  • Then add these lines below to the end of the file.
1
2
3
[network]
hostname=duc-server
generateHosts=false
  • Next let's open the /etc/hosts file.
1
sudo gedit /etc/hosts
  • Then replace texts which contains your old hostname to your new hostname and save. For example:
  • The old domain is look like below.
1
2
127.0.0.1       localhost
127.0.1.1       test-server.localdomain  test-server
  • Then the new domain will be like this.
1
2
127.0.0.1       localhost
127.0.1.1       new-server.localdomain  new-server
  • Finally restart your WSL machine and then you will see your WSL hostname has changed

Backup WSL2#

  • To backup WSL2 let's open the window powershell and then run the command below.

```sh lineums="1"

wsl --export

```sh linenums="1"

wsl --export Ubuntu D:\Softwares\duc-server.tar
  • Then after the execution you will see the backup file like this.

 #zoom

Restore WSL2#

  • To restore WSL2 from the .tar file let's run the command below.
1
wsl --import <DistributionName> <InstallLocation> <FileName>
1
wsl --import UbuntuRestored D:\Softwares\UbuntuRestored D:\Softwares\duc-server.tar
  • You can check if the distribution was successfully imported by listing your WSL distributions:
1
wsl -l -v
  • Then now, you can start the WSL2 restored distro by using the command below.
1
wsl -d <DistributionName>
1
wsl -d UbuntuRestored

Intellij With WSL2#

  • In this setup we will install the Intellij IDE on Windows and all the java environments and project will be installed and put in WSL2.
  • Firstly, you can download the JetBrains tool box and then install the intellij on Windows.
  • In the WSL2, you can use the ASDF tool as in the topic Linux Setup Tools to install java, maven for java projects.
  • Now, let's clone the java project from the git repository from your WSL2.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
duc@duc-server  ~/study/com.springboot3.security   master  ls
angular-integrations
asymmetric-encryption-keypair
asymmetric-encryption-keypair-generation
google-recaptcha-v3-integration
google-recaptcha-v3-integration-annotation
pom.xml
spring-configuration-property-documenter
spring-security-code-grant-oidc
spring-security-code-grant-oidc-csrf-integration
spring-security-code-grant-oidc-csrf-race-condition-integration
spring-security-code-grant-oidc-integration
spring-security-code-grant-oidc-no-samesite-integration
spring-security-code-grant-oidc-stateless-session
spring-security-code-grant-oidc-user-roles
spring-security-code-grant-oidc-user-roles-csrf
spring-security-code-grant-oidc-user-roles-csrf-xor
  • Then let's open the Intellij IDE and choose the java project in the WSL2 following steps as in the image below.

 #zoom

  • Choose Project --> Open --> Select your Distribution mount in the Linux section in your File Explorer --> Select the java project in your WSL2 --> Click Select Folder button.
  • After open the project in the intellij then let's open the File --> Project Structure and set the project SDK as in the image below.

 #zoom

  • Open the SDK dropdown and select the SDK from the WSL2 of your distribution then click OK.

 #zoom

  • Then you will see the terminal of WSL2 is open at the location of your project.

 #zoom

  • Then from this terminal you can build the java project without any performance issue.

 #zoom - Then you can also use the build tool of Intellij to build the project.

 #zoom

  • Then if you the build got the issue like [FATAL] Non-readable POM then let's grant the permission for the pom.xml by using the command.
1
chmod 755 pom.xml
  • Then if you still got the issue like the classes in the import doesn't exists then let's remove the folder JetBrains in the ~/.cache in WSL2
1
2
cd ~/.cache
rm -rf JetBrains
  • Then on Windows restart your Intellij and then click build button of the Intellij again you should build the java project successfully.

  • Now, you can run your java project from Intellij without any issue.

 #zoom

  • Next, If you want to run the debug mode there you may get the issue as below.

 #zoom

  • Then you can try to follow the Intellij Instruction to fix the issue and hope it works. However, there is more easy way to run debugging is using remote debug.
  • So firstly, let's create a debug remote as in the image below.

 #zoom

  • Then in the Intellij Terminal run the command below to start the remote server.
1
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
  • Then you should see the server is started and listening on port 5005.  #zoom

  • Now, let run the RemoteDebug on Intellij then you should see it connect to the target VM.

 #zoom

  • Now, you can try to test debugging and see the result as below.

 #zoom