background

Migrating workflows to Windows is an option, given the lack of common software on Linux and the odd bugs that sometimes occur with graphical interfaces (including, but not limited to, text editors being the default way to open compressed files, interface freezes, etc.).

The biggest disadvantage of WSL 2 compared to WSL 1 is that it runs in the virtualization layer and has poor read and write performance on Windows disks! After all, you switched to using a network implementation to mount Windows partitions to WSL 2 (there is also the problem that hardware and network ports are no longer shared). But the advantage is that it is a full Linux system, and can do all the functions of Linux (systemctl is still not available).

If you want to use WSL 2 for routine work, you must avoid NTFS and use other file systems that are natively supported on Linux. This article focuses on how to use the native supported file system for WSL 2 and how to connect USB devices to WSL 2.

WSL2 installs and configures, mounts, and uses a hard disk

If you have only one hard disk and it is large enough, you do not need to do this operation, and can refer to the following documentation to remove the 256GiB space limit. This chapter can be skipped:

Expand the size of your WSL 2 Virtual Hard Disk

If you switch from Linux to Windows, the Linux disk remains (and is a file system supported by ****WSL 2 Kernel), The following operations can be done optionally (such as preparing and mounting Ext4 partitions, creating new partitions, creating encrypted partitions, and formatting virtual devices mapped to encrypted partitions can all be skipped).

Configure the environment

  1. To enable Linux subsystem functionality (administrator permissions required), the following command automatically configures all environments, using WSL 2, Ubuntu by default.
wsl --install
Copy the code

  1. Restart the system, and the following installation box will pop up automatically

  1. Follow the prompts to set up a user name and password

Mount and prepare Ext4 partitions

Do not use F2FS, because the WSL2 kernel does not support F2FS, you can find the default source of Microsoft f2FS-Tools, you can switch source download, but it is not useful. Microsoft’s default source eliminates the need to waste time doing a lot of things you wouldn’t normally do.

Use /proc/filesystems to view the filesystems supported by the current kernel

  1. View the current Windows disk with the following command
GET-CimInstance -query "SELECT * from Win32_DiskDrive"
Copy the code

5. Using DiskPart, clear a disk that needs to be used for WSL 2

Administrator permission Run the following commands

diskpart.exe
Copy the code

Then run the following command in DiskPart to view the existing disk information

list disk
Copy the code

Select the disk that you want to mount to WSL2 (determined by deviceID in Step 4), clear all information, and execute the following command

# Select the disk to operate on
# select disk 0
select disk [Disk id(Can be based ondeviceIDNumber judgement)]Delete all information including partition table
clean
Create GPT (GUID)
convert gpt
# exit
exit
Copy the code

This is the end of the disk!

  1. Mount the device (” mount “a physical disk to WSL2) by following the instructions
# wsl --mount \\.\PHYSICALDRIVE0 --bare
wsl --mount [DeviceID] --bare
The unmount command is WSL --unmount \\.\PHYSICALDRIVE0
Copy the code

At this point you can see that the device has been mapped to WSL 2

Create a new partition (WSL2 operation)

As you can see, there is already a sDE1 partition by default, but do not use this partition, this is the Default MSR reserved partition for Windows! Removing this partition may cause some bugs. For details, see: Windows 10 quick boot shutdown back to login screen! The cause has been found, seek a solution! – Microsoft Community

Add a new partition using fdisk
# sudo fdisk /dev/sde
sudo fdisk [disk path]
# fdisk Command introduction
# n -- create a new partition.
# w -- Synchronize all previous actions to disk and exit
Copy the code

Sde2 above is our new partition.

Creating an encrypted partition

Create an encrypted partition
# sudo cryptsetup luksFormat /dev/sde2
sudo cryptsetup luksFormat [part path]
# decrypt the encrypted partition and map a virtual device, which will be placed in /dev/mapper/
# sudo cryptsetup luksOpen /dev/sde2 FastStoreSudo cryptsetup luksOpen [part Path]Sudo cryptsetup luksClose can be used to close the mapping
# sudo cryptsetup luksClose FastStore
Copy the code

Now it looks something like the image above.

The virtual device mapped from the formatted encrypted partition is Ext4 and mounted to it

  1. Format the disk as Ext4 and mount it
# mkfs to ext4
# sudo mkfs.ext4 /dev/mapper/FastStoreSudo mkfs.ext4 /dev/mapper/# mount FastStore (don't waste your time trying to use TRIM because it's a solid mount, because SL2 doesn't support this kind of mapping.)
# mount -o discard is completely useless, I have tried everything
TRIM is triggered manually with the fSTRIm command following the corresponding mount point root directory

# mkdir ~/AndroidCodes
# sudo mount /dev/mapper/FastStore ~/AndroidCodesMkdir [mount point path] sudo mount /dev/mapper/[map name]# Change owner
# sudo chown ovea ~/AndroidCodesSudo chown [current user name] [mount point path]Copy the code

Operations required for the next startup

At this point, the mount part is complete! Next time, just do the following operations:

# In Windows [admin privileges]
# wsl --mount \\.\PHYSICALDRIVE0 --bare
wsl --mount [DeviceID] --bare

# In WSL 2
# sudo cryptsetup luksOpen /dev/sdd1 FastStore
# sudo mount /dev/mapper/FastStore ~/AndroidCodesSudo cryptsetup luksOpen [part path] [mapping name] sudo mount /dev/mapper/[mapping name] [mount point path]Copy the code

Use some recovery software to view hard disk data (you will find that you can’t see any data at all! It’s important to encrypt everything, so that if the computer is stolen, or recycled, and the hard drive is replaced, as long as the password is complex enough, no one has any way to recover the data inside.)

Windows 11 can even see the WSL 2 directory structure directly in the file manager! Operation is very convenient!

The performance test

  1. Get a copy of the Redis code compiled and check the time (especially NTFS and Ext4 times, you can see how low THE IO of WSL 2 is on NTFS, so it is necessary to mount an Ext4 hard drive for Android compilation)

Redis build time on NTFS partition (Windows Security disabled) : 4 min 47 SEC

Redis build time on Ext4 partition: 1 minute 37 seconds

Native Ubuntu 21.10:1 min 34 SEC

Android code pull and build test

  1. First pull the code
  2. Next, create a swap (use a notebook, memory space is a bit tight)
sudo dd if=/dev/zero of=swapfile bs=1G count=40
sudo chmod 0600 swapfile
sudo mkswap swapfile
sudo swapon swapfile
Copy the code

  1. Build the Android code, and here are the results

As long as the Swap is good enough, it will not trigger OOM even if it freezes (it will if you compile with Java, Swap is as good as air), and SSD will not freeze at all

3.8GIB of memory (laptop 8GiB, WSL only score 3.8GIB, although can be increased, but will make Windows very difficult) will be OOM when Compiling Java Framework, so compile the source code that does not contain Java code to test. You can see that boot.img compiled successfully.

WSL2 connects to a USB device

Native support is not yet available, refer to the following documentation, or use WSL1 (WSL2 and WSL1 can coexist) :

Connect USB devices

Follow the documentation and try it out

The WSL2 kernel version must be >=5.10.60.1 to use this function

Unable to update WSL2 kernel with APT!

WSL2 kernel Update method: Microsoft Update Catalog

After the installation is complete, restart WSL2 to take effect by running WSL –shutdown and then WSL. Or restart Windows 11.

Environment configuration — Windows

On Windows, run

winget install --interactive --exact dorssel.usbipd-win
Copy the code

When the download is complete, the installation dialog box will pop up automatically. Click Install.

Environment configuration — WSL2

# This is independent of the kernel versionSudo apt install Linux-tools-5.4.0-77 - Generic hwData sudo VisudoCopy the code

Add /usr/lib/linux-tools/5.4.0-77-generic: to the secure_path header.

use

Display all USB devices in Windows

usbipd wsl list
Copy the code

Add a USB device to WSL2

# usbipd wsl attach --busid 1-4
usbipd wsl attach --busid <busid>
Copy the code

This USB device can now be seen in WSL2 and can be manipulated!

lsusb
Copy the code

Windows enable SSHD

If the daily work desktop and notebook are set to Windows11, in addition to the use of Windows remote desktop (unstable), you can also enable SSHD, use SSH connection (more stable than remote desktop). The following describes how to enable administrator privileges when using SSH to connect to remote Windows, mount the hard disk to WSL2, and use WSL command to enter WSL2 mode

1. Enable the openSSH Server function module

2. Configure and start the openSSH Server

# Start the sshd service
# start SSHD
Start-Service sshd

# OPTIONAL but recommended:
Set SSHD to automatically start
Set-Service -Name sshd -StartupType 'Automatic'
Copy the code

3. Remotely connect the SSHD

Attention! If powershell is an administrator, then you have administrator rights by default. Otherwise, you do not have administrator rights.

# ssh [email protected]SSH [user name]@[ip]
Copy the code

4. Use WSL2 remotely

# In Windows [admin privileges]
# wsl --mount \\.\PHYSICALDRIVE0 --bare
wsl --mount [DeviceID] --bare

# In WSL 2
# sudo cryptsetup luksOpen /dev/sdc2 FastStore
# sudo mount /dev/mapper/FastStore ~/AndroidCodesSudo cryptsetup luksOpen [part path] [mapping name] sudo mount /dev/mapper/[mapping name] [mount point path]Copy the code

Additional information

I have been working on Windows10+WSL1+VMware Workstation for 8 months, but switching to Windows11+WSL2 is more efficient and performs much better than VMware Workstation.

(Although it’s back to Linux for the next 2 months, there are no middlemen and most of the restrictions are gone. Try F2FS or other new file systems and other new features. The point is that F2FS is available for Linux, and it’s also great for taking notes! But you can’t drive all the hardware properly… For example, the fingerprint module is driven but cannot be used normally, TPM chip is hardly used, and GPU must be installed with the closed source driver provided by Nvidia to play close to 100% performance.)

The resources

Install WSL | Microsoft Docs

Which software can convert hard disk partitions to ext4 format under Windows? – zhihu (zhihu.com)

Get started mounting a Linux disk in WSL 2 | Microsoft Docs

Redis warehouse

Connecting USB devices to WSL – Windows Command Line (microsoft.com)

Linux LUKS Encrypted Hard Disk Partition – CyberSecurityBook – Blogpark

Win: use diskPart to rebuild usb disk partition – Jun’s – blog garden

Get started with OpenSSH

Use sudo with SSH-Session from PowerShell