good-arrow’s blog

https://good-arrow.net/

WSL2 で systemctl を使うメモ

(2021.09.02 時点)

WSL2 で sudo systemctl start XXX 実行時のエラー

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down


genie を使った対応 Ubuntu 20.04 LTS

genie の 公式GitHub
https://github.com/arkane-systems/genie
手順が更新される場合があるので、最新の情報は上記を参照のこと。

UbuntuDebianディストリビューションなので Debian の項を参照します。
https://github.com/arkane-systems/genie#debian


.NET ランライム 5.0 をインストールする

https://docs.microsoft.com/ja-jp/dotnet/core/install/linux-ubuntu#2004-

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
  • ランタイムのインストール
sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y aspnetcore-runtime-5.0


wsl-transdebian リポジトリを追加する

root に切り替えて実行します

sudo -s
wget -O /etc/apt/trusted.gpg.d/wsl-transdebian.gpg https://arkane-systems.github.io/wsl-transdebian/apt/wsl-transdebian.gpg
chmod a+r /etc/apt/trusted.gpg.d/wsl-transdebian.gpg
cat << EOF > /etc/apt/sources.list.d/wsl-transdebian.list
deb https://arkane-systems.github.io/wsl-transdebian/apt/ $(lsb_release -cs) main
deb-src https://arkane-systems.github.io/wsl-transdebian/apt/ $(lsb_release -cs) main
EOF
apt update
exit


genie をインストールする

sudo apt update
sudo apt install -y systemd-genie


genie を実行する

genie -s、genie -lまたはgenie -c のいずれかで実行します。

ps aux で確認し、PID「1」が systemd となっていれば OK です。

$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.1 173960 12032 ?        Ss   13:00   0:01 systemd

systemctl で起動できる事を確認します。

sudo systemctl start XXX
sudo systemctl status XXX


genie コマンド

genie:
  Handles transitions to the "bottle" namespace for systemd under WSL.

Usage:
  genie [options] [command]

Options:
  -v, --verbose <VERBOSE>    Display verbose progress messages
  --version                  Display version information

Commands:
  -i, --initialize           Initialize the bottle (if necessary) only.
  -s, --shell                Initialize the bottle (if necessary), and run a shell in it.
  -l, --login                Initialize the bottle (if necessary), and open a logon prompt in it.
  -c, --command <COMMAND>    Initialize the bottle (if necessary), and run the specified command in it.
  -u, --shutdown             Shut down systemd and exit the bottle.
  -r, --is-running           Check whether systemd is running in genie, or not.
  -b, --is-in-bottle         Check whether currently executing within the genie bottle, or not.