LoginSignup
38
37

More than 1 year has passed since last update.

WSL2でsystemdを動かすDistrodが便利だった件

Last updated at Posted at 2022-05-05

この記事について

WSL 上にインストールした Linux で systemctl コマンドが使用できない問題を解決します。
この記事では Ubuntu 20.04 LTS を使用して説明していますが、他のディストリビューションでも手順に大きな違いは無いと思いますので、参考にしていただければと思います。

環境

この記事は以下の環境にて動作を確認しています。

  • Windows 10 Pro 21H2 ( OS Build : 19044.1645 )
  • Windows Terminal v1.12.10982.0
  • Distrod v0.1.5
  • WSL2 ( Kernel version: 5.10.102.1 )

Distrodとは?

公式では以下の様に説明されています。

Distrod is a systemd-based meta-distro for WSL2 that allows you to install Ubuntu, Arch Linux, Gentoo and many other distros with systemd in a minute, or make your current distro run systemd.
Distrod also provides built-in auto-start feature and port forwarding service. This allows you to start systemd-managed services, such as ssh, on Windows startup and make it accessible from outside Windows.

日本語訳
Distrod は WSL2 用の systemd ベースのメタディストリビューションで、Ubuntu, Arch Linux, Gentoo やその他多くの systemd 付きディストリビューションを1分でインストールしたり、現在使用しているディストロを systemd で動作させたりすることができます。
Distrod はビルトインの自動起動機能とポートフォワーディングサービスも提供します。これにより、ssh などの systemd が管理するサービスを Windows のスタートアップで起動し、Windows の外からアクセスできるようにすることができます。

arkane-systems/genie を使って systemd を PID1 で動作させる方法が広く知られていますが、Distrod も簡単&便利なので試してみてください。

WSL2 の利用準備

下記記事の WSL2 の利用準備 を参照

Distrod のインストール

nullpo-head/wsl-distrod から最新版の distrod_wsl_launcher-x86_64.zip をダウンロードし、適当な場所に展開するだけです。
この記事では、展開した実行ファイルのパスが C:\tools\distrod_wsl_launcher-x86_64.exe となっている前提で説明していきます。

Distrod の実行 ( = Linux のインストール )

Windows Terminal ( PowerShell ) を管理者権限で起動して、distrod_wsl_launcher-x86_64.exe を実行します。

Administrator: PowerShell : 仮想マシン名を指定しない場合
# 仮想マシン名としてデフォルト名 ( Distrod ) が設定されます
$ cd C:\tools
$ ./distrod_wsl_launcher.exe
Administrator: PowerShell : 仮想マシン名を指定する場合
# 仮想マシン名として Ubuntu20 が設定されます
$ cd C:\tools
$ ./distrod_wsl_launcher.exe -d Ubuntu20

2022-05-05_233422.png
linuxcontainers.org からイメージファイルをダウンロードしたいので 2 を選択します。

Administrator: PowerShell
[1] Use a local tar.xz file
[2] Download an image from linuxcontainers.org
[Distrod] Choose the way to get a distro image from the list above.
[Distrod] Type the name or the index of your choice.
[Default: Download an image from linuxcontainers.org]:2

2022-05-05_233743.png
ubuntu をインストールしたいので 23 を選択します。

Administrator: PowerShell
[Distrod] Choose a linuxcontainers.org image from the list above.
[Distrod] Type the name or the index of your choice.
[Default: ubuntu]: 23

2022-05-05_234119.png
続けて Ubuntu のバージョンを選択します。
ややこしいですが、Ubuntu 20.04 LTSFocal Fossa 1 なので 3 を選択します。

Administrator: PowerShell
[Distrod] Fetching from linuxcontainers.org...
[1] bionic
[2] impish
[3] focal
[4] xenial
[5] jammy
[6] kinetic
[Distrod] Choose a version from the list above.
[Distrod] Type the name or the index of your choice.
[Default: focal]: 3

ここからは見慣れた設問です。
Ubuntu に追加するユーザー名を入力し、パスワードを設定します。

Administrator: PowerShell
[Distrod] Fetching from linuxcontainers.org...
[Distrod] Downloading 'https://images.linuxcontainers.org/images/ubuntu/focal/amd64/default/20220505_09:40/rootfs.tar.xz'...
  [00:00:20] [##############] 98.03MiB/98.03MiB (4.84MiB/s, 0s)
[Distrod] Download done.
[Distrod] Unpacking and merging the given rootfs to the distrod rootfs. This may take a while...
[Distrod] Now Windows is installing the new distribution. This may take a while...
[Distrod] Distrod is installed in %LocalAppData%\Distrod
[Distrod] Done!
[Distrod] Please input the new Linux user name. This doesn't have to be the same as your Windows user name.
[Input user name]: slangsoft
New password:
Retype new password:

以下の様に表示されればインストールは完了です。

Administrator: PowerShell
passwd: password updated successfully
[Distrod] Querying the generated uid. This may take some time depending on your machine.
[Distrod] Initializing the new Distrod distribution. This may take a while...
[Distrod] Distrod has been enabled. Now your shell will start under systemd.
[Distrod] Setting the default user to uid: 1001
[Distrod] Installation of Distrod is now complete.

最後に、肝心の systemctl コマンドが使用できるかを確認します。

Administrator: PowerShell
$ systemctl --version

正しく動作している場合は、以下の様に systemd のバージョン情報が表示されます。

Administrator: PowerShell
systemd 245 (245.4-4ubuntu3.16)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

Ubuntu への再接続

Distrod でインストールした Ubuntu には、Windows Terminal から簡単に再接続することができます。
2022-05-15.png

補足

WSL2 上にインストールした Linux の確認

PowerShell
$ wsl --list
Linux  Windows サブシステム ディストリビューション:
docker-desktop-data (既定)
Distrod

不要になった Linux の削除

PowerShell を管理者権限で起動し、以下のコマンドを実行することで削除できます。

Administrator: PowerShell
# ディストリビューション名を確認
$ wsl --list
Linux  Windows サブシステム ディストリビューション:
docker-desktop-data (既定)
Distrod  # これを削除したい場合

# 登録の解除
$ wsl --unregister Distrod

# 関連ファイルの削除
# <username> : Windows のユーザー名
$ Remove-Item -Path C:\Users\<username>\AppData\Local\Distrod -Recurse -Force

あとがき

冒頭で「systemctl コマンドが使用できない問題を解決します」と書きましたが、Distrod を使えば systemctl の問題は発生しません。しかも linuxcontainers.org から Linux の OS イメージを自動で落としてくれる優れ者でした。

参考サイト様

  1. バージョン番号と名称の対応は Ubuntuのバージョン履歴 を参照

38
37
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
38
37