Bỏ qua đến nội dung chính

Đồng bộ WSL gốc (nâng cao)

Chương quan trọng nhất nếu bạn dùng WSL. Real performance numbers từ kinh nghiệm thực.

Vấn đề: Syncthing trên Windows đọc file WSL chậm

Phần tiêu đề “Vấn đề: Syncthing trên Windows đọc file WSL chậm”

Nếu bạn add folder Windows Syncthing với path \\wsl.localhost\Ubuntu\home\<user>\projects (cross-OS), bạn sẽ gặp:

Folder: wsl-projects
Total scanned: 1.07 GB / 5 phút
Rate: ~33 MB/phút 🐌

24 GB folder ~= 12 giờ initial scan. Daily incremental sync rất chậm. File watcher unreliable.

Windows Syncthing process
Đọc file qua 9P bridge (Windows ↔ WSL VM) ← BOTTLENECK
WSL ext4 filesystem

9P Plan 9 protocol là cách Windows access WSL filesystem. Slow vì:

  • Each file read = round-trip Windows → WSL VM → ext4 → response
  • Per-file overhead lớn (~ms each) cho many small files
  • Hash 24GB qua 9P = unbearable
Trước:
Windows Syncthing → 9P bridge → WSL ext4 (33 MB/min)
Sau:
WSL Syncthing native → ext4 (3952 MB/min) — 120x faster

Trade-off: 2 Syncthing instances per machine (1 Windows + 1 WSL).

PC machine Laptop machine
┌─────────────────────────────┐ ┌─────────────────────────────┐
│ Windows │ │ Windows │
│ ├─ Syncthing :8384 (BEP 22000)│ │ ├─ Syncthing :8384 │
│ │ ├─ ~/.claude/ │ ←────→ │ │ ├─ ~/.claude/ │
│ │ └─ Documents/ │ │ │ └─ Documents/ │
│ │ │ │ │ │
│ └─ WSL Ubuntu │ │ └─ WSL Ubuntu │
│ └─ Syncthing :8385 │ │ └─ Syncthing :8385 │
│ (BEP 22001) │ ←────→ │ (BEP 22001) │
│ └─ ~/projects/ │ │ └─ ~/projects/ │
└─────────────────────────────┘ └─────────────────────────────┘

4 Device IDs total (2 instances × 2 machines).

Without mirrored mode, WSL VM có private NAT IP (172.x.x.x) — laptop’s WSL không reach PC’s WSL được.

Edit C:\Users\<user>\.wslconfig trên cả 2 máy:

[wsl2]
networkingMode=mirrored
firewall=true
dnsTunneling=true
autoProxy=true
[experimental]
hostAddressLoopback=true

Restart WSL:

Terminal window
wsl --shutdown
# Wait 5s
wsl bash -c "hostname -I"
# Expect: shows host LAN IP (vd 192.168.1.14)
Terminal window
# Inside WSL Ubuntu
sudo apt update
sudo apt install -y syncthing

Vấn đề: WSL share host network → port 22000 (BEP) + 8384 (GUI) đã dùng bởi Windows Syncthing → conflict.

Solution: change WSL Syncthing ports.

Terminal window
# Generate config with separate home dir
syncthing generate --home ~/.config/syncthing-wsl
# Change ports
sed -i 's|<address>127.0.0.1:8384</address>|<address>127.0.0.1:8385</address>|' ~/.config/syncthing-wsl/config.xml
sed -i 's|<listenAddress>default</listenAddress>|<listenAddress>tcp://0.0.0.0:22001</listenAddress><listenAddress>quic://0.0.0.0:22001</listenAddress>|' ~/.config/syncthing-wsl/config.xml
sed -i 's|<localAnnouncePort>21027|<localAnnouncePort>21028|' ~/.config/syncthing-wsl/config.xml
# Verify
grep -E "listenAddress|<address>|localAnnouncePort" ~/.config/syncthing-wsl/config.xml

Port allocation table:

ServiceWindows SyncthingWSL Syncthing
GUI Web UI83848385
BEP sync (TCP+QUIC)2200022001
Local discovery2102721028

Bước 4: Khởi động daemon Syncthing trong WSL

Phần tiêu đề “Bước 4: Khởi động daemon Syncthing trong WSL”
Terminal window
nohup syncthing serve --home ~/.config/syncthing-wsl --no-browser > ~/syncthing-wsl.log 2>&1 &
sleep 5
# Verify
curl -s -o /dev/null -w "Web UI: HTTP %{http_code}\n" http://127.0.0.1:8385
# Expect: HTTP 200
Terminal window
# From config.xml
grep -oE '<device id="[^"]+" name="[^"]*"' ~/.config/syncthing-wsl/config.xml | head -1
# Output (example):
# <device id="ABCD123-EFG456-IJKL789-..." name="My WSL"

Note Device ID này — sẽ dùng cho pairing.

Bước 6: Tự khởi động Syncthing khi WSL boot

Phần tiêu đề “Bước 6: Tự khởi động Syncthing khi WSL boot”

Default WSL Syncthing không tự start sau wsl --shutdown. Add to startup:

Terminal window
# Add to ~/.bashrc hoặc ~/.zshrc
cat >> ~/.bashrc << 'EOF'
# Auto-start Syncthing WSL daemon (skip if already running)
if ! pgrep -f "syncthing.*syncthing-wsl" > /dev/null; then
nohup syncthing serve --home ~/.config/syncthing-wsl --no-browser > ~/syncthing-wsl.log 2>&1 &
fi
EOF

Repeat Step 1-6 trên Laptop.

Sau đó pair:

  1. Trên PC’s WSL Web UI http://127.0.0.1:8385:

    • Add Remote Device → paste Laptop’s WSL Device ID
  2. Trên Laptop’s WSL Web UI:

    • Auto-detect notification → click Add Device, accept
  3. Verify connection trong Web UI → Remote Devices section:

    • Status: Connected
    • Address: shows host LAN IP (vd 192.168.1.16:22001)
    • Type: TCP LAN hoặc QUIC
  • Path: /home/<user>/projects (native ext4)
  • Sharing: tick Laptop’s WSL device
  • Ignore Patterns — critical cho code projects:
**/node_modules
**/dist
**/build
**/.next
**/out
**/.venv
**/venv
**/__pycache__
**/*.pyc
**/coverage
**/data
**/postgres-data
**/.docker
**/*.log
**/.cache
**/tmp
**/.env.local
.DS_Store
Thumbs.db
  • Path: /home/<user>/projects (mirror PC)

Native ext4 reads = fast scan + transfer.

Em đã measure thực tế setup AIWS production:

MetricWindows reads WSL (9P)WSL NativeSpeedup
Initial scan rate33 MB/phút3952 MB/phút120x faster
24 GB folder scan~12 giờ~6 phút120x
Sync rate (LAN)46 KiB/s80-100 MB/s~2000x
File watcherUnreliableReliable

Windows Syncthing đã dùng 22000. WSL phải dùng 22001 — xem Step 3.

Verify với:

Terminal window
ss -tlnp | grep -E ":22000|:22001|:8384|:8385"

WSL native sync đã ready. Real-world use cases: