背景:
嵌入式linux通常使用busybox或者sysvinit作为init启动管理单元,我们探索嵌入式linux支持systemd。
环境:
基于项目linux-ps项目(linux-ps · GitCode),在meta-ls(自建demo层)中添加自己的内核编译bb文件。
实现:
1. 在local.conf中添加信息:
diff --git a/configs/qemuarm64/local.conf b/configs/qemuarm64/local.conf
index 48840db..b44c41d 100755
--- a/configs/qemuarm64/local.conf
+++ b/configs/qemuarm64/local.conf
@@ -273,3 +273,12 @@ PACKAGECONFIG:append:pn-qemu-system-native = " sdl"
# track the version of this file when it was generated. This can safely be ignored if
# this doesn't mean anything to you.
CONF_VERSION = "2"
+
+#使用systemd udev
+PREFERRED_PROVIDER_udev ?= "systemd"
+PREFERRED_PROVIDER_udev-utils ?= "systemd"
+#使用systemd作为init_manager
+VIRTUAL-RUNTIME_init_manager = "systemd"
+VIRTUAL-RUNTIME_initscripts = ""
+
+DISTRO_FEATURES = "systemd"
2.修改qemuarm64(额外,与systemd无关)
--- a/conf/machine/qemuarm64.conf
+++ b/conf/machine/qemuarm64.conf
@@ -14,7 +14,7 @@ SERIAL_CONSOLES_CHECK = "${SERIAL_CONSOLES}"
QB_SYSTEM_NAME = "qemu-system-aarch64"
QB_MACHINE = "-machine virt"
QB_CPU = "-cpu cortex-a57"
-QB_SMP = "-smp 4"
+QB_SMP = "-smp 8"^M
QB_CPU_KVM = "-cpu host -machine gic-version=3"
# For graphics to work we need to define the VGA device as well as the necessary USB devices
QB_GRAPHICS = "-device virtio-gpu-pci"
@@ -28,6 +28,7 @@ QB_ROOTFS_OPT = "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio
QB_SERIAL_OPT = "-device virtio-serial-device -chardev null,id=virtcon -device virtconsole,chardev=virtcon"
QB_TCPSERIAL_OPT = "-device virtio-serial-device -chardev socket,id=virtcon,port=@PORT@,host=127.0.0.1 -device virtconsole,chardev=virtcon"
+QB_MEM = "-m 1024"^M
PREFERRED_PROVIDER_virtual/kernel = "linux-kernel"
@@ -36,4 +37,4 @@ DISTRO_FEATURES:remove = " sysvinit"
VIRTUAL-RUNTIME_init_manager = "systemd"
DISTRO_FEATURES = "systemd"
-IMAGE_INSTALL += "packagegroup-core-boot packagegroup-base-extended kernel-module-hello "
+IMAGE_INSTALL += "packagegroup-core-boot packagegroup-base-extended systemd-analyze kernel-module-hello "
完成编译:
启动qemuarm64.
runqemu qemuarm64 nographic
runqemu - INFO - Running /home/samba_shar/work/linux-ps1/build/tmp/work/x86_64-linux/qemu-helper-native/1.0-r1/recipe-sysroot-native/usr/bin/qemu-system-aarch64 -device virtio-net-device,netdev=net0,mac=52:54:00:12:34:02 -netdev tap,id=net0,ifname=tap0,script=no,downscript=no -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 -drive id=disk0,file=/home/samba_shar/work/linux-ps1/build/tmp/deploy/images/qemuarm64/core-image-base-qemuarm64-20231101140625.rootfs.ext4,if=none,format=raw -device virtio-blk-device,drive=disk0 -device qemu-xhci -device usb-tablet -device usb-kbd -machine virt -cpu cortex-a57 -smp 8 -m 1024 -serial mon:stdio -serial null -nographic -device virtio-gpu-pci -kernel /home/samba_shar/work/linux-ps1/build/tmp/deploy/images/qemuarm64/Image--5.10-rc7-r0-qemuarm64-20231101133345.bin -append 'root=/dev/vda rw mem=1024M ip=192.168.7.2::192.168.7.1:255.255.255.0 console=ttyAMA0 console=hvc0 '
进入命令行:
root@qemuarm64:~# ls -l /sbin/init
lrwxrwxrwx 1 root root 22 Mar 9 2018 /sbin/init -> ../lib/systemd/systemd
确认已转为systemd方式启动,查看信息。
root@qemuarm64:~# systemd-analyze blame
2.888s dev-vda.device
2.404s systemd-udev-trigger.service
1.607s systemd-timesyncd.service
1.481s systemd-networkd.service
1.243s systemd-resolved.service
1.164s systemd-journald.service
1.023s modprobe@drm.service
992ms dev-mqueue.mount
987ms sys-kernel-debug.mount
986ms modprobe@fuse.service
965ms sys-kernel-tracing.mount
959ms tmp.mount
932ms modprobe@configfs.service
876ms systemd-sysctl.service
782ms systemd-remount-fs.service
759ms systemd-logind.service
559ms psplash-start.service
499ms systemd-udevd.service
491ms systemd-tmpfiles-setup.service
311ms systemd-update-utmp.service
291ms systemd-journal-flush.service
284ms systemd-tmpfiles-setup-dev.service
276ms systemd-random-seed.service
176ms var-volatile.mount
125ms systemd-update-utmp-runlevel.service