From fe7e74d567537d39020c22fad573b77aed2aa017 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 22 Apr 2025 16:09:23 +0200 Subject: bin: Force a busybox path when running QEMU MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running QEMU/RISC-V, force the user to provide the `BUSYBOX_SRC` environment variable. This variable points to the path where busybox has been built. This way the path to the initramfs is not hardcoded. Moreover, this allows us to rebuilt the initramfs every time the `run-riscv-qemu.sh` is called, which in turn brings some consistency to the process. Plus, this opens the door for allowing files to be passed into a new `/home` directory for the current user. This way, someone can just statically compile a binary via cross compilation, and then run it in isolation with QEMU/RISC-V. Last but not least, also update the .bashrc file with a value for `BUSYBOX_SRC` on my system. Signed-off-by: Miquel Sabaté Solà --- .bashrc | 4 ++++ bin/run-riscv-qemu.sh | 28 +++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.bashrc b/.bashrc index 76ff96a..3c75c5e 100644 --- a/.bashrc +++ b/.bashrc @@ -168,6 +168,10 @@ if [ "$(uname -m)" != "riscv64" ]; then export CROSS_COMPILE=riscv64-suse-linux- fi +# I have built a local busybox so I can generate an initramfs for various +# hacking thingies, let's reference it into an environment variable. +export BUSYBOX_SRC="$HOME/src/busybox/1.36.1" + ## # Lastly I had some problems with the GPG agent recently. So I copied a solution # from https://github.com/jessfraz/dotfiles diff --git a/bin/run-riscv-qemu.sh b/bin/run-riscv-qemu.sh index 6b9aaa9..94310c5 100755 --- a/bin/run-riscv-qemu.sh +++ b/bin/run-riscv-qemu.sh @@ -1,12 +1,20 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash set -e +# Enfore the BUSYBOX_SRC path. +if [ -z "$BUSYBOX_SRC" ]; then + echo "run-qemu (error): you need to define BUSYBOX_SRC." + exit 1 +fi + CORES=${CORES:-4} EXTRA=${EXTRA:-} REPO=${REPO:-riscv} KERNEL=${KERNEL:-~/src/git.kernel.org/linux/kernel/$REPO/arch/riscv/boot/Image} -INITRD=${INITRD:-~/src/busybox/1.36.1/initramfs.cpio.gz} + +# Cleanup older environments. +rm -rf "$BUSYBOX_SRC/initramfs/home" while [[ $# -gt 0 ]]; do case $1 in @@ -23,7 +31,8 @@ while [[ $# -gt 0 ]]; do exit 1 ;; *) - echo "run-qemu (warning): argument '$1' will be ignored." + mkdir -p "$BUSYBOX_SRC/initramfs/home/$(whoami)" + cp "$1" "$BUSYBOX_SRC/initramfs/home/$(whoami)/" shift # past argument ;; esac @@ -43,17 +52,26 @@ Environment variables that can be used the further configure the run: - CORES: number of cores (i.e. argument for '-smp'). Default: 4. - EXTRA: extra arguments to pass. Default: (empty). - - INITRD: the initial ramdisk for the run (e.g. argument for '-initrd'). Default: "$INITRD". - KERNEL: the kernel to be passed (i.e. argument for '-kernel'). Default: "$KERNEL". - REPO: the Linux repository to be used. Default: "riscv". HERE exit 0 fi +## +# Generate the initramfs. + +pushd "$BUSYBOX_SRC/initramfs" +find . -print0 | cpio --null -o --format=newc | gzip -9 >../initramfs.cpio.gz +popd + +## +# Actual run. + qemu-system-riscv64 -machine virt -nographic \ -smp "$CORES" \ -kernel "$KERNEL" \ - -initrd "$INITRD" \ + -initrd "$BUSYBOX_SRC/initramfs.cpio.gz" \ -append "root=/dev/ram" \ -append nokaslr \ -append ftrace_dump_on_oops \ -- cgit v1.2.3