背景:
systemd-network支持vlan。
环境:
基于项目linux-ps项目(linux-ps · GitCode),在meta-ls(自建demo层)中添加自己的内核编译bb文件。
参考资料:
systemd手册。
为了支持systemd-network,需要再嵌入式环境支持systemd,见文章:
https://blog.csdn.net/xiayutian747/article/details/134171466?spm=1001.2014.3001.5502
随后编写network unit。
一,原生eth0的network文件(eth0.network):
设定为DHCP模式,支持vlan名称:eth0.4
[Match]
Name=eth0
[Network]
Description="eth0"
#Address=192.168.7.2/24
#Gateway=192.168.7.1
#DNS=8.8.8.8
DHCP=ipv4
VLAN=eth0.4
[Link]
MACAddress=00:0c:29:06:76:f8
[DHCP]
CriticalConnection=true
二、创建vlan设备(vlan.netdev):
[NetDev]
Name=eth0.4
Kind=vlan
[VLAN]
Id=123
三、vlan配置(vlan.network):
[Match]
Name=eth0.4
Type=vlan
[Network]
Description=The interface for eth0.4
# Very simple static IPv4-only address configuration.
# Well, an IPv6-link-local address as well, by default.
[Address]
Address=10.1.2.3/24
四、修改内核编译配置,支持vlan:
五,将network配置安装到systemd
diff --git a/recipes-core/systemd/systemd_249.7.bbappend b/recipes-core/systemd/systemd_249.7.bbappend
index 96866ba..4aa0d70 100644
--- a/recipes-core/systemd/systemd_249.7.bbappend
+++ b/recipes-core/systemd/systemd_249.7.bbappend
@@ -3,11 +3,25 @@ FILESEXTRAPATHS:prepend := "${THISDIR}/files:"
SRC_URI += " \
file://0008-show-status-full-info.patch \
- "
+ file://eth0.network \^M
+ file://vlan.network \^M
+ file://vlan.netdev \^M
+ "^M
inherit externalsrc
METASEMI_DIR = "${THISDIR}/../../../../"
EXTERNALSRC = "${METASEMI_DIR}/source/systemd/"
FILESEXTRAPATHS:append := "${THISDIR}/files:"
-SRC_URI:append = "file://80-drivers-rules.patch"
\ No newline at end of file
+SRC_URI:append = "file://80-drivers-rules.patch"^M
+^M
+FILES_${PN} += "${sysconfdir}/systemd/network/*"^M
+^M
+USERADD_PARAM_${PN} += "; --system systemd-network "^M
+^M
+do_install:append() {^M
+ install -d ${D}${sysconfdir}/systemd/network/^M
+ install -m 0644 ${WORKDIR}/eth0.network ${D}${sysconfdir}/systemd/network/^M
+ install -m 0644 ${WORKDIR}/vlan.network ${D}${sysconfdir}/systemd/network/^M
+ install -m 0644 ${WORKDIR}/vlan.netdev ${D}${sysconfdir}/systemd/network/^M
+}
\ No newline at end of file
六、重新编译内核,重新编译core-image-base
七、启动qemu:
root@qemuarm64:~# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:06:76:F8
inet addr:192.168.7.2 Bcast:192.168.7.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:45 errors:0 dropped:0 overruns:0 frame:0
TX packets:52 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8042 (7.8 KiB) TX bytes:5391 (5.2 KiB)
eth0.4 Link encap:Ethernet HWaddr 00:0C:29:06:76:F8
inet addr:10.1.2.3 Bcast:10.1.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:1438 (1.4 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:80 errors:0 dropped:0 overruns:0 frame:0
TX packets:80 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6080 (5.9 KiB) TX bytes:6080 (5.9 KiB)
root@qemuarm64:~# ls -l /etc/systemd/network
-rw-r--r-- 1 root root 197 Mar 9 2018 eth0.network
-rw-r--r-- 1 root root 46 Mar 9 2018 vlan.netdev
-rw-r--r-- 1 root root 219 Mar 9 2018 vlan.network
root@qemuarm64:~#
日志:
root@qemuarm64:~# journalctl -u systemd-networkd
-- Journal begins at Wed 2023-11-01 15:02:07 UTC, ends at Wed 2023-11-01 15:13:55 UTC. --
Nov 01 15:02:09 qemuarm64 systemd[1]: Starting Network Configuration...
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: eth0: Link UP
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: eth0: Gained carrier
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: lo: Link UP
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: lo: Gained carrier
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: eth0: Gained IPv6LL
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: Enumeration completed
Nov 01 15:02:11 qemuarm64 systemd[1]: Started Network Configuration.
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: eth0: DHCPv4 connection considered critical, ignoring request to reconfigure it.
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: eth0.4: netdev ready
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: eth0.4: Link UP
Nov 01 15:02:11 qemuarm64 systemd-networkd[187]: eth0.4: Gained carrier
Nov 01 15:02:13 qemuarm64 systemd-networkd[187]: eth0.4: Gained IPv6LL
root@qemuarm64:~#
完成配置。