WSL 内での Gentoo

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Gentoo in WSL and the translation is 83% complete.
Other languages:

このページは、WSL (Windows Subsystem for Linux) 上で Gentoo を実行させる - 実質的に Windows 下で Gentoo を実行する - ためのプロセスと、設定のヒントについて文書化しています。

WSL はハイパーバイザの上で動作する本物の Linux カーネルを使用するので、Linux は本質的には Windows 内で動作します。Gentoo も WSL の上で実行することができます。この記事では WSL バージョン 2 の使用が想定されていますが、ここでの指示はバージョン 1 にも適用できるかもしれません。

WSL をインストールする

Gentoo をインストールする前に、Windows 内で WSL を有効化する必要があります。以下では、--no-distribution フラグによってデフォルトのディストリビューション (Ubuntu) がデフォルトでインストールされないようにしています (もし望むのであれば、これは省いてもかまいません)。

PS >wsl --install --no-distribution

Microsoft による、Windows 10 (ビルド 19041 以降) または Windows 11 およびより古いビルドの Windows 10 に WSL をインストールするためのドキュメンテーションを参照してください。

WSL グローバルオプション

.wslconfig ファイルは、WSL 2 とともに実行されるすべての Linux ディストリビューションについてグローバルに設定を構成します。セクションとしては、RAM、スワップ、プロセッサ数、およびホストポートフォワーディングのための設定が含まれます。ファイル .wslconfig が存在しない場合は、Windows ユーザのホームディレクトリにこのファイルを作成することができます (例: C:\Users\larry\.wslconfig)。以下は WSL をこれらのオプションについて構成する例です:

ファイル %UserProfile%\.wslconfigグローバルオプション
[wsl2]
# Uses the amount specified or the lesser of {8GB OR 50% of the available RAM}
memory=8GB

# see Windows System Information > System Summary > Processor to determine the number available
# Note: should not exceed half of RAM to avoid compilation OOM errors
processors=4

# allow port forwarding (on by default)
localhostforwarding=true

これらの設定は、下で行うインストール後構成 (コンパイラオプションなど) のために重要になってくるでしょう。グローバル構成ファイルのより完全な例はここで見つけることができます。

ディストリビューションごとの構成 (/etc/wsl.conf) は、以降の節で検討します。

メモ
WSL 1 として実行しているディストリビューションは、仮想マシンとして実行されるのではないので、この構成による影響を受けないでしょう。

stage 3 ファイルを使って Gentoo をインポートする

WSL ディストリビューションをロードするために最もよく使用される方法は、Microsoft ストアを使用してインストールする方法、または wsl --install -d <ディストリビューション名> のようなコマンドを実行する方法ですが、Gentoo のためにはより手動によるアプローチが使用されます (執筆時点で Gentoo は公式のディストリビューションに含まれていません - 利用可能なディストリビューションを確認するには wsl --list --online を実行してください)。基本的な Gentoo ファイルシステムをインポートするために、stage 3 ファイルが使用されます。これはベアメタルのインストールと似ています。アーカイブはこれからマウントされるファイルシステム内に展開されます。WSL が上の手順に従ってインストールおよび有効化されていると仮定して、手順は簡略化すると次のようになります:

stage 3 ファイルをダウンロードし、展開し、インポートする。
  1. WSL は x64 および Arm CPU に対応しています。Gentoo ダウンロードページから適切な stage ファイルをダウンロードしてください。
  2. 適切なツール (例: 7-zip - 下のメモを参照) を使用して stage ファイルを展開してください。
  3. Gentoo をインポートするために、管理者権限シェルを開いて、wsl --import <Distro> <InstallLocation> <FileName> を、各引数を適切な値で置き換えて実行してください。例えば (ファイル名をダウンロードおよび展開された stage ファイルに置き換えて):
PS >wsl --import Gentoo C:\Users\Larry\AppData\Local\WSL\Gentoo\ .\stage3-amd64-openrc-20211121T170545Z.tar --version 2
  • Gentoo: 様々な wsl コマンドの出力内に現れる Linux ディストリビューションのラベル (以降は "Gentoo" であると仮定します)
  • C:\Users\Larry\AppData\Local\WSL\Gentoo\: Gentoo に関連する WSL ファイルが保存されるパス (ディレクトリを先に作成する必要があるかもしれません)
  • .\stage3-amd64-openrc-20211121T170545Z.tar: stage 3 ファイルへのパス
  • --version 2: インポートされるディストリビューションのために使用される WSL バージョン (この例では 2 です - 不要かもしれません)
メモ
wsl コマンドに渡される stage 3 ファイルは .tar ファイル名拡張子を持つべきです。Gentoo ミラー上の stage アーカイブは .xz アーカイブですが、展開して .tar ファイルを作成することができます。Windows では、.xz アーカイブを展開できるプログラムとしては 7-Zip があります。

基本的なシステムと構成

初回起動

stage 3 をインポートしたら、その結果として作成された Gentoo システムはすぐに使用することができます。インポートの手順で与えられたラベルが "Gentoo" であったと仮定して、以下のコマンドを使用してシステムをロードすることができます:

PS >wsl -d Gentoo

しかし、いくつかの追加の手順を踏むことが強く推奨されます (次節)。

インストールハンドブック: 推奨されるセットアップ手順

インストールハンドブックの内以下の節は、最小限のシステムのインストールプロセスを完了させるために、使用されるべきです (AMD64 アーキテクチャを仮定しています)。

メモ
WSL の構成は chroot 作業を必要としないため、ハンドブックが /mnt/gentoo で始まるパスに言及しているときはいつでも、この接頭辞はパスから省いて読むべきです。例えば、ハンドブックに /mnt/gentoo/etc/portage/make.conf と書かれている場合は、代わりに /etc/portage/make.conf を使用してください。


一般論として、ハンドブックの内容は、(追加の) 細かな注意点があるものの、そのまま使用することができます。特に、特定の機能 (例: ネットワーキング) はすぐに動作しているため設定する必要がなく、一部の機能 (例: ファイルシステム、ブートローダ) は自動的に提供されるか、当てはまらないため、設定不可能です。さらに、カーネルをカスタマイズすることもできますが、これはこの記事では扱いません。

非 root のデフォルトユーザをセットアップする

WSL 内で Gentoo を開始したとき、デフォルトでは root ユーザが使用されるでしょう。これは安全ではなく、望ましくもありません。それよりも、非 root ユーザを有効化し、デフォルトとして設定することが推奨されます。デフォルトユーザを設定する最も簡単な方法は、(まだ作成されていない場合は) まずユーザを作成して:

root #useradd -m -G wheel larry
root #passwd larry

そして /etc/wsl.conf に以下の行を追加することです:

ファイル /etc/wsl.confWSL 上で Gentoo を起動するときにユーザ larry としてログインする
[user]
default=larry

デフォルトユーザを構成した後でも、wsl コマンド内で明示的にユーザをしていすることで、root セッションを開始することができます。例えば:

PS >wsl -u root -d Gentoo

デフォルトユーザを指定するための他の方法もあります。/etc/wsl.conf 内で指定されている構成はディストリビューションをエクスポート/インポートするときに保持されるので、これはテンプレートを作成するのには適していません。Windows レジストリ内でデフォルトユーザを指定すればエクスポート/インポート時に保持されず、インスタンスを再インポートするときに繰り返す必要があるでしょう。

メモ
Windows build 18980 より前は、デフォルトユーザを追加するための文書化されていない方法は Windows レジストリを編集することによって達成されていました (https://superuser.com/a/1506322/928571 を参照)。この方法はもう必要ありません。

ディストリビューションごとの WSL 構成

ディストリビューションごとの /etc/wsl.conf ファイルを使用して、新しく展開された Gentoo システムのための重要な設定を構成することができます。このファイルはデフォルトでは存在しませんが、簡単に作成することができます(例: touch /etc/wsl.conf)。/etc/wsl.conf では WSL1 と WSL2 の両方を構成することができます。ファイルは ini 構文でモデル化されています。以下のセクションは、これらのファイル内で変更できる多数のオプションを例示します。デフォルト値を持つファイル例:

ファイル /etc/wsl.confExample per-distribution configuration
[automount]
# https://learn.microsoft.com/en-us/windows/wsl/wsl-config#automount-settings
# Automatically mount Windows drive when the distribution is launched
# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

# Sets the directory where fixed drives will be automatically mounted. 
root = /mnt/

# DrvFs-specific options can be specified.  
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"


[network]
# https://learn.microsoft.com/en-us/windows/wsl/wsl-config#network-settings
# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that a custom nameserver can be used (ie. nameserver 1.1.1.1).
# hostname = (DEFAULT WINDOWS HOSTNAME)
generateHosts = true
generateResolvConf = true


[interop]
# https://learn.microsoft.com/en-us/windows/wsl/wsl-config#interop-settings
# Set whether WSL supports interop process like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
enabled = true
appendWindowsPath = true


[user]
# https://learn.microsoft.com/en-us/windows/wsl/wsl-config#user-settings
# Set the user when launching a distribution with WSL. Default is `root`.
# default = larry


[boot]
# https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings
# Set a command to run when a new WSL instance launches.

# To enable systemd, if the system Gentoo profile supports it, uncomment the following line:
# systemd = true

# Additional commands. e.g. Run OpenRC:
# command = /sbin/openrc default

wsl.conf で利用可能なオプションの完全な説明については、WSL での詳細設定の構成を参照してください。

Init システム

WSL 開始時に OpenRC を実行することができます。/etc/wsl.conf に以下を追加してください:

ファイル /etc/wsl.confWSL 開始時に OpenRC を実行する
[boot]
command = "/sbin/openrc default"

WSL ディストリビューション (ラベルとして "Gentoo" を仮定します) を停止した後:

PS >wsl --terminate Gentoo

サービスは、通常の openrc コマンドと同様に有効化して実行することができます。

メモ
シャットダウンしてすべてのプロセスを終了させるには、よりグローバルなコマンドが使用できます: wsl --shutdown
重要
構成設定を適用させるために、ユーザは WSL ディストリビューションが完全にシャットダウンするのを待つ必要があります。典型的にはこれはおよそ 8 秒です (詳細についてはここを参照してください)。

Systemd の場合は、以下を追加してください:

ファイル /etc/wsl.confWSL 開始時に systemd を実行する
[boot]
systemd=true
メモ
systemd を使用したブートがエラーになる場合は、systemd の最初のブートの検出が原因かもしれません。これは以下の変更によって無効化することができます:
ファイル %UserProfile%\.wslconfigsystemd のための回避策
[wsl2]
kernelCommandLine = systemd.firstboot=0

同様に、WSL をシャットダウンするか実行中のディストリビューションを終了させて、通常の方法で WSL ディストリビューションを起動してください (再起動の前に少々待ってください - 上のメモを参照してください)。

WSL ファイルシステム

デフォルトでは、既存のドライブは drvfs ドライバを使用して自動的にマウントされます。これらのドライバには /mnt ディレクトリ下でアクセスすることができます。

メモ
WSL の外のディレクトリ (例:/mnt/c/) で作業していると、パフォーマンスが低下します。そのため、ユーザは代わりに WSL ファイルシステム (例: ユーザのホームディレクトリ ~/) を使用することが推奨されます (ファイル システム間のファイル ストレージとパフォーマンスを参照してください)。

Windows によって認識されていないドライブをマウントするためには、Linux ディスクを WSL 2 にマウントするをお読みください。(Windows ホスト上にすでにマウントされている) USB ドライブをマウントするための現行の手法は、Windows でマウントされた USB ドライブへのアクセスを WSL に与えるために IP プロトコルを使用する usbip を使用しています。さらに詳しい手順については、ここでアクセスすることができます。

X11 または Wayland を使用するグラフィカルプログラム

WSLg (Windows Subsystem for Linux GUI) のおかげで、グラフィカルプログラムは WSL 下で実行することができます。このプロジェクトの目的は、Windows 上で Linux GUI アプリケーション (X11 および Wayland) の実行のサポートを有効化することです。WSLg の「システムディストリビューション」はコンテナ化された Linux 環境で、WSLg の XServer、Wayland サーバ、および Pulse Audio ソケットはこの環境に由来します。WSLg はデバイス /etc/dxg を介して DirectX 12 API を公開し、Windows ホスト上の GPU と直接通信します。これを有効化するためには、/etc/portage/make.conf 内でビデオカードとして d3d12 を有効化してください:

ファイル /etc/portage/make.confd3d12 ビデオカード
VIDEO_CARDS="d3d12"

そして、システムを更新して変更を有効化してください:

root #emerge --ask --verbose --update --deep --changed-use @world
重要
WSL 2 は Wayland コンポジタ、mesa 3D アクセラレーション、および PulseAudio を提供します。現時点で、これらはすぐに機能するはずです。ハンドブックまたはソフトウェアのインストール手順によって求められたとしても、X サーバ、Wayland コンポジタ、ウィンドウ/デスクトップマネージャ、ログインマネージャ、グラフィックスドライバ、オーディオドライバをインストールする必要はありません。適切な USE フラグを使用して、ソフトウェアを起動するだけで、オーディオとグラフィックスは Windows システムとともに機能するでしょう。不要なドライバをインストールすることは問題を発生させることがあります。

Once a Windows NVIDIA GPU driver is installed on the HOST system, CUDA becomes available within WSL 2. The CUDA driver installed on Windows host will be stubbed inside the WSL 2 as libcuda.so, therefore users must not install any NVIDIA GPU Linux driver within WSL 2. The user has to be very careful here as the default CUDA Toolkit comes packaged with a driver, and it is easy to overwrite the WSL 2 NVIDIA driver with the default installation.

重要
CUDA Support: To compile new CUDA applications, a CUDA Toolkit for Linux x86 is needed. CUDA Toolkit support for WSL is still in preview stage as developer tools such as profilers are not available yet. However, CUDA application development is fully supported in the WSL2 environment. Be particularly careful if installing CUDA ([1]).

Optional: Xwayland can be merged to allow older X11 apps to connect to the newer modern Wayland compositor exposed by WSL2 for better performance. Without this, X11 applications will default to using the older WSL backwards compatibility feature of X11 server translation which is also less secure (X11 applications can read and write each other's data).

システムがドライバを認識していることを検証するために、x11-apps/mesa-progs をインストールしてください:

root #emerge --ask --verbose x11-apps/mesa-progs

glxinfo -B 2>/dev/null | grep Device の出力を使用して、グラフィックスカードの名前を検証することができます。

Gentoo システム内で glxgears を実行することによって、Windows のタスクマネージャ内で GPU の使用を観測することができます。

複数の GPU を持つシステム上では、環境変数 MESA_D3D12_DEFAULT_ADAPTER_NAME を GPU の名前の一意な部分文字列 (大文字小文字を区別しません) に設定することによって、希望する GPU を選択することができます。例えば:

root #echo 'MESA_D3D12_DEFAULT_ADAPTER_NAME="nvidia"' > /etc/env.d/99d3d12
root #env-update

systemd では、/tmp の tmpfs によるマウント時にその内の X11 ソケットが隠されてしまうため、アプリケーションが X と通信しようとするとハングしてしまう結果になるでしょう。 これは tmpfiles.d を使用し解決することができます:

root #echo 'L+ /tmp/.X11-unix - - - - /mnt/wslg/.X11-unix' > /etc/tmpfiles.d/wsl.conf

トラブルシューティング

An error occurred mounting one of the file systems

If a new Gentoo installation on Windows 11 fails to start with this error message:

PS >wsl -d Gentoo
An error occurred mounting one of the file systems. Please run 'dmesg' for more details.

Then reinstall Gentoo using a nomultilib stage3 variant.

docker での segmentation fault

If using a docker image with some old glibc distributions inside Gentoo in WSL, segmentation faults may occur. In most cases this happens because vsyscall is off by default. Workaround is to enable its emulation at global WSL configurationː

ファイル %UserProfile%\.wslconfigvsyscall エミュレーションを有効化する
[wsl2]
kernelCommandLine = vsyscall=emulate

Problems with network with active Cisco AnyConnect VPN

If the VPN has no routes to the internet (only intranet), then change VPN interface metrics after each reconnect. Otherwise all traffic from WSL will go to VPN interface. This can be done with the next command on Windowsː

PS >Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

音声がない / ALSA に接続できない

The WSLg component of WSL provides a PULSE_SERVER environment variable. It is only possible to push audio from WSL guest into Windows host through that socket. Ensure the variable PULSE_SERVER is set and the desired software to run was built with pulseaudio support. For example, MPV requires the pulseaudio USE flag to have audio playback from videos.

/usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link

This error message may appear in the kernel log (dmesg or journalctl -kb --grep libcuda). This can be fixed with an administrator cmd with the following commands:

>C:
>cd %WINDIR%\System32\lxss\lib
>del libcuda.so
>del libcuda.so.1
>mklink libcuda.so libcuda.so.1.1
>mklink libcuda.so.1 libcuda.so.1.1

After completing these steps, dir libcuda.so libcuda.so.1 output should look like the following:

C:\Windows\System32\lxss\lib>dir libcuda.so libcuda.so.1
Directory of C:\Windows\System32\lxss\lib
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
03/15/2022 03:59 PM libcuda.so [libcuda.so.1.1]|
03/15/2022 04:00 PM libcuda.so.1 [libcuda.so.1.1]|

/proc/sys/fs/binfmt_misc/WSLInterop-late: Permission denied

VM 内で:

root #echo ':WSLInterop:M::MZ::/init:PF' > /etc/binfmt.d/wsl.conf

VM を再起動すれば、VM 内からの Windows 実行可能形式の起動は機能するはずです。

Windows プログラムが PATH にない

By default, despite setting appendWindowsPath = true, the Gentoo baselayout will overwrite the provided PATH completely. To solve this, edit /etc/profile to preserve the initial PATH:

ファイル /etc/profileProfile Config
# [...]

WSLPATH="$PATH"

# Load environment settings from profile.env, which is created by
# env-update from the files in /etc/env.d
if [ -e /etc/profile.env ] ; then
        . /etc/profile.env
fi

export PATH="$PATH:$WSLPATH"
unset WSLPATH

# [...]

The first line and the last two lines of this block need to be added around the pre-existing middle block at the top of /etc/profile. Zsh users should perform the change in /etc/zsh/zprofile.

関連項目

  • Prefix — enables the power of Gentoo and Portage on other distributions and/or operating systems (Microsoft Windows via Cygwin, Android via Termux, etc.).

外部資料