LoginSignup
1
3

WSL2でUSBメモリ認識のため、カーネルビルド

Last updated at Posted at 2023-12-31

概要

image.png

WSL2でUSB関連デバイスを認識して使うために必要なカーネルビルドについてまとめます。

事前準備

Windows 11でWSL2が使えるような設定は割愛させていただきます。

ディストリビューション起動

使えるLinuxディストリビューションを確認します。

PS C:\> wsl --list --online
NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_7_9                        Oracle Linux 7.9
OracleLinux_8_7                        Oracle Linux 8.7
OracleLinux_9_1                        Oracle Linux 9.1
openSUSE-Leap-15.5                     openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5           SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed                    openSUSE Tumbleweed

現在、インストール済みのLinuxディストリビューションを確認します。
二つ見えているのは「Docker Desktop on Windows」をインストールして関連設定しているからです。

PS C:\> wsl --list --verbose
  NAME                   STATE           VERSION
* docker-desktop-data    Stopped         2
  docker-desktop         Stopped         2

Ubuntu 22.04ディストリビューションをインストールします。

PS C:\> wsl --install --distribution Ubuntu-22.04
Installing: Ubuntu 22.04 LTS
[================          29.0%                           ]
Ubuntu 22.04 LTS has been installed.
Launching Ubuntu 22.04 LTS...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: [ユーザー名]
New password: [パスワード]
Retype new password: [パスワード]
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.133.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This message is shown once a day. To disable it please create the
/home/[ユーザー名]/.hushlogin file.
[ユーザー名]@[ホスト名]:~$

手順簡素化のため、[ユーザー名]@[ホスト名]:~$$に置換します。

パッケージインストール

$ sudo apt update
$ sudo apt install -y build-essential flex bison libssl-dev libelf-dev libncurses-dev autoconf libudev-dev libtool bc dwarves fdisk

カーネルビルド

カーネル情報を確認します。

$ uname -r
5.15.133.1-microsoft-standard-WSL2

GitHubからWSL2カーネルをダウンロードします。

$ git clone https://github.com/microsoft/WSL2-Linux-Kernel.git && cd WSL2-Linux-Kernel

GitHubのWSL2カーネルタグ情報から最新版のタグを確認します。
青色が現在インストールされているバージョンで、赤色がこれからビルドするバージョンとなります。

image.png

$ git checkout linux-msft-wsl-5.15.137.3

カーネルのコンフィグレーションします。
回答項目で*をつけます。
終わったら保存して抜けます。

$ cp /proc/config.gz config.gz
$ gunzip config.gz
$ mv config .config
$ sudo make menuconfig

image.png

image.png

| Device Drivers > USB Support
| Device Drivers > USB Support > USB Mass Storage Support
| Device Drivers > USB Support > USB announce new devices
| Device Drivers > USB Support > USB Modem (CDC ACM) support
| Device Drivers > USB Support > USB/IP
| Device Drivers > USB Support > USB/IP > VHCI HCD
| Device Drivers > USB Support > USB/IP > Debug messages for USB/IP
| Device Drivers > USB Support > USB Serial Converter Support
| Device Drivers > USB Support > USB Serial Converter Support > USB FTDI Single port Serial Driver

CPUのユニット数を確認します。

$ getconf _NPROCESSORS_ONLN
12

CPUユニット数を設定してビルドします。

$ sudo make -j [CPUユニット数] && sudo make modules_install -j [CPUユニット数] && sudo make install -j [CPUユニット数]
...
  LD      arch/x86/boot/setup.elf
  OBJCOPY arch/x86/boot/setup.bin
  BUILD   arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready  (#1)
arch/x86/Makefile:142: CONFIG_X86_X32 enabled but no binutils support
  DEPMOD  /lib/modules/5.15.137.3-microsoft-standard-WSL2+
arch/x86/Makefile:142: CONFIG_X86_X32 enabled but no binutils support
sh ./arch/x86/boot/install.sh 5.15.137.3-microsoft-standard-WSL2+ \
        arch/x86/boot/bzImage System.map "/boot"
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 5.15.137.3-microsoft-standard-WSL2+ /boot/vmlinuz-5.15.137.3-microsoft-standard-WSL2+

ビルドしたファイルをWindowsのフォルダに配置してWSL2カーネルとして使えるように設定しておきます。

$ cp arch/x86/boot/bzImage /mnt/c/Users/[Windowsログインユーザー名]/WSL2-USB-Image
$ vi /mnt/c/Users/[Windowsログインユーザー名]/.wslconfig
[wsl2]
kernel=c:\\users\\[Windowsログインユーザー名]\\WSL2-USB-Image
$ exit

カーネル確認

起動しているディストリビューション終了して再度起動します。
カーネルバージョンが変わったら成功です。

PS> wsl --shutdown
PS> wsl --distribution Ubuntu-22.04
$ uname -r
5.15.137.3-microsoft-standard-WSL2+

動作確認

WSL2でUSBを使うためにはWin32とWSL2間でのUSBデバイス共有が必要なので以下の記事でまとめておきましたので、ご参照ください。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3