現在很多 Linux 套件都需要 systemd 來管理跟啟動服務. 由於 WSL2 是 Windows 下的一個 VM , 所以微軟實作了一個init (PID 1)
來做到快速啓動 WSL VM 以及作為與 windows 溝通的橋樑. 可以執行systemctl 確認是否有 systemd
[root@dev workspace]# systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
[root@dev workspace]#
所幸, GitHub 上已經有幾個專案可以直接拿來建構有systemd(PID 1)環境, 這裡我們使用
DamionGans/ubuntu-wsl2-systemd-script, 只要按照說明操作就可以了
[root@dev workspace]# cp -f /mnt/d/Projects/Github/linux/ubuntu-wsl2-systemd-script/ubuntu-wsl2-systemd-script.sh /root/workspace
[root@dev workspace]# ls
golang hello.c test.sh
hello Makefile ubuntu-wsl2-systemd-script.sh
[root@dev workspace]# chmod +x ubuntu-wsl2-systemd-script.sh
[root@dev workspace]# bash ubuntu-wsl2-systemd-script.sh
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]Get:3 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,445 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [892 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [19.3 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [656 B]
Fetched 2,692 kB in 6s (462 kB/s)
Reading package lists... Done
Selecting previously unselected package fontconfig.
(Reading database ... 54217 files and directories currently installed.)Preparing to unpack .../fontconfig_2.13.1-2ubuntu3_amd64.deb ...
Unpacking fontconfig (2.13.1-2ubuntu3) ...
Setting up fontconfig (2.13.1-2ubuntu3) ...
Regenerating fonts cache... done.
Processing triggers for man-db (2.9.1-1) ...
cp: cannot stat './start-systemd-namespace': No such file or directory
cp: cannot stat './enter-systemd-namespace': No such file or directory
chmod: cannot access '/usr/sbin/enter-systemd-namespace': No such file or directory
/mnt/c/Windows/System32/cmd.exe: Invalid argument
/mnt/c/Windows/System32/cmd.exe: Invalid argument
原文链接
[root@dev workspace]# cp -f /mnt/d/Projects/Github/linux/install-sg.sh /root/workspace
[root@dev workspace]# ls
golang hello hello.c install-sg.sh Makefile test.sh ubuntu-wsl2-systemd-script.sh
[root@dev workspace]# bash install-sg.sh
Exit the WSL terminal and shutdown the WSL env:
PS C:\Users\Administrator> wsl --shutdown
To open a new WSL terminal with systemd enabled, run:
PS C:\Users\Administrator>wsl genie -s
Prove that it works:
[root@dev workspace]# sudo systemctl status time-sync.target
原文链接
https://gist.github.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950