ぶていのログでぶログ

思い出したが吉日

WSLでWindowsのEXEが実行できなくなったときの対処

前の記事でUbuntu 23.04 on WSLにしても問題は起こっていない!っと書いた直後に、WindowsのEXEが実行できないことに気がついた…しょぼーん。

EXEを実行すると以下のようなエラーを吐く。

❯ wslview .
grep: /proc/sys/fs/binfmt_misc/WSLInterop: No such file or directory
WSL Interopability is disabled. Please enable it before using WSL.
grep: /proc/sys/fs/binfmt_misc/WSLInterop: No such file or directory
[error] WSL Interoperability is disabled. Please enable it before using WSL.
/usr/bin/wslview: line 216: /mnt/c/Windows/System32/reg.exe: cannot execute binary file: Exec format error
/usr/bin/wslview: line 316: [: -ge: unary operator expected
[error] This protocol is not supported before version 1903.

grep: /proc/sys/fs/binfmt_misc/WSLInterop: No such file or directory っとある通り、WSLInteropがbinfmtに定義されていないのが原因。 binfmtはLinuxカーネルがバイナリファイルをどのように実行するか定義する仕組みである…っと理解している。

docs.kernel.org

本来であればWindowsのEXEを実行する際にこのWSLInteropを読んで判定するのだが、定義がなくてエラーになってしまうという感じ。 この問題を解決するためには以下のコマンドを実行する。

$ echo ":WSLInterop:M::MZ::/init:PF" | sudo tee /usr/lib/binfmt.d/WSLInterop.conf
$ sudo systemctl restart systemd-binfmt

# 登録されている!ls -lah /proc/sys/fs/binfmt_misc/WSLInterop
.rw-r--r-- root root 0 B Fri Apr 21 16:49:50 2023  /proc/sys/fs/binfmt_misc/WSLInterop

これでEXEが実行できるようになるはず。

参考