云梦
www.htcp.net

利用 genie 工具在 WSL2 中启用 systemd

前言

由于 WSL2 不支持 systemd,导致 rc.local cron 等都不能用,非常让人苦恼。

偶然间在 Github 找到了 genie,可用用它来加载 systemd,非常好用。下面是教程。

一、安装 systemd-genie

sudo apt install apt-transport-https

sudo wget -O /etc/apt/trusted.gpg.d/wsl-transdebian.gpg https://arkane-systems.github.io/wsl-transdebian/apt/wsl-transdebian.gpg

sudo chmod a+r /etc/apt/trusted.gpg.d/wsl-transdebian.gpg

sudo 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

sudo apt update
sudo apt -y install systemd-genie

二、解决一些 BUG

安装完 genie,直接 wsl genie -s 使用的话,可能会出现一些报错,如:

genie: WARNING: systemd default target is default.target; targets other than multi-user.target may not work
genie: WARNING: if you wish to use a different target, this warning can be disabled in the config file
genie: WARNING: if you experience problems, please change the target to multi-user.target
Waiting for systemd….!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
genie: systemd did not enter running state (degraded) after 240 seconds
genie: this may be due to a problem with your systemd configuration
genie: information on problematic units is available at https://github.com/arkane-systems/genie/wiki/Systemd-units-known-to-be-problematic-under-WSL
genie: a list of failed units follows:

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
● systemd-sysusers.service loaded failed failed Create System Users

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.
genie: WARNING: systemd is in degraded state, issues may occur!

我们一条一条的解决。

1. 修改 systemd 默认目标

WSL2 它使用的是 default.target,我们要将其改为 multi-user.target

执行如下命令:

systemctl set-default multi-user.target

2. 修复 ssh.service

这是 WSL2 从来没有从 ssh 连接过造成的,没有密钥,需要生成。

执行如下命令:

sudo ssh-keygen -A

3. 修复 systemd-remount-fs.service

因为根目录的标签应该是 cloudimg-rootfs,但是 WSL2 的是空的。

首先使用 df 命令确定一下根目录所在的磁盘名:

df

/dev/sdc 263174212 4253788 245482268 2% /

可以得知根目录所在的磁盘为 /dev/sdc

添加 label:

sudo e2label /dev/sdc cloudimg-rootfs

4. 修复 systemd-sysusers.service

由于该 service 配置文件中的 LoadCredential 需要内核模块 sg 和 crypto_user,而这些模块无法在 WSL 中加载。所以它会导致 systemd-sysusers.service 启动失败。LoadCredential 条目如下:

# Optionally, pick up a root password and shell for the root user from a
# credential passed to the service manager. This is useful for importing this
# data from nspawn's --set-credential= switch.
LoadCredential=passwd.hashed-password.root
LoadCredential=passwd.plaintext-password.root
LoadCredential=passwd.shell.root

使用文本编辑器例如 vim 编辑,在 LoadCredential=passwd.shell.root 后面,回车再加一行让它们失效:

LoadCredential=

5. 修改 systemd 等待启动时间

最后,编辑 /etc/genie.ini,将 systemd-timeout 值的时间调小至 20 以下:

[genie]
systemd-timeout=20 # 调小至 20
clone-env=WSL_DISTRO_NAME,WSL_INTEROP,WSLENV,DISPLAY,WAYLAND_DISPLAY,PULSE_SERVER
secure-path=/lib/systemd:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
clone-path=false
target-warning=true
update-hostname=true
update-hostname-suffix=-wsl
resolved-stub=false

三、以 systemd 启动 WSL2

接下来就可以正常以 systemd 启动了,先彻底关闭 wsl2。

例如 Ubuntu:

wsl --shudown Ubuntu

然后以 genie 启动:

wsl genie -s

正常输出:

PS C:\Users\kane> wsl genie -s
Waiting for systemd….!

相关链接:

Genie 的 GitHub地址:https://github.com/arkane-systems/genie

参考:

https://github.com/arkane-systems/genie/wiki

本文链接:https://www.htcp.net/9833.html

赞(0) 打赏
文章通过 CC 4.0 BY-NC-ND 协议发布,转载请注明出处:云梦 » 利用 genie 工具在 WSL2 中启用 systemd

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

如果您觉得文章有用,可以支持一下本站

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏