From 8e2a17b78fe0da38ad3a65c6c5f193cc7f503441 Mon Sep 17 00:00:00 2001 From: Wiktor Ogrodnik Date: Thu, 4 May 2023 19:58:45 +0200 Subject: [PATCH] Separates initramfs and rootfs (#26) - adds two phases of compilation: first initramfs + kernel + bootloader, second: rootfs + tools (python, git, pip, ...) - speeds up Linux boot time to a few seconds - more flexible approach: adding new tools to rootfs will not significantly increase boot time --- action/hifive.resc | 9 ++- action/run-in-renode.py | 34 ++++++----- .../configs/hifive_unleashed_defconfig | 30 +--------- .../package/busybox/busybox.config.fragment | 48 +++++++++++++++ ...s-second-virtio-device-for-user-data.patch | 38 ++++++++++++ br2-external/rootfs/Config.in | 0 .../hifive-unleashed/genimage_sdcard.cfg | 11 ++++ .../sifive/hifive-unleashed/post-image.sh | 18 ++++++ .../rootfs/configs/hifive_unleashed_defconfig | 58 +++++++++++++++++++ br2-external/rootfs/external.desc | 2 + br2-external/rootfs/external.mk | 0 scripts/compile.sh | 31 ++++++++-- 12 files changed, 229 insertions(+), 50 deletions(-) create mode 100644 br2-external/package/busybox/busybox.config.fragment create mode 100644 br2-external/patches/linux/0003-adds-second-virtio-device-for-user-data.patch create mode 100644 br2-external/rootfs/Config.in create mode 100644 br2-external/rootfs/board/sifive/hifive-unleashed/genimage_sdcard.cfg create mode 100755 br2-external/rootfs/board/sifive/hifive-unleashed/post-image.sh create mode 100644 br2-external/rootfs/configs/hifive_unleashed_defconfig create mode 100644 br2-external/rootfs/external.desc create mode 100644 br2-external/rootfs/external.mk diff --git a/action/hifive.resc b/action/hifive.resc index f4d26de..20eac7b 100644 --- a/action/hifive.resc +++ b/action/hifive.resc @@ -7,8 +7,13 @@ using sysbus mach create $name machine LoadPlatformDescription @action/sifive-fu540.repl +# rootfs machine LoadPlatformDescriptionFromString 'virtio: Storage.VirtIOBlockDevice @ sysbus 0x100d0000 { IRQ -> plic@50 }' -virtio LoadImage @drive.img +virtio LoadImage @images/rootfs.img + +# user data +machine LoadPlatformDescriptionFromString 'virtioUser: Storage.VirtIOBlockDevice @ sysbus 0x100d0200 { IRQ -> plic@49 }' +virtioUser LoadImage @drive.img connector Connect sysbus.ethernet switch0 @@ -26,7 +31,7 @@ $rootfs ?= @images/rootfs.cpio macro reset """ sysbus LoadELF $elf - sysbus LoadFdt $fdt 0x88000000 "earlycon=sbi console=ttyS0 root=/dev/ram init=/sbin/init initrd=0x8d000000,120M" + sysbus LoadFdt $fdt 0x88000000 "earlycon=sbi console=ttyS0 root=/dev/ram init=/sbin/init initrd=0x8d000000,28M" sysbus LoadBinary $rootfs 0x8D000000 sysbus LoadBinary $image 0x8B000000 """ diff --git a/action/run-in-renode.py b/action/run-in-renode.py index 2c05cb7..e87362c 100755 --- a/action/run-in-renode.py +++ b/action/run-in-renode.py @@ -496,13 +496,31 @@ def setup_renode(): for command in device.add_commands: run_cmd(child, "#", command) - # Extracting files from Virtio + # Set time + + now = datetime.now() + + run_cmd(child, "#", f'date -s "{now.strftime("%Y-%m-%d %H:%M:%S")}"') + + # Preparing rootfs run_cmd(child, "#", "mount /dev/vda /mnt") run_cmd(child, "#", "cd /mnt") + + run_cmd(child, "#", "mount -t proc /proc proc/") + run_cmd(child, "#", "mount -t sysfs /sys sys/") + run_cmd(child, "#", "mount -o bind /dev dev/") + run_cmd(child, "#", "mount -o bind /run run/") + run_cmd(child, "#", "chroot /mnt /bin/sh") + run_cmd(child, "#", "mkdir -p /sys/kernel/debug") run_cmd(child, "#", "mount -t debugfs nodev /sys/kernel/debug") + # Extracting user data + + run_cmd(child, "#", "mount /dev/vdb /root") + run_cmd(child, "#", "cd /root") + # Network configuration # This configuration allows simulated linux to connect to # the tap0 interface created in the host @@ -511,20 +529,6 @@ def setup_renode(): run_cmd(child, "#", "ip route add default via 172.16.0.1") run_cmd(child, "#", 'echo "nameserver 1.1.1.1" >> /etc/resolv.conf') # adds dns server address - now = datetime.now() - - run_cmd(child, "#", f'date -s "{now.strftime("%Y-%m-%d %H:%M:%S")}"') - - # increase git buffers - # mitigates issues with broken pipe `Send failure: Broken pipe` - run_cmd(child, "#", 'git config --global http.maxRequestBuffer 240M') - run_cmd(child, "#", 'git config --global http.postBuffer 100M') - run_cmd(child, "#", 'git config --global core.compression 0') - - run_cmd(child, "#", 'git config --global --unset https.proxy') - run_cmd(child, "#", 'git config --global --unset http.proxy') - run_cmd(child, "#", 'git config --global ssl.Verify false') - child.expect_exact("#") except px_TIMEOUT: print("Timeout!") diff --git a/br2-external/configs/hifive_unleashed_defconfig b/br2-external/configs/hifive_unleashed_defconfig index e22f0fb..d3c6ea7 100644 --- a/br2-external/configs/hifive_unleashed_defconfig +++ b/br2-external/configs/hifive_unleashed_defconfig @@ -1,7 +1,6 @@ # CCACHE BR2_CCACHE=y -BR2_CCACHE_DIR=/buildroot-ccache # Architecture BR2_riscv=y @@ -17,15 +16,9 @@ BR2_RISCV_ABI_LP64D=y BR2_SYSTEM_DHCP="eth0" BR2_TARGET_GENERIC_GETTY=y BR2_TARGET_GENERIC_GETTY_PORT="ttySIF0" -BR2_ROOTFS_POST_BUILD_SCRIPT="board/sifive/hifive-unleashed/post-build.sh" -BR2_ROOTFS_POST_IMAGE_SCRIPT="support/scripts/genimage.sh" -BR2_ROOTFS_POST_SCRIPT_ARGS="-c board/sifive/hifive-unleashed/genimage_sdcard.cfg" # Filesystem BR2_TARGET_ROOTFS_CPIO=y -BR2_TARGET_ROOTFS_EXT2=y -BR2_TARGET_ROOTFS_EXT2_4=y -BR2_TARGET_ROOTFS_EXT2_SIZE="120M" # Linux headers same as kernel, a 5.10 series BR2_PACKAGE_HOST_LINUX_HEADERS_CUSTOM_5_10=y @@ -66,25 +59,4 @@ BR2_PACKAGE_HOST_UBOOT_TOOLS_BOOT_SCRIPT_SOURCE="board/sifive/hifive-unleashed/b BR2_GLOBAL_PATCH_DIR="${BR2_EXTERNAL_SIFIVE_RENODE_CI_PATH}/patches" # Packages - -# video4linux packages -BR2_PACKAGE_LIBV4L=y -BR2_PACKAGE_LIBV4L_UTILS=y - -# python -BR2_PACKAGE_PYTHON3=y -BR2_PACKAGE_PYTHON_PIP=y - -# git -BR2_PACKAGE_GIT=y -BR2_PACKAGE_GIT_CRYPT=y -BR2_PACKAGE_OPENSSL=y -BR2_PACKAGE_LIBCURL=y -BR2_PACKAGE_CA_CERTIFICATES=y - -# gpio packages -BR2_PACKAGE_LIBGPIOD=y -BR2_PACKAGE_LIBGPIOD_TOOLS=y - -# i2c packages -BR2_PACKAGE_I2C_TOOLS=y +BR2_PACKAGE_BUSYBOX_CONFIG_FRAGMENT_FILES="${BR2_EXTERNAL_SIFIVE_RENODE_CI_PATH}/package/busybox/busybox.config.fragment" diff --git a/br2-external/package/busybox/busybox.config.fragment b/br2-external/package/busybox/busybox.config.fragment new file mode 100644 index 0000000..89a8e97 --- /dev/null +++ b/br2-external/package/busybox/busybox.config.fragment @@ -0,0 +1,48 @@ +CONFIG_LINUXRC=n +CONFIG_FEATURE_INIT_QUIET=n + +# disable unnecessary modules in initramfs environment + +# editors + +CONFIG_AWK=n +CONFIG_CMP=n +CONFIG_DIFF=n +CONFIG_PATCH=n +CONFIG_SED=n +CONFIG_VI=n + +# finding utilities +CONFIG_FIND=n +CONFIG_GREP=n +CONFIG_EGREP=n +CONFIG_FGREP=n +CONFIG_XARGS=n + +# user management + +CONFIG_ADDGROUP=n +CONFIG_ADDUSER=n +CONFIG_DELUSER=n +CONFIG_DELGROUP=n + +# process management + +CONFIG_FREE=n +CONFIG_FUSER=n +CONFIG_KILL=n +CONFIG_KILLALL=n +CONFIG_KILLALL5=n +CONFIG_LSOF=n +CONFIG_PIDOF=n +CONFIG_PS=n +CONFIG_BB_SYSCTL=n +CONFIG_TOP=n +CONFIG_UPTIME=n +CONFIG_WATCH=n + +# networking + +CONFIG_FEATURE_IPV6=n +CONFIG_ARP=n +CONFIG_IFCONFIG=n diff --git a/br2-external/patches/linux/0003-adds-second-virtio-device-for-user-data.patch b/br2-external/patches/linux/0003-adds-second-virtio-device-for-user-data.patch new file mode 100644 index 0000000..24f11af --- /dev/null +++ b/br2-external/patches/linux/0003-adds-second-virtio-device-for-user-data.patch @@ -0,0 +1,38 @@ +From 502c5511e5f214da0d12862022f808929ca42a10 Mon Sep 17 00:00:00 2001 +From: Wiktor Ogrodnik +Date: Thu, 27 Apr 2023 14:54:46 +0200 +Subject: [PATCH 3/3] adds second virtio device for user data + +--- + arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts | 11 +++++++++-- + 1 file changed, 9 insertions(+), 2 deletions(-) + +diff --git a/arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts b/arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts +index 15e528335102..295c70ba0296 100644 +--- a/arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts ++++ b/arch/riscv/boot/dts/sifive/hifive-unleashed-a00.dts +@@ -29,12 +29,19 @@ memory@80000000 { + }; + + soc { +- virtio@100d0000 { ++ virtio@100d0000 { + compatible = "virtio,mmio"; + reg = <0x00 0x100d0000 0x00 0x150>; + interrupt-parent = <&plic0>; + interrupts = <50>; +- }; ++ }; ++ ++ virtio@100d0200 { ++ compatible = "virtio,mmio"; ++ reg = <0x00 0x100d0200 0x00 0x150>; ++ interrupt-parent = <&plic0>; ++ interrupts = <49>; ++ }; + }; + + hfclk: hfclk { +-- +2.30.2 + diff --git a/br2-external/rootfs/Config.in b/br2-external/rootfs/Config.in new file mode 100644 index 0000000..e69de29 diff --git a/br2-external/rootfs/board/sifive/hifive-unleashed/genimage_sdcard.cfg b/br2-external/rootfs/board/sifive/hifive-unleashed/genimage_sdcard.cfg new file mode 100644 index 0000000..48ecc63 --- /dev/null +++ b/br2-external/rootfs/board/sifive/hifive-unleashed/genimage_sdcard.cfg @@ -0,0 +1,11 @@ +image sdcard.img { + hdimage { + partition-table-type = "gpt" + } + + partition rootfs { + image = "rootfs.ext4" + partition-type-uuid = 0fc63daf-8483-4772-8e79-3d69d8477de4 + bootable = true + } +} diff --git a/br2-external/rootfs/board/sifive/hifive-unleashed/post-image.sh b/br2-external/rootfs/board/sifive/hifive-unleashed/post-image.sh new file mode 100755 index 0000000..e0bdf78 --- /dev/null +++ b/br2-external/rootfs/board/sifive/hifive-unleashed/post-image.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# Repack rootfs to the ext4 image + +PATH=/usr/sbin:$PATH + +cd output/images + +# Clear previous images + +[ -f rootfs ] && rm -rf rootfs +[ -e rootfs.img ] && rm -f rootfs.img + +mkdir -p rootfs +tar -xf rootfs.tar --directory rootfs + +truncate rootfs.img -s 120M +mkfs.ext4 -d rootfs rootfs.img diff --git a/br2-external/rootfs/configs/hifive_unleashed_defconfig b/br2-external/rootfs/configs/hifive_unleashed_defconfig new file mode 100644 index 0000000..89b6749 --- /dev/null +++ b/br2-external/rootfs/configs/hifive_unleashed_defconfig @@ -0,0 +1,58 @@ +# CCACHE + +BR2_CCACHE=y + +# Architecture +BR2_riscv=y +BR2_riscv_custom=y +BR2_RISCV_ISA_CUSTOM_RVM=y +BR2_RISCV_ISA_CUSTOM_RVF=y +BR2_RISCV_ISA_CUSTOM_RVD=y +BR2_RISCV_ISA_CUSTOM_RVC=y +BR2_RISCV_64=y +BR2_RISCV_ABI_LP64D=y + +# System +BR2_SYSTEM_DHCP="eth0" +BR2_TARGET_GENERIC_GETTY=y +BR2_TARGET_GENERIC_GETTY_PORT="ttySIF0" +BR2_ROOTFS_POST_IMAGE_SCRIPT="${BR2_EXTERNAL_SIFIVE_RENODE_CI_ROOTFS_PATH}/board/sifive/hifive-unleashed/post-image.sh" + +# Linux headers same as kernel, a 5.10 series +BR2_PACKAGE_HOST_LINUX_HEADERS_CUSTOM_5_10=y + +# Kernel +BR2_LINUX_KERNEL=n + +# Bootloader +BR2_TARGET_OPENSBI=n +BR2_TARGET_UBOOT=n + +# Host tools +BR2_PACKAGE_HOST_GENIMAGE=n +BR2_PACKAGE_HOST_UBOOT_TOOLS=n + +# Packages + +# video4linux packages +BR2_PACKAGE_LIBV4L=y +BR2_PACKAGE_LIBV4L_UTILS=y + +# python +BR2_PACKAGE_PYTHON3=y +BR2_PACKAGE_PYTHON_PIP=y + +# git +BR2_PACKAGE_GIT=y +BR2_PACKAGE_GIT_CRYPT=y +BR2_PACKAGE_OPENSSL=y +BR2_PACKAGE_LIBCURL=y +BR2_PACKAGE_LIBCURL_CURL=y +BR2_PACKAGE_CA_CERTIFICATES=y + +# gpio packages +BR2_PACKAGE_LIBGPIOD=y +BR2_PACKAGE_LIBGPIOD_TOOLS=y + +# i2c packages +BR2_PACKAGE_I2C_TOOLS=y diff --git a/br2-external/rootfs/external.desc b/br2-external/rootfs/external.desc new file mode 100644 index 0000000..61c3a12 --- /dev/null +++ b/br2-external/rootfs/external.desc @@ -0,0 +1,2 @@ +name: SIFIVE_RENODE_CI_ROOTFS +desc: Linux embedded instance for use in github actions diff --git a/br2-external/rootfs/external.mk b/br2-external/rootfs/external.mk new file mode 100644 index 0000000..e69de29 diff --git a/scripts/compile.sh b/scripts/compile.sh index 98d3985..a748faa 100755 --- a/scripts/compile.sh +++ b/scripts/compile.sh @@ -1,16 +1,39 @@ #!/bin/sh +mkdir -p images git clone --branch 2022.11.3 https://github.com/buildroot/buildroot buildroot -cd buildroot && \ -make BR2_EXTERNAL=../br2-external hifive_unleashed_defconfig && \ +cd buildroot + +# Set configuration for initramfs + +make BR2_EXTERNAL=../br2-external hifive_unleashed_defconfig make -j$(nproc) cd .. -mkdir -p images for i in hifive-unleashed-a00.dtb fw_payload.elf Image rootfs.cpio; do - cp buildroot/output/images/$i images; + cp buildroot/output/images/$i images done +# Clear only target binaries, do not delete host toolchain that will be used again +# More info: https://stackoverflow.com/questions/47320800/how-to-clean-only-target-in-buildroot + +cd buildroot + +rm -rf output/target +find output/ -name ".stamp_target_installed" -delete +rm -f output/build/host-gcc-final-*/.stamp_host_installed + + +# Set configuration for rootfs + +make BR2_EXTERNAL=../br2-external/rootfs hifive_unleashed_defconfig +make -j$(nproc) + +cd .. + +cp buildroot/output/images/rootfs.img images +rm -rf buildroot + tar cJvf images.tar.xz images