WSL(Windows Subsystem Linux)でsystemctlを動かす方法

更新日 2021-07-07 (水) 15:27:25

WSLでubuntuを起動するとPIDが1のプロセスは「/init」になっている。そのため、「systemctl」を実行すると以下のようなエラーがでる

System has not been booted with systemd as init system (PID 1). Can't operate.

PIDが1のプロセスを「systemd」になるようにする。そのためには、「genie」を動かす

genieは、WSL2上でsystemd を起動するためのコマンドです。genieはWSL2の中でPID名前空間を使用して、systemdのpidが1の状態で起動します。これにより、systemdによるサービスの制御が行えます。(https://www.school.ctc-g.co.jp/columns/miyazaki/miyazaki22.html)

本内容を設定するとWSLgが動作しなくなった。xeyesのようなGUIが表示しなくなる。現時点で対策不明

Genieの依存パッケージであるdaemonize/dbus/policykit-1をインストール

# apt install daemonize dbus policykit-1

packages-microsoft-prodをDLしてインストール

packages-microsoft-prodはマイクロソフトのリポジトリPPA(Personal Package Archives)で、これをインストールすることで、ubuntuからaptコマンドでパッケージがインストールできるようになる。

$ wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb dpkg -i packages-microsoft-prod.deb

--2021-07-02 09:52:23--  https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb
Resolving packages.microsoft.com (packages.microsoft.com)... 13.75.64.135, 40.90.4.5, 40.90.4.5, ...
Connecting to packages.microsoft.com (packages.microsoft.com)|13.75.64.135|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3124 (3.1K) [application/octet-stream]
Saving to: ‘packages-microsoft-prod.deb’

packages-microsoft-prod.deb   100%
[=================================================>]   3.05K  --.-KB/s    in 0s
 
2021-07-02 09:52:24 (488 MB/s) - ‘packages-microsoft-prod.deb’ saved 
[3124/3124]

--2021-07-02 09:52:24--  http://dpkg/
Resolving dpkg (dpkg)... failed: Name or service not known.
wget: unable to resolve host address ‘dpkg’
packages-microsoft-prod.deb: Invalid URL http://(ここから文字化け)
 
^Y�x: Invalid host name
packages-microsoft-prod.deb: Invalid URL http://?(ここから文字化け)
                                                                    
\����: Invalid host name
packages-microsoft-prod.deb: Invalid URL http:/(ここから文字化)
 
Inv alid host name
--2021-07-02 09:52:25--  http://!%3Carch%3E/
Resolving !<arch> (!<arch>)... failed: Name or service not known.
wget: unable to resolve host address ‘!<arch>’
--2021-07-02 09:52:25--  http://debian-binary/%20%200%20%20%20%20%20%20%20%...
Resolving debian-binary (debian-binary)... failed: Name or service not known.
wget: unable to resolve host address ‘debian-binary’
--2021-07-02 09:52:26--  http://2.0/
Resolving 2.0 (2.0)... 2.0.0.0
Connecting to 2.0 (2.0)|2.0.0.0|:80... ^C
  • DLを確認
root@DESKTOP-U6QLQDT:/home/okada# ls
packages-microsoft-prod.deb
  • システム更新
# apt update
# apt upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package dotnet-runtime-3.1
E: Couldn't find any package by glob 'dotnet-runtime-3.1'
E: Couldn't find any package by regex 'dotnet-runtime-3.1'
  • packages-microsoft-prodをインストール
# dpkg --purge packages-microsoft-prod && dpkg -i packages-microsoft-prod.deb
dpkg: warning: ignoring request to remove packages-microsoft-prod which isn't installed
Selecting previously unselected package packages-microsoft-prod.
(Reading database ... 72517 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-debian10.1) ...
Setting up packages-microsoft-prod (1.0-debian10.1) ...
# apt-get update

dotnet-runtime-3.1のインストール

# sudo apt-get install -y dotnet-runtime-3.1
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  dotnet-host dotnet-hostfxr-3.1 dotnet-runtime-deps-3.1
The following NEW packages will be installed:
  dotnet-host dotnet-hostfxr-3.1 dotnet-runtime-3.1 dotnet-runtime-deps-3.1
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 21.9 MB of archives.

(略)

Unpacking dotnet-runtime-3.1 (3.1.16-1) ...
Setting up dotnet-host (5.0.7-1) ...
Setting up dotnet-runtime-deps-3.1 (3.1.16-1) ...
Setting up dotnet-hostfxr-3.1 (3.1.16-1) ...
Setting up dotnet-runtime-3.1 (3.1.16-1) ...
Processing triggers for man-db (2.9.1-1) ...

wsl-transdebianリポジトリの構成

# apt install apt-transport-https

/etc/apt/trusted.gpg.d/にwsl-transdebian.gpgをDL

# 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

/etc/apt/sources.list.d/wsl-transdebian.listを新規に作成し、以下の2行を入れる

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
  • 上の2行のを入れるコマンド
# 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

genieのインストール

# apt update
# apt install -y systemd-genie
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
 dotnet-hostfxr-5.0 dotnet-runtime-5.0 dotnet-runtime-deps-5.0 libnss-mymachines systemd-container
The following NEW packages will be installed:
(略)
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for dbus (1.12.16-2ubuntu2.1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

genieの起動

$ genie -s
Waiting for systemd....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Timed out waiting for systemd to enter running state.
This may indicate a systemd configuration error.
Attempting to continue.
Failed units will now be displayed (systemctl list-units --failed):
  UNIT                       LOAD   ACTIVE SUB    DESCRIPTION
● ssh.service                loaded failed failed OpenBSD Secure Shell server 
● systemd-remount-fs.service loaded failed failed Remount Root and Kernel File Systems
● multipathd.socket          loaded failed failed multipathd control socket

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

3 loaded units listed.

PID 1が「systemd」になっているか確認

$ ps -aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.7  0.2 175252 13812 ?        Ss   11:03   0:02 systemd
root          51  0.1  0.2  43232 17940 ?        S<s  11:03   0:00 /lib/systemd/systemd-journald
root          71  0.1  0.1  20028  6576 ?        Ss   11:03   0:00 /lib/systemd/systemd-udevd
systemd+      76  0.0  0.1  18412  7708 ?        Ss   11:03   0:00 /lib/systemd/systemd-networkd
systemd+     246  0.0  0.2  23960 12276 ?        Ss   11:03   0:00 /lib/systemd/systemd-resolved

systemctlコマンドの確認

$ systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Fri 2021-07-02 11:03:34 JST; 5min ago
       Docs: man:sshd(8)
             man:sshd_config(5)

接続時、PID 1をsystemdになるようにする

.bashrcの文末に以下を追加

if [ "`ps -eo pid,cmd | grep systemd | grep -v grep | sort -n -k 1 | awk 'NR==1 { print $1 }'`" != "1" ]; then
 genie -s
fi

参考


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2021-07-07 (水) 15:27:25 (82d)