我创建了systemd
脚本来启动我Ubuntu 18.04
机器上的 CAN 接口。Systemd 脚本如下:
[Unit]
Description=Serial CAN adapter for %I device
[Service]
User=root
ExecStart=/usr/local/bin/can_ACM.sh %i
Type=oneshot
[Install]
WantedBy=multi-user.target
~
脚本can_ACM.sh
如下:
#!/bin/sh
DEV="can0"
TTY=$1
BAUDRATE=1000000
slcand -s6 -S${BAUDRATE} "/dev/${TTY}" "${DEV}"
sleep 1
ip link set up ${DEV}
我使用它NetworkManager
作为渲染器netplan
并创建了/etc/network/interfaces
如下配置:
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
allow-hotplug can0
iface can0 can static
pre-up /sbin/ip link set $IFACE type can bitrate 1000000
up /sbin/ip link set up $IFACE
down /sbin/ip link set down $IFACE
当我can_ACM.sh
以 root 身份手动运行脚本时,CAN 网络成功启动,但当我尝试通过 启动它时systemd
,NetworkManager
会删除can0
设备。日志来自journalctl
:
gru 09 10:17:33 systemd[1]: Starting Serial CAN adapter for ttyACM0 device...
gru 09 10:17:33 slcand[5436]: starting on TTY device /dev/ttyACM0
gru 09 10:17:33 slcand[5437]: attached TTY /dev/ttyACM0 to netdevice slcan0
gru 09 10:17:33 kernel: can0: renamed from slcan0
gru 09 10:17:33 slcand[5437]: netdevice slcan0 renamed to can0
gru 09 10:17:33 NetworkManager[3901]: <info> [1575883053.8788] manager: (can0): new Generic device (/org/freedesktop/NetworkManager/Devices/5)
gru 09 10:17:33 systemd-udevd[5440]: link_config: autonegotiation is unset or enabled, the speed and duplex are not writable.
gru 09 10:17:33 systemd-udevd[5440]: link_config: could not get ethtool features for slcan0
gru 09 10:17:33 systemd-udevd[5440]: Could not set offload features of slcan0: No such device
gru 09 10:17:33 NetworkManager[3901]: <info> [1575883053.9118] devices added (path: /sys/devices/virtual/net/slcan0, iface: slcan0)
gru 09 10:17:33 NetworkManager[3901]: <info> [1575883053.9119] device added (path: /sys/devices/virtual/net/slcan0, iface: slcan0): no ifupdown configuration found.
gru 09 10:17:34 NetworkManager[3901]: <info> [1575883054.8523] devices removed (path: /sys/devices/virtual/net/can0, iface: can0)
gru 09 10:17:34 systemd[1]: Started Serial CAN adapter for ttyACM0 device.
手动通过runninc脚本启动网络成功后,日志如下:
gru 09 10:24:56 slcand[8234]: starting on TTY device /dev/ttyACM0
gru 09 10:24:56 slcand[8235]: attached TTY /dev/ttyACM0 to netdevice slcan0
gru 09 10:24:56 kernel: can0: renamed from slcan0
gru 09 10:24:56 slcand[8235]: netdevice slcan0 renamed to can0
gru 09 10:24:56 systemd-udevd[8237]: link_config: autonegotiation is unset or enabled, the speed and duplex are not writable.
gru 09 10:24:56 NetworkManager[3901]: <info> [1575883496.7958] manager: (can0): new Generic device (/org/freedesktop/NetworkManager/Devices/8)
gru 09 10:24:56 systemd-udevd[8237]: link_config: could not get ethtool features for slcan0
gru 09 10:24:56 systemd-udevd[8237]: Could not set offload features of slcan0: No such device
gru 09 10:24:56 NetworkManager[3901]: <info> [1575883496.8226] devices added (path: /sys/devices/virtual/net/slcan0, iface: slcan0)
gru 09 10:24:56 NetworkManager[3901]: <info> [1575883496.8226] device added (path: /sys/devices/virtual/net/slcan0, iface: slcan0): no ifupdown configuration found.
为什么NetworkManager
当 systemd 触发创建接口时会删除接口以及如何修复它?
答案1
问题是由服务引起的systemd
:oneshot
脚本完成后服务停用,并NetworkManager
自动从死systemd
服务中删除接口。RemainAfterExit=yes
向服务添加选项解决了问题。