MS_WSLInterop - NetDevInfraWGinOSSConsortium/NetDevInfraWiki GitHub Wiki
- 戻る(Windows Subsystem for Linux)
- WSLでのWindowsとLinuxの相互運用
- WindowsとWSL間の接続
- WSL上での.NET Core開発
ファイルシステム、環境変数、プログラム実行辺りの話。
$ echo 1 > /proc/sys/fs/binfmt_misc/WSLInterop$ echo 0 > /proc/sys/fs/binfmt_misc/WSLInterop補足(現在は設定ファイルで制御する):
binfmt_miscを直接叩く方法は
再起動で元に戻るため、現在は/etc/wsl.confに書くのが標準である。[interop] enabled = false # Windows の実行ファイルを起動しない appendWindowsPath = false # Windows の PATH を引き継がない
appendWindowsPath = falseは、Windows 側の PATH が長大で
Linux 側のコマンド解決が遅くなる・意図せぬ実行ファイルを拾う
といった問題への対処としてよく使われる。
-
WSL 環境に Windows のボリュームをマウントし、
Linux から Windows のファイルにアクセスできるようにする仕組み。 -
以下のようなパスで、Windows のボリュームにアクセス出来る。
$ dir /mnt c d $ dir /mnt/c ...
WSL2から、DrvFs は、P9 に置き換えられる。
補足(
/mnt/cは遅い): WSL2 の 9P はネットワーク ファイル
プロトコルであり、Windows 側のファイルへのアクセスは
WSL1 の DrvFs より大幅に遅い。
逆に、Linux 側(ext4 の仮想ディスク)へのアクセスは WSL2 の方が速い。
このため WSL2 では 作業ディレクトリを/home配下に置き、
/mnt/c配下でビルドしないのが定石である
(Windows から Linux 側を見るときは\\wsl$\<distro>を使う)。
なお、Windows 11 の新しいビルドでは 9P に代わる
より高速な仕組み(VirtioFS など)の導入も進んでいる。
ということなので、WSL で rm -rf / を実行すると、
Windows のファイルが削除され Windows が壊れるという話がある。
- 実行してはいけない Linux コマンド(3)
Windows 10 WSL で『rm -rf /』を実行 | マイナビニュース
https://news.mynavi.jp/article/dont_run_on_linux-3/
※ WSL2については、P9 の仕様次第。
補足(現在の挙動): WSL2 では Windows 側のファイルは
9P 越しにマウントされた別ファイルシステムであり、
rm -rf /は既定で--no-preserve-rootを要求されるうえ、
マウント境界を越えないため、記事のような被害は起きにくい。
とはいえrm -rf /mnt/c/...を明示的に叩けば
Windows 側のファイルは消えるので、
相互運用が「両刃の剣」である点は変わらない。
-
WSL で Windows 側のドライブをマウントする方法
株式会社シーポイントラボ|浜松のシステム開発会社
https://cpoint-lab.co.jp/article/201904/9153/ -
Windows 10 から WSL 上の Linux ファイルへ
読み書き可能に、4 月に予定のアップデートで - Publickey
https://www.publickey1.jp/blog/19/windows_10wsllinux4.html
※ 下記「kledgeb」も参照。
wsl がアクセスできる Windows 環境変数は PATH のみ。
WSLENV はブリッジのための特殊な環境変数。
C:\>set WSLENV=USERPROFILE:USERNAME
C:\>wsl
seigi@nishino:/mnt/c/Users/nishi$ echo $WSLENV
USERPROFILE:USERNAME
補足(
WSLENVのフラグ):WSLENVには
変数名の後ろに/付きのフラグを添えて
パス形式の変換を指示できる。
フラグ 意味 /pパスとして変換する( C:\foo↔/mnt/c/foo)/lパスのリストとして変換する( ;↔:)/uWSL → Windows の方向のみ渡す /wWindows → WSL の方向のみ渡す 例:
set WSLENV=USERPROFILE/p:MYLIST/l
wsl.exe を使用して CMD または PowerShell から Linux バイナリを実行
wsl.exe <command>
-
wsl でのコマンド実行と同様に処理される。
-
sudo、パイプ、ファイルリダイレクトなどが機能する。
-
例
-
バイナリ
-
ls コマンド
wsl ls -la
-
-
パイプ
-
ls(Linux) を findstr(Windows) にパイプ
c:\> wsl ls -la | findstr "root" -
dir(Windows) を grep(Linux) にパイプ
c:\>dir | wsl grep root
-
-
-
Windows バイナリには、ファイル拡張子が含まれている必要がある。
-
また、ファイルの大文字と小文字が一致している必要がある。
-
例
-
バイナリ
-
notepad
$ notepad.exe
-
-
パイプ
-
ls(Linux) を findstr(Windows) にパイプ
$ ls -la | findstr.exe "root" drwxr-xr-x 1 root root 512 May 8 2018 .. drwxrwxrwx 1 seigi seigi 512 May 8 2018 wwwroot
-
ipconfig(Windows) を grep(Linux) にパイプ
$ ipconfig.exe | grep IPv4 IPv4 Address. . . . . . . . . . . : 10.0.75.1 IPv4 Address. . . . . . . . . . . : 192.168.179.2 IPv4 Address. . . . . . . . . . . : 172.17.68.193
-
-
補足(文字コードに注意): Windows バイナリの出力は
日本語環境では **CP932(Shift_JIS)**で返るため、
UTF-8 前提の Linux 側コマンドにパイプすると文字化けする。
ipconfig.exe | iconv -f cp932 -t utf-8 | grep ...のように
変換を挟むか、chcp.com 65001で
コード ページを UTF-8 にしておく必要がある。
上記の例が英語の文字列(root、IPv4)を対象にしているのは、
この問題を避けているとも読める。
- Windows と Linux の相互運用性 | Microsoft Docs
https://docs.microsoft.com/ja-jp/windows/wsl/interop
-
その 52 - Bash から Windows バイナリーを起動する機能の
有効・無効を切り替えるには
https://kledgeb.blogspot.jp/2016/10/wsl-52-bashwindows.html -
その 53 - Windows と Bash の相互運用
https://kledgeb.blogspot.jp/2016/11/wsl-53-windowsbash.html -
Windows と Ubuntu の相互運用の仕組み
-
UNIX ソケット
-
その 131 - Build 17093 の WSL に関する変更点・
UNIX ソケットによる相互運用の改善と WSL の自動設定機能の追加
https://kledgeb.blogspot.com/2018/02/wsl-131-build-17093wslunixwsl.html -
その 132 - Windows と WSL 間で UNIX ソケットを利用した通信が可能に - kledgeb
https://kledgeb.blogspot.com/2018/02/wsl-132-windowswslunix.html
-
-
ファイル
-
1903 未満
-
WSL がサポートするファイルシステム
その 26 -(前編)Linux のファイルシステムと Windows のファイルシステム
https://kledgeb.blogspot.jp/2016/06/wsl-26-wsllinuxwindows.html
その 27 -(後編)WSL のファイルシステム
https://kledgeb.blogspot.jp/2016/06/wsl-27-wslwsl.html -
その 60 - Windows アプリから Ubuntu 環境内のファイルを
決していじってはならない
https://kledgeb.blogspot.jp/2016/11/wsl-60-windowsubuntu.html -
その 109 - Linux から Windows のファイルにアクセスできない不具合
https://kledgeb.blogspot.jp/2017/08/wsl-109-linuxwindows.html
-
-
1903 以後
- その 167 - Windows から Linux のファイルにアクセスする仕組み
(Windows 10 version 1903)
https://kledgeb.blogspot.com/2019/02/wsl-167-windowslinuxwindows-10-version.html
- その 167 - Windows から Linux のファイルにアクセスする仕組み
-
Tags: Windows, Linux, Linuxサブシステム, 仮想化