「WSL」糟了我成替身了
目录
记录 Windows 10/11 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.msimanual install
参考官方分布指引
- 管理员权限启动
PowerShell并执行# 开启 WSL 子系统 dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart # 开启虚拟机功能 dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart - 从发布页下载最新的
*.x64.msi内核更新,并双击安装 - 切换默认版本
wsl --set-default-version 2 - 确认
WSL2是否启用wsl --list --verbose # 如果出错,按提示运行如下命令,并重启 wsl --install --no-distribution - 下载发行版
- 安装发行版
Add-AppxPackage .\TheDebianProject.DebianGNULinux_1.12.2.0_neutral___76v4gfsz19hv4.AppxBundle - 更新发行版(可选)
sudo apt update sudo apt upgrade # cat /etc/os-release sudo apt install -y neofetch && neofetch
mount samba with wsl2
经尝试:Windows主机中无法查看WSL2中成功挂载的SMB共享文件
# 创建本地挂载目录
sudo mkdir -p /mnt/share/samba
# 创建密码文件
sudo tee /etc/.smbcredentials << 'EOF'
username=your_username
password=your_password
EOF
sudo chmod 600 /etc/.smbcredentials
# 创建挂载配置,注意:替换真实用户名和密码
sudo tee /etc/systemd/system/mnt-share-samba.mount << 'EOF'
[Unit]
Description=Mount TrueNAS YArk SMB Share
Documentation=man:mount.cifs(8)
After=network-online.target
Wants=network-online.target
[Mount]
What=//aio.nas.yirami.xyz/YArk
Where=/mnt/share/samba
Type=cifs
Options=username=your_username,password=your_password,vers=3.1.1,sec=ntlmssp,cache=strict,rsize=65536,wsize=65536,uid=1000,gid=1000,_netdev,nofail,iocharset=utf8
[Install]
WantedBy=multi-user.target
EOF
# 重新加载 Systemd 配置并启动服务
sudo systemctl daemon-reload
sudo systemctl start mnt-share-samba.mount
# 检查是否挂载成功
sudo systemctl status mnt-share-samba.mount
# 停止挂载
sudo systemctl stop mnt-share-samba.mountmount nfs with wsl2
经尝试:Windows主机中无法查看WSL2中成功挂载的NFS共享文件
compile kernel
挂载尝试未成功
# 检查内核是否支持 NFS
cat /proc/filesystems | grep nfs
# 准备编译依赖
sudo apt update && sudo apt install build-essential flex bison libssl-dev libelf-dev bc git libncurses-dev -y
# 确认当前内核版本,寻求兼容的内核源码
uname -r
# 配置 git 代理
export ALL_PROXY="http://127.0.0.1:10808"
git config --global http.proxy $ALL_PROXY
git config --global https.proxy $ALL_PROXY
# 下载源码
cd ~
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git --branch linux-msft-wsl-6.18.35.2 --depth=1
cd WSL2-Linux-Kernel
# 生成基础配置并开启 NFS
make menuconfig KCONFIG_CONFIG=Microsoft/config-wsl
# 在弹出的配置界面依次进入
# ... `File systems` -> `Network File Systems` 按 `Y` 勾选
# ... 1) `NFS client support`
# ... 2) `NFS client support for NFS version 3`
# ... 3) `NFS version 4 client support`
# 左右移动到 `Save` 上保存并退出
# 全力编译
make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl
# 拷贝内核
cp ./vmlinux /mnt/c/Users/i/vmlinux-nfs
# 在 `.wslconfig` 中配置
[wsl2]
kernel=C:\\Users\\i\\vmlinux-nfs
# 重启内核
wsl --shutdownexplose 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 directlyuse port mapping [FAILED]
fix command
connect iSCSI by wsl2
introduction
- compile new kernel with iscsi support | reference
- install systemd-genie for systemd support | reference
- install iscsi client
open-iscsi| reference - 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 # check if is WSL2 when "modprobe: FATAL: Module xxx not found in directory /lib/modules/4.4.0-19041-Microsoft", reference to https://unix.stackexchange.com/a/639820
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
- According to this comment, move source code to
/home/user/xxxrather then/mnt/xxxif kernel compile failed
x11 forwarding
较新的 Windows 版本中引入了 WSLg 功能以直接支持 WSL2 内部的图形渲染工作,但目前还不支持渲染 WSL2 中 Docker 容器中的渲染任务。
step by step
- prepare x11 server, recommand mobaxterm or vcxsrv
- use mobaxterm portable version
- install vcxsrv | reference
- 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 - configure firewall | reference
- add port
6000forWSL 2 Firewall Unlock - disable
TCPblock rule ofxwin_mobax.exe
- add port
- test
sudo apt install -y x11-apps xclock
clean vhdx for wsl or docker
# (如需清理)退出 docker
wsl --shutdown
# diskpart 压缩
diskpart
## 定位 vhdx 文件位置
## Docker 通常在: %LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx
## WSL 发行版通常在: %LOCALAPPDATA%\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\ext4.vhdx
select vdisk file="your\path\to\ext4.vhdx"
## 以只读方式挂载
attach vdisk readonly
## 开始压缩
compact vdisk
## 分离磁盘并退出
detach vdisk
exitDebian 10 / Buster
环境
中文支持
# 配置本地化 Space「选中/取消」 TAB「切换」
sudo dpkg-reconfigure locales命令补全
sudo apt install bash-completionC/C++环境
sudo apt install -y g++ gcc cmake make
# 或安装开发套件
sudo apt install -y build-essentialgit
- WSL环境安装
git - 安装wslgit
- 修复中文显示问题
git config --global core.quotepath false- 修复跨平台换行符问题
# 修改全局配置
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
# 或手动临时修改
dos2unixdocker
vscode
导致 Windows Defender 高资源占用
添加排除项
Code.exeC:\Users\servi\AppData\Local\Programs\Microsoft VS CodeC:\Users\servi\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4