Cubieboard2(三) 系統(tǒng)構建 —— WSL Ubuntu 中掛載 U 盤(SDCard)

1 WSL Ubuntu 中掛載 U 盤(SDCard)

Linux 驅動開發(fā)需要給 SDCard 燒寫 u-boot,kernal,rootfs 等內容。如果我們要使用 wsl(這里的 wsl 指的是 WSL 2.x 版本,下同) 進行 Linux 驅動開發(fā),那么我們首先要將 SDCard 掛載到 wsl 中。

2 usbipd 搭建虛擬機與宿主機 USB 通信橋梁

usbipd 在 OSI 的網(wǎng)絡層(IP)、傳輸層(TCP)之間建立 USB 設備與虛擬機的通信,即 IP 網(wǎng)絡共享 USB 設備。

  • 1)安裝 usbipd 工具
# 可以通過以下命令下載安裝(我這里可能因為網(wǎng)絡原因失敗了)
winget install --interactive --exact dorssel.usbipd-win

# 也可以直接去其 github 分發(fā)地址下載,下載完成后雙擊安裝即可。
https://github.com/dorssel/usbipd-win/releases
  • 2)回到 wsl ubuntu 中安裝 usbipd 工具:
sudo apt install linux-tools-generic hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*-generic/usbip 20
  • 3)根據(jù)要求重啟宿主機。

3 WSL 內核添加 USB 設備驅動

與 Linux 驅動開發(fā)的流程一樣:配置驅動,編譯內核。

3.1 編譯 WSL Linux 內核

  • 1)編譯工具庫準備
sudo apt install libncurses-dev libgmp-dev  \
    build-essential flex bison libssl-dev libelf-dev dwarves
  • 2)源碼準備:
# 克隆源碼
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git

# 選擇合適的分支檢出(原來這里我選擇 6.6.y 版本,卻沒有六六大順 ^_^)
git branch -a 
git tag

git checkout linux-msft-wsl-5.15.y
  • 3)內核配置文件編輯:
    make menuconfig KCONFIG_CONFIG=Microsoft/config-wsl
    
    • (1)進入 Device Drivers -> USB Support

    • (2)找到 Support for Host-side USB,鍵入 “y”

    • (3)找到 USB/IP Support -> VCHI hcd.

    • image.png
    • (4)找到 USB Mass Storage support,將其子條目全部選中

    • image.png
  • 4)編譯 wsl linux 內核:(如果中途有詢問選項,直接回車即可)
make -j$(nproc) bzImage KCONFIG_CONFIG=Microsoft/config-wsl

# 結果查詢:
find -name bzImage -exec ls -lh {} \;

lrwxrwxrwx 1 root root 22 Sep 11 16:24  ./arch/x86_64/boot/bzImage -> ../../x86/boot/bzImage
-rw-r--r-- 1 root root 16M Sep 11 16:24 ./arch/x86/boot/bzImage

3.2 掛載 USB(SDCard) 設備

  • 1)打開用戶目錄(如 C:\Users\[username],可在資源管理器中輸入 %UserProfile% 打開該目錄),將 bzImage 拷貝進去

  • 2)新建名為 .wslconfig 的配置文件,并輸入以下內容:

    [wsl2]
    kernel=C:\\Users\\[Username]\\bzImage
    # 替換上述 [Username]
    
  • 3)重啟 wsl ubuntu。

# 在 PowerShell 中輸入以下命令
# 終止運行
wsl -t Ubuntu-22.04-cb2 
# 以 root 用戶啟動
wsl -d Ubuntu-22.04-cb2 -u root

# 在 ubuntu 中輸入
$ uname -r
5.15.153.1-microsoft-standard-WSL2+
  • 4)連接:
PS C:\Users\Chris> usbipd list
Connected:
BUSID  VID:PID    DEVICE                                                        STATE
1-19   067b:2731  USB 大容量存儲設備                                            Not shared

Persisted:
GUID                                  DEVICE

PS C:\Users\Chris> usbipd bind -b 1-19
PS C:\Users\Chris> usbipd attach -a --wsl --busid 1-19
usbipd: info: Using WSL distribution 'Ubuntu-22.04-cb2' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Using IP address 172.22.48.1 to reach the host.
usbipd: info: Starting endless attach loop; press Ctrl+C to quit.
WSL Attached
  • 5)連接結果:
$ lsusb
Bus 002 Device 002: ID 067b:2731 Prolific Technology, Inc. USB SD Card Reader
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

$ lsblk
NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda    8:0    0 388.6M  1 disk
sdb    8:16   0     2G  0 disk [SWAP]
sdc    8:32   0     1T  0 disk /snap
                               /mnt/wslg/distro
                               /
sdd    8:48   1  29.1G  0 disk
  • 6)問題日志:
# 情況 1:
usbipd attach -a --wsl --busid 1-19
usbipd: error: Device is not shared; run 'usbipd bind --busid 1-19' as administrator first.
這種情況需要先使用 usbipd bind -b 1-19 命令將 STATE 的狀態(tài)變成 Shared

# 情況 2:
usbipd attach -a --wsl --busid 1-19
usbipd: info: Using WSL distribution 'Ubuntu-22.04-cb2' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Loading vhci_hcd module.
usbipd: error: Loading vhci_hcd failed.
這種情況可以先去 wsl ubuntu 中輸入 modprobe vhci_hcd 指令查看 vhci_hcd 模塊是否安裝,
如果沒有安裝,需要在 make menuconfig 時設置 Device Drivers -> USB Support -> USB/IP Support -> VHCI hcd

# 情況3:
usbipd list
WARNING: usbipd not found for kernel 6.6.36.6-microsoft

  You may need to install the following packages for this specific kernel:
    linux-tools-6.6.36.6-microsoft-standard-WSL2+
    linux-cloud-tools-6.6.36.6-microsoft-standard-WSL2+

  You may also want to install one of the following packages to keep up to date:
    linux-tools-standard-WSL2+
    linux-cloud-tools-standard-WSL2+
這個命令應該在 wsl 中執(zhí)行,而不是 wsl ubuntu 的命令行
    

附錄:WSL 操作命令

wsl -l [--running]  # 列出當前 WSL [運行中] Linux 子系統(tǒng) 
wsl -t [wsl_name]   # 終止名為 wsl_name 的 Linux 子系統(tǒng)

# 打開新 WSL 實例
wsl -d UbuntuRuby               # 僅打開指定版本
wsl -d UbuntuRuby -u username   # 以指定用戶啟動

# 備份與恢復
wsl --export Ubuntu-22.04                 D:\Workspace\WSL_Bak\Ubuntu-22.04-20240908.tar
wsl --import Ubuntu-22.04 D:\Software\WSL D:\Workspace\WSL_Bak\Ubuntu-22.04-20240908.tar

附錄:git 倉庫檢出

# 1)我們想要操作的是遠程【分支】,以 wsl linux 倉庫為例:
$ git branch -a
remotes/origin/HEAD -> origin/linux-msft-wsl-6.6.y
remotes/origin/linux-msft-wsl-4.19.y
remotes/origin/linux-msft-wsl-5.10.y
remotes/origin/linux-msft-wsl-5.15.y
remotes/origin/linux-msft-wsl-5.4.y
remotes/origin/linux-msft-wsl-6.1.y
remotes/origin/linux-msft-wsl-6.6.y
remotes/origin/master
## (1)先創(chuàng)建本地分支,再檢出
git branch linux-msft-wsl-5.15.y remotes/origin/linux-msft-wsl-5.15.y
git checkout linux-msft-wsl-5.15.y
## (2)直接檢出遠程分支,并創(chuàng)建一個與之關聯(lián)的本地分支
git checkout -b linux-msft-wsl-6.6.y remotes/origin/linux-msft-wsl-6.6.y
# (3)在本地新建一個同名分支,系統(tǒng)會自動關聯(lián)其同名遠程分支
git checkout linux-msft-wsl-6.6.y

# 2)我們想要操作的是【標簽】,以 linux 倉庫為例
$ git tag | grep v6.0
v6.0
v6.0-rc1
v6.0-rc2
v6.0-rc3
v6.0-rc4
v6.0-rc5
v6.0-rc6
v6.0-rc7
# (1)先創(chuàng)建分支,再切換
git branch v6.0-rc7 v6.0-rc7 
git checkout v6.0-rc7 
# (2)直接創(chuàng)建
git checkout -b v6.0-rc7 v6.0-rc7 
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容