🐧

Arch Linux on WSL2 をやる方法

2023/10/09に公開

要約

WSL2のディストリビューションを非公式だがArchLinuxにしたい!
→ できた

前提

  • WSL2のインストール(私は Ubuntu on WSL)
  • dockerの実行環境(私は Windows11側にインストール)
  • この例では,ArchLinuxのユーザー名を userName にする(任意の名前を使用可能)
  • この例では,ArchLinuxのデータを C:\arch\ext4.vhdx に保存する(任意のディレクトリを指定可能)

経過観察

手順

ArchLinuxの準備 in wsl

  • wslではなくpwshでも可能だと思われる(未検証)
wsl
docker run -it --name foo archlinux

ユーザー作成 in arch on docker

  • ユーザー作成時にパスワードを設定するので標準入力が必要
  • pacmanでインストール時の yes/no を尋ねるようにするには --noconfirm オプションを外す
  • sed ではなく EDITOR=vim visudo でも可能(寧ろ そちらの方が一般的)
arch on docker
useradd -m -G wheel userName
passwd userName
pacman -Syu --noconfirm
pacman -Syu sudo vim --noconfirm
sed -i "s/# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/g" /etc/sudoers | EDITOR=tee visudo >/dev/null

ログインユーザー変更 in arch on docker

  • wslで実行したときに,ログインするデフォルトユーザー名を変更する
  • こちらも任意のエディタで編集しても同じ結果が得られる(未検証)
arch on docker
cat << EOF > /etc/wsl.conf
[user]
default=userName
EOF
  • 全部終わったので docker から exit する
arch on docker
exit

エクスポート in wsl

wslではなくpwshでも可能だと思われる(未検証)

wsl
mkdir /mnt/c/arch
docker export foo > /mnt/c/arch/ArchLinuxImage.tar

インポート in pwsh

pwshではなくwslでも可能だと思われる(未検証)が,wsl内部から別のwslを操作するのは明瞭ではない.

  • 相対パスで指定も可能
powershell
wsl --import Arch C:\arch C:\arch\ArchLinuxImage.tar
wsl -d Arch

Dockerの追加設定

WSLとDockerの統合を有効にする.

The command 'docker' could not be found in this WSL 2 distro.
We recommend to activate the WSL integration in Docker Desktop settings.

For details about using Docker Desktop with WSL 2, visit:

https://docs.docker.com/go/wsl2/
というような警告が出た場合に有効.

  • Docker Desktop を起動
  • 右上の⚙をクリック
  • 左のサイドバーから Resources → WSL integration を選択
  • Arch のトグルをONにする
  • 右下の Apply & restart で保存する

WindowsTerminalの追加設定

現在,WindowsTerminalでは画面上部の﹀からArchを起動することができないはず.
ここにArch追加するには2つの方法がある.

再起動

  • WindowsTerminalあるいはWindowsOSを再起動する.

手動で追加する

  • WindowsTerminalの 設定 → プロファイル
  • JSONで追加する方向けに例を記述しておく
  • guid は こちら などで作成できるはず
{
    "guid": "{a5a97cb8-8961-5535-816d-772efe0c6a3f}",
    "hidden": false,
    "icon": "C:\\images\\arch.png",
    "name": "Arch",
    "source": "Windows.Terminal.Wsl"
}

wslの設定の変更

/etc/wsl.conf の設定を自分用に付け足す(以下が完成形)

/etc/wsl.conf
# https://learn.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c.
root = /

# 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 you can create your own (ie. nameserver 1.1.1.1).
[network]
generateResolvConf = true
# generateResolvConf = true

[user]
default = ray

[boot]
systemd = true

削除方法

削除方法も記しておく.ブックマークなど保存しておくと良いかも?

ディストリビューション一覧

wsl -l

削除

wsl --unregister ディストリビューション名

実行環境

wsl.exe -v

WSL バージョン: 1.2.5.0
カーネル バージョン: 5.15.90.1
WSLg バージョン: 1.0.51
MSRDC バージョン: 1.2.3770
Direct3D バージョン: 1.608.2-61064218
DXCore バージョン: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows バージョン: 10.0.22621.2283

docker -v

Docker version 24.0.6, build ed223bc

参考文献

GitHubで編集を提案

Discussion