2024年7月5日 星期五

Configuration of a minimal systemd setup in QEMU


本文記錄如何在 QEMU 環境中設定最小的 systemd init。基本上需要建立兩個必要的目標:sysinit.target 和 basic.target,以及用於登入的 getty@.service。
SRCROOT=/opt/armv7vet2hf/sysroots/
DSTROOT=initrd
SRCLIB=$SRCROOT/lib
DSTLIB=$DSTROOT/lib
SRCSYSDLIB=$SRCROOT/lib/systemd
DSTSYSDLIB=$DSTROOT/lib/systemd
SRCUSRLIB=$SRCROOT/usr/lib
DSTUSRLIB=$DSTROOT/usr/lib

rm -rf $DSTROOT
mkdir -p $DSTROOT/bin $DSTROOT/usr/lib $DSTROOT/lib/systemd/system $DSTROOT/etc/systemd/system

# copy libraries
cp -a $SRCLIB/libselinux.so* $DSTLIB
cp -a $SRCLIB/libmount.so* $DSTLIB
cp -a $SRCLIB/libaudit.so* $DSTLIB
cp -a $SRCLIB/libc.so* $DSTLIB
cp -a $SRCLIB/ld-linux-armhf.so* $DSTLIB
cp -a $SRCLIB/libblkid.so* $DSTLIB
cp -a $SRCLIB/libcap.so* $DSTLIB
cp -a $SRCLIB/libm.so* $DSTLIB
cp -a $SRCLIB/libpcre.so* $DSTLIB
cp -a $SRCLIB/libcap-ng.so* $DSTLIB

cp -a $SRCUSRLIB/libacl.so* $DSTUSRLIB
cp -a $SRCUSRLIB/libcrypt.so* $DSTUSRLIB
cp -a $SRCUSRLIB/liblzma.so* $DSTUSRLIB
cp -a $SRCUSRLIB/libattr.so* $DSTUSRLIB

cp -a $SRCSYSDLIB/systemd $DSTSYSDLIB
cp -a $SRCSYSDLIB/libsystemd-shared* $DSTSYSDLIB
install -m 555 busybox-build/busybox $DSTROOT/bin/


# Create basic.target
cat << EOF > $DSTSYSDLIB/system/basic.target
[Unit]
Description=Basic System
EOF

# Create sysinit.target
cat << EOF > $DSTSYSDLIB/system/sysinit.target
[Unit]
Description=System Initialization
DefaultDependencies=no
EOF

# Create getty@tty1.service
cat << EOF > $DSTSYSDLIB/system/getty@.service
[Unit]
Description=Getty on %I
ConditionPathExists=/dev/%I

[Service]
ExecStart=-/sbin/getty 115200 %I
Restart=always

[Install]
WantedBy=basic.target rescue.target
EOF


chmod 644 -R $DSTSYSDLIB/system/*

cat << EOF > $DSTROOT/init
#!/bin/busybox sh
## Mount essential filesystems
/bin/busybox mkdir -p /proc /sys /dev /home /run/systemd/journal /tmp /var /mnt /sbin /usr/bin /usr/sbin /etc/systemd/system/basic.target.wants /etc/systemd/system/rescue.target.wants /etc/systemd/system/default.target.wants
/bin/busybox --install -s
ln -sf /lib/systemd/systemd /sbin/init

## Set the path for BusyBox applets if using BusyBox
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mkdir -p /dev/pts
mount -t devpts none /dev/pts
## Ensure the getty@ttyAMA0.service file is linked in the correct place
ln -sf /lib/systemd/system/getty@.service /etc/systemd/system/basic.target.wants/getty@ttyAMA0.service
# create default password
echo "root:vnpTT1BZdW1/s:0:0:root:/root:/bin/sh" > /etc/passwd
echo "root:x:0:" > /etc/group

# Ensure systemd can find its units
ln -sf /lib/systemd/system/basic.target /lib/systemd/system/default.target

# Start systemd
exec /sbin/init
EOF
chmod +x $DSTROOT/init

fakeroot bash -c "cd linux && ./usr/gen_initramfs.sh ../$DSTROOT -o ../initrd-arm.img"



沒有留言:

張貼留言

熱門文章