WSL Ubuntu で SD カードをマウントする方法
Linux ドライバの開発において、SD カードに u-boot、カーネル、ルートファイルシステムなどを書き込む必要がある。WSL(ここでは WSL 2.x を指す)を使用して Linux ドライバ開発を行う場合、まず SD カードを WSL に接続する必要がある。
参考資料:
- https://zhuanlan.zhihu.com/p/661175117
- https://zhuanlan.zhihu.com/p/607493975
SD カードが WSL にマウントできない理由は主に 2 つある:
- WSL は仮想マシンであり、ホスト PC の USB デバイスに直接アクセスできない。この問題は usbipd というブリッジツールで解決する
- WSL のカーネルに USB ストレージデバイス用のドライバが含まれていない。この問題は WSL カーネルイメージの再コンパイルで解決する
usbipd を使用したホストと WSL の USB 接続確立
usbipd は OSI モデルのネットワーク層(IP)とトランスポート層(TCP)の間で USB デバイスと仮想マシンの通信を確立する、つまり IP ネットワーク経由での USB デバイス共有を実現する。
1)usbipd ツールのインストール
# 以下のコマンドでダウンロード・インストール可能(ネットワーク状況により失敗することもある)
winget install --interactive --exact dorssel.usbipd-win
# GitHub のリリースページから直接ダウンロードし、実行ファイルをダブルクリックしてインストールしてもよい
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)指示に従ってホスト PC を再起動する。
WSL カーネルへの USB デバイスドライバ追加
Linux ドライバ開発の通常手順と同じ:ドライバ設定 → カーネルコンパイル
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 を選んだが、結局 5.15.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 を探す
(4)USB Mass Storage support を探し、そのサブ項目をすべて有効化
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(SD カード)デバイスのマウント
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 の状態を 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 で実行すべきであり、WSL Ubuntu のコマンドラインではない
付録:WSL 操作コマンド
参考:https://blog.csdn.net/weixin_38103659/article/details/125135074
wsl -l [--running] # 現在の WSL [実行中] Linux サブシステムを表示
wsl -t [wsl_name] # wsl_name という名前の Linux サブシステムを停止
# 新しい 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)まずローカルブランチを作成し、チェックアウト
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)リモートブランチを直接チェックアウトし、関連するローカルブランチを作成
git checkout -b linux-msft-wsl-6.6.y remotes/origin/linux-msft-wsl-6.6.y
# (3)ローカルに同名ブランチを作成すると、自動的に同名のリモートブランチに関連付けられる
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)まずブランチを作成し、切り替え
git branch v6.0-rc7 v6.0-rc7
git checkout v6.0-rc7
# (2)直接作成
git checkout -b v6.0-rc7 v6.0-rc7