记录 Windows 10 WSL/WSL2 安装和使用中的问题及总结.

开启/关闭功能

WSL

WSL2

upgrade from wsl

# check if version 1
wsl -l -v
# enable hyper-v by administrator powershell command
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
# upgrade
wsl --set-version Debian 2
# if error occured, need to update kernel by download sw from https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

mount samba with wsl2

经尝试:Windows主机中无法查看WSL2中成功挂载的SMB共享文件

mount nfs with wsl2

经尝试:Windows主机中无法查看WSL2中成功挂载的NFS共享文件

explose port
use bridged network [PASSED;重启后失效]
# check adapter
Get-NetAdapter
# switch to bridge by administrator powershell command
Set-VMSwitch WSL -NetAdapterName Wi-Fi
# delete old ip by wsl console
sudo ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0
# set static ip
sudo ip addr add 10.0.0.48/24 broadcast 10.0.0.255 dev eth0
sudo ip route add 0.0.0.0/0 via 10.0.0.1 dev eth0
# and set nameserver
sudo cp /etc/resolv.conf /etc/resolv.conf.bak
sudo nano /etc/resolv.conf
# replace with 10.0.0.1 and then save
# or set DHCP directly
use port mapping [FAILED]
fix command

connect iSCSI by wsl2

introduction
  1. compile new kernel with iscsi support | reference
  2. install systemd-genie for systemd support | reference
  3. install iscsi client open-iscsi | reference
  4. discovery and connect
step by step
# 1. compile kernel
sudo apt update
sudo apt install -y build-essential flex bison libssl-dev libelf-dev libncurses5-dev git bc dwarves
cd ~  # move to home to avoid compile failure
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
cd WSL2-Linux-Kernel
export KCONFIG_CONFIG=Microsoft/config-wsl
make menuconfig  # select reference: https://github.com/jovton/USB-Storage-on-WSL2/blob/master/README.md
sudo make KCONFIG_CONFIG=Microsoft/config-wsl
sudo make modules_install
cp ./arch/x86_64/boot/bzImage /mnt/c/Users/your-user-name/  # Replace "your-user-name" with your actual Windows user account name.
touch /mnt/c/Users/your-user-name/.wslconfig
vim .wslconfig  # and add content below
[wsl2]
kernel=C:\\Users\\your-user-name\\bzImage
swap=0
localhostForwarding=true
## exit wsl console, and reboot wsl2 instance by powershell
wsl --shutdown
## start a new wsl console, then run
sudo modprobe -v libiscsi
sudo modprobe -v scsi_transport_iscsi
sudo modprobe -v iscsi_tcp
sudo modprobe -v libiscsi_tcp

# 2. install systemd-genie
sudo -s
apt install lsb-release
wget -O /etc/apt/trusted.gpg.d/wsl-transdebian.gpg https://arkane-systems.github.io/wsl-transdebian/apt/wsl-transdebian.gpg
chmod a+r /etc/apt/trusted.gpg.d/wsl-transdebian.gpg

cat << EOF > /etc/apt/sources.list.d/wsl-transdebian.list
deb https://arkane-systems.github.io/wsl-transdebian/apt/ $(lsb_release -cs) main
deb-src https://arkane-systems.github.io/wsl-transdebian/apt/ $(lsb_release -cs) main
EOF

apt update
apt install -y systemd-genie

# 3. install open-iscsi
apt install -y open-iscsi
# 4. discovery and connect
sudo genie -c /etc/init.d/open-iscsi start  # start open-iscsi via genie
sudo iscsiadm -m discovery -t sendtargets -p aio.nas.yirami.xyz
sudo iscsiadm -m node --login  # login all
sudo iscsiadm -m node -T iqn.2005-10.org.freenas.ctl:iscsi-documents -p aio.nas.yirami.xyz:3260 --login  # login specified target
#sudo iscsiadm -m node -u  # logout all
#sudo iscsiadm -m node -u -T iqn.2005-10.org.freenas.ctl:iscsi-documents -p aio.nas.yirami.xyz:3260  # logout specified target
#sudo iscsiadm -m session --rescan
ls -l /dev/disk/by-path/
sudo fdisk -l
df -hT
sudo mount /dev/sdb /home/yirami/share
#sudo umount /home/share
sudo chown -R yirami_debian:yirami_debian /home/yirami/share 
troubleshooting
  1. According to this comment, move source code to /home/user/xxx rather then /mnt/xxx if kernel compile failed

x11 forwarding

step by step
  1. prepare x11 server, recommand mobaxterm or vcxsrv
    • use mobaxterm portable version
    • install vcxsrv | reference
  2. configure environment variable | reference
     export WINDOWS_HOST=$(grep nameserver /etc/resolv.conf | awk '{print $2}')
     export DISPLAY="$WINDOWS_HOST:0.0"
     export LIBGL_ALWAYS_INDIRECT=1
  3. configure firewall | reference
    • add port 6000 for WSL 2 Firewall Unlock
    • disable TCP block rule of xwin_mobax.exe
  4. test
     sudo apt install -y x11-apps
     xclock

Debian 10 / Buster

环境

中文支持

# 配置本地化 Space「选中/取消」 TAB「切换」
sudo dpkg-reconfigure locales

命令补全

sudo apt install bash-completion

C/C++环境

sudo apt install -y g++ gcc cmake make
# 或安装开发套件
sudo apt install -y build-essential

git

  1. WSL环境安装git
  2. 安装wslgit
  3. 修复中文显示问题
    git config --global core.quotepath false
  4. 修复跨平台换行符问题
# 修改全局配置
git config --global core.eol lf
git config --global core.autocrlf input # 对于Linux系统,仅在检入时转换,无需双向转换
git config --global core.safecrlf true
# 或修改项目配置
## 1. 新增 `.gitattributes`文件
## 2. 配置内容
* text eol=lf
# 或手动临时修改
dos2unix

docker

vscode

导致 Windows Defender 高资源占用

添加排除项

  • Code.exe
  • C:\Users\servi\AppData\Local\Programs\Microsoft VS Code
  • C:\Users\servi\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4