genie-报错处理

上一级页面:index-wsl

前言

genie有许多模块需要处理,以此按下列列表进行处理

解决宿主主机ip解析错误

1
genie: WARNING: systemd is in degraded state, issues may occur!

有时genie会以低等级的状态启动,解决方法见:

Systemd units known to be problematic under WSL · arkane-systems/genie Wiki (github.com)

进入root用户模式

1
sudo -s

输入下列命令

1
2
3
4
5
6
7
8
9
10
11
12
13
cat << EOF > /etc/systemd/network/10-eth0.network
[Match]
Name=eth0

[Link]
Unmanaged=yes

[Network]
DHCP=no

[DHCP]
UseDNS=false
EOF

退出root用户

1
exit

apparmor.service

即使使用支持 AppArmor 的内核,该服务也会运行,但会退出并显示消息“不在容器中启动 AppArmor”。

这按预期工作,因为 WSL 在技术上是一个容器,但使系统不安全。要使 AppArmor 在 WSL 2 下正常运行,需要一个 AppArmor 补丁。

有关更多详细信息和修补程序,请参阅此文章:

https://randombytes.substack.com/p/apparmor-on-wsl-2

具体来说,解决方法是手动打一个补丁:

1
sudo vim /lib/apparmor/rc.apparmor.functions

找到第91行(91,6 +91,11)。可以在vim的normal模式下,输入命令:set nu来设置行号

如图所示:

Pasted%20image%2020220707174556.png

在下面代码here处,添加一段代码

1
2
3
4
5
6
7
8
9
10
11
# Returns 0 if the container environment is capable of having its own internal
# policy and non-zero otherwise.

local ns_stacked
local ns_name

# ============================here=============================

if ! [ -f "$ns_stacked_path" ] || ! [ -f "$ns_name_path" ]; then
return 1
fi

注意下面代码中的+号。这里就是添加的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 # Returns 0 if the container environment is capable of having its own internal
# policy and non-zero otherwise.
local ns_stacked
local ns_name

+ # WSL needs to be detected explicitly
+ if [ $(systemd-detect-virt --container) = "wsl" ]; then
+ return 0
+ fi
+

if ! [ -f "$ns_stacked_path" ] || ! [ -f "$ns_name_path" ]; then
return 1
fi

添加的代码如下:

1
2
3
4
5
# WSL needs to be detected explicitly
if [ $(systemd-detect-virt --container) = "wsl" ]; then
return 0
fi

在每次更新ubuntu版本后都需要重复操作

解决/proc/sys/fs/binfmt_misc报错

尝试访问 Ubuntu 下瓶子内的binfmts_misc可能会产生以下错误:

1
2
ls /proc/sys/fs/binfmt_misc
ls: cannot open directory '/proc/sys/fs/binfmt_misc': Too many levels of symbolic links

在Arch等发行版中,它的表现形式可能是这样的

1
systemctl list-units --state failed

输出

1
2
3
  UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
● systemd-binfmt.service loaded failed failed Set Up Additional Binary Formats

这似乎是binfmt_misc无法正确挂载,

或者当systemd在Ubuntu下启动时,挂载会以某种方式中断。

有一个解决方法。只需使用以下命令在现有装载的顶部重新安装binfmt_misc:

1
sudo mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc

如果上面的命令有效解决问题,您可以通过添加以下内容到 /etc/rc.local来自动执行:

ubuntu 18.04系统默认已经将/etc/rc.local文件移除了,因此,我们需要手动创建一个,

1
sudo vim /etc/rc.local

注意文件头部有一行#!/bin/bash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# 上面这行放在在文件头,没有的自己加
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
#

# 尾部添加
ls /proc/sys/fs/binfmt_misc > /dev/null 2>&1 || \
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc

/etc/rc.local加上可执行的权限

1
sudo chmod a+x /etc/rc.local

然后执行

1
sudo systemctl enable rc-local
1
2
xugaoxiang@ubuntu:~$ sudo systemctl enable rc-local
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /etc/systemd/system/rc-local.service.

接着启动这个服务并查看它的状态

1
2
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

命令输出如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
└─debian.conf
Active: active (running) since Thu 2018-11-01 13:17:08 CST; 2s ago
Docs: man:systemd-rc-local-generator(8)
Process: 10810 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/rc-local.service
└─10811 /usr/bin/python /usr/bin/sslocal -c /home/xugaoxiang/Tools/ss/ss.json

11 01 13:17:08 ubuntu systemd[1]: Starting /etc/rc.local Compatibility...
11 01 13:17:08 ubuntu systemd[1]: Started /etc/rc.local Compatibility.
11 01 13:17:08 ubuntu rc.local[10810]: INFO: loading config from /home/xugaoxiang/Tools/ss/ss.json
11 01 13:17:08 ubuntu rc.local[10810]: 2018-11-01 13:17:08 INFO loading libcrypto from libcrypto.so.1.1
11 01 13:17:08 ubuntu rc.local[10810]: 2018-11-01 13:17:08 INFO starting local at 127.0.0.1:1080

可以看到rc.local中的脚本已经被正确执行了。

然后编辑systemd-binfmt.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
sudo systemctl edit systemd-binfmt.service
````

在下图所示的位置

![图片](https://user-images.githubusercontent.com/45024266/175800394-cd6e0009-9ef0-4105-b874-6de407d442af.png)

填写以下内容,先启动`rc-local.service`,再启动`systemd-binfmt.service`

```shell
[Unit]
After=rc-local.service
````

然后按`ctrl+x`会出现保存相关的提示,

然后按`Enter`成功保存配置

重启 WSL

````
sudo systemctl reboot
````

成功

见[无法访问 Ubuntu 下binfmt_misc的 proc sys fs ·阿卡内系统/精灵维基 (github.com)](https://github.com/arkane-systems/genie/wiki/Cannot-access--proc-sys-fs-binfmt_misc-under-Ubuntu)

# 查看失败的service列表

```shell
systemctl list-units --state failed

必须处理的列表

在WSL下已知有问题的系统单元 ·arkane-systems/genie Wiki (github.com)

下面这些服务,在wsl2的内核中全部都不支持,除非你使用自己编译的内核

1
2
3
4
5
6
sudo systemctl disable auditd.service
sudo systemctl disable systemd-modules-load.service
sudo systemctl disable multipathd.service

sudo touch /etc/cloud/cloud-init.disabled

下面这些服务,容易引起systemd的降级运行

1
sudo systemctl mask fwupd-refresh.service

修复open-ssh-server

1
sudo ssh-keygen -A

修复systemd-sysusers.service

1
sudo systemctl edit systemd-sysusers.service

and add the lines:

1
2
[Service]
LoadCredential=

avahi-daemon.service

Avahi(avahi守护程序)相关于免费实现Apple的“ ZeroConf”程序,并使用mDNS / DNS-SD protocol套件进行服务发现。

并在网络中扫描打印机和其他共享资源。

它还可以提供DNS和DHCP服务。 这对于笔记本电脑非常有用,但在服务器上并不需要,也可能导致网络性能下降,并在重负载下变得不稳定。

用于零配置网络发现,使电脑超容易发现网络中打印机或其他的主机

1
sudo systemctl disable avahi-daemon.service

mount: /: can’t find LABEL=cloudimg-rootfs.

失败,并出现以下错误:

1
mount: /: can't find LABEL=cloudimg-rootfs.

这个稍微麻烦一点,见在WSL下已知有问题的系统单元 ·arkane-systems/genie Wiki (github.com)

发生这种情况是因为 systemd-remount-fs 正在尝试重新挂载根分区,以确保它具有在 /etc/fstab 中配置的选项;

Ubuntu 默认的 /etc/fstab 假定根分区被标记为“cloudimg-rootfs”。

但在 WSL 下不是这种情况(根分区是其 .vhdx 文件之一),因此重新装载失败。

(严格来说,这不是服务的问题 - 如果您尝试重新装载/手动,则会收到相同的错误。)

有两种方法可以解决这个问题:

方法一:

找到从中挂载根分区的设备(一般情况可能是/dev/sdb,但你必须先检查一下,确认路径是什么),

1
cat /etc/fstab

确定路径是什么,我的输出是/dev/sdb,然后适当地标记它:

1
sudo e2label /dev/sdb cloudimg-rootfs

方法二:

或者,如果您对根文件系统的现有挂载选项感到满意,只需从 /etc/fstab 中完全删除 / 的行即可。

/etc/fstab是用来存放文件系统的静态信息的文件。 位于/etc/目录下,可以用命令less /etc/fstab 来查看

1
less /etc/fstab

先进入root用户模式

1
sudo -s

清空/etc/fstab

1
sudo echo '' > /etc/fstab

退出root用户模式

1
exit

WSL在启动发行版时会自动为您安装它并提供合理的选项,因此除非您知道您需要一些不同的东西,否则实际上没有任何意义。

openssh-server可能出现这样的错误

Dbus错误

1
2
3
4
5
6
7
8
Warning! D-Bus connection terminated.
Failed to restart ssh.service: 连接被对方重设
See system logs and 'systemctl status ssh.service' for details.
invoke-rc.d: initscript ssh, action "restart" failed.
Failed to get properties: 连接被对方重设
在处理时有错误发生:
openssh-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

Systemd units known to be problematic under WSL · arkane-systems/genie Wiki (github.com)

这个报错是dbus没有正常运行

解决方案:

首先正确安装systemctl:wsl2开启systemctl命令简单方法

然后正确配置dbus:配置dbus和dbus-daemon

sshd: no hostkeys available – exiting.

这个报错

1
sshd: no hostkeys available -- exiting.

修复方案

1
sudo ssh-keygen -A

依赖缺失

尝试修复依赖缺失:

  1. 先退出genie

    1
    genie -u
  2. 修复依赖

    1
    2
    sudo apt update --fix-missing
    sudo apt autoremove && sudo apt clean && sudo apt install -f

22端口占用

ssh端口已经被监听

iscsid.service loaded failed failed

报错信息如下

1
2
3
4
sudo systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
iscsid.service loaded failed failed iSCSI initiator daemon (iscsid)
iscsid.socket loaded failed failed Open-iSCSI iscsid Socket

什么是iscsi服务呢?见iSCSI的基本架构及操作简介 - 知乎 (zhihu.com)

排查原因

1
systemctl status iscsid.service
1
2
3
4
5
6
7
× iscsid.service - iSCSI initiator daemon (iscsid)
Loaded: loaded (/lib/systemd/system/iscsid.service; enabled; vendor preset: enabled)
Active: failed (Result: timeout) since Mon 2022-07-04 20:50:24 CST; 25min ago
TriggeredBy: iscsid.socket
Docs: man:iscsid(8)

Warning: some journal files were not opened due to insufficient permissions.

继续排查

1
journalctl -xe

这个错误很可能是由于丢失iscsi驱动所导致的。(The issue was caused by missing iscsi driver.)

查询资料:open-iscsi installation failed on TX1/TX2 Jetpack3.0/3.1 – ubuntu 16.04 - Jetson & Embedded Systems / Jetson TX2 - NVIDIA Developer Forums

得到原因如下:

首先应当确认安装open-iscsi,这是一个默认安装的包

1
sudo apt install  open-iscsi

安装 open-iscsi 会导致在 Ubuntu 上同时安装客户端和服务器端 ,

在安装时,客户端和服务器端都不会自动配置并且,如果您未正确配置它们,服务iscsid.service将无法正常启动

客户端和服务器端的供应商预设值都是enable,但很明显,这是一个错误的默认值,因为新安装open-iscsi时不会自动配置,在没有配置的情况下,要求iscsid.service开机自启只会报错。

  • 客户端:iscsi.service
  • 服务器端:iscsid.service

iSCSI 目标提供服务的为服务器端 (iscsid.service)

1
sudo systemctl status iscsid.service

请求/挂载 iSCSI 目标的为客户端(iscsi.service)

1
sudo systemctl status iscsi.service

解决方案如下:

如果您工作在客户端,你只需要 iscsi.service

(你不会想要 iscsid.service,因为 iscsid.service是服务器端,它对你来说无用)

既然 iscsid.service对您来说是无用的,您可能希望避免每次启动时出现 iscsid.service 错误,因此:

1
2
3
sudo systemctl disable iscsid.service iscsid.socket
# Verify: 是否成功关闭
sudo systemctl status iscsid.service | egrep disabled

输出如下

1
Loaded: loaded (/lib/systemd/system/iscsid.service; disabled; vendor preset: enabled)

如果您工作在服务器端,并且您有使用服务器端 iscsid.service 的工作需求,那么请正确的配置并启用iscsid.service,适用于 iSCSI 的 Ubuntu 服务器指南如下:

1
https://help.ubuntu.com/lts/serverguide/iscsi-initiator.html

在genie中找不到VScode的命令“code”?这是一个解决方案

如果类似或zsh: command not found: code

1
2
3
Command 'code' not found, but can be installed with:

sudo snap install code

在genie中出现报错,而在瓶子外面,该code命令完美地打开VScode,

然后您可以编辑.zprofile``.profile等,填加如下

1
2
3
code() {
/mnt/c/Users/<YOUR USERNAME>/AppData/Local/Programs/Microsoft\ VS\ Code/bin/code "$@"
}

<YOUR USERNAME>替换为您拥有的任何用户名。

VScode在不同设备之间可能有不同的路径,所以自己确定一下路径,在此之后,VScode应该没有问题!

参考、引用、致谢