Skip to content

Commit

Permalink
Separates initramfs and rootfs (antmicro#26)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
WiktorOgrodnik authored May 4, 2023
1 parent 7dd0d00 commit 8e2a17b
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 50 deletions.
9 changes: 7 additions & 2 deletions action/hifive.resc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
"""
Expand Down
34 changes: 19 additions & 15 deletions action/run-in-renode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!")
Expand Down
30 changes: 1 addition & 29 deletions br2-external/configs/hifive_unleashed_defconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# CCACHE

BR2_CCACHE=y
BR2_CCACHE_DIR=/buildroot-ccache

# Architecture
BR2_riscv=y
Expand All @@ -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
Expand Down Expand Up @@ -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"
48 changes: 48 additions & 0 deletions br2-external/package/busybox/busybox.config.fragment
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
From 502c5511e5f214da0d12862022f808929ca42a10 Mon Sep 17 00:00:00 2001
From: Wiktor Ogrodnik <wogrodnik@internships.antmicro.com>
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

Empty file added br2-external/rootfs/Config.in
Empty file.
Original file line number Diff line number Diff line change
@@ -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
}
}
18 changes: 18 additions & 0 deletions br2-external/rootfs/board/sifive/hifive-unleashed/post-image.sh
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions br2-external/rootfs/configs/hifive_unleashed_defconfig
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions br2-external/rootfs/external.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: SIFIVE_RENODE_CI_ROOTFS
desc: Linux embedded instance for use in github actions
Empty file added br2-external/rootfs/external.mk
Empty file.
31 changes: 27 additions & 4 deletions scripts/compile.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8e2a17b

Please sign in to comment.