LoginSignup
3
5

More than 3 years have passed since last update.

WSL2上のCentOS8でsystemctlを使えるようにする

Posted at

はじめに

CentOS WSLを使うと、WSL2上でCentOS8を使えて便利です。
ただsystemctlを呼びだそうとすると、次のようなエラーメッセージが表示されて使えません。(2021年4月現在)
systemctlが使えないとMariaDBなどのサービスが起動できず不便です。

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

起動しない理由や対策

起動しない理由や対策は、こちらのブログ記事の説明が詳しいです。

systemctlを有効化する

上記の記事によると、systemctlを使えるようにするには、genieを用いる方法が解説されています。
記事では、make installでビルドしてインストールする方法が説明されていますが
genieのgithubのページを見ると、releasesにfedora用のrpmが公開されています。
CentOS8でもこのrpmが使えました。

なおgenieをインストールする前には、EPELを有効にしておきます。
rpmインストール時に依存するdaemonizeやdotnet-runtimeもインストールされます。

dnf install epel-release
cd /root/
wget https://github.com/arkane-systems/genie/releases/download/v1.36/genie-1.36-1.fc33.x86_64.rpm
dnf install genie-1.36-1.fc33.x86_64.rpm

WSL 1で起動している場合

環境によってはgenie -sを実行すると、WSL2ではなく WSL1で起動している旨にエラーが表示される場合があります。
この場合、PowerShellかコマンドプロンプトでWSLの既定バージョンを2に変えてからCentOS8 WSLをインストールします。

wsl --list --verbose
wsl --set-default-version 2

次回起動時にも有効化する

.bashrcの末尾にコマンドを入れておきます。

vi /root/.bashrc
# 末尾に追記する
if [ "`ps -eo pid,lstart,cmd | grep systemd | grep -v -e grep -e systemd- | sort -n -k2 | awk 'NR==1 { print $1 }'`" != "1" ]; then
  genie -s
fi

これでsystemctlが使えるようになります。

参考

3
5
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
3
5