aboutsummaryrefslogtreecommitdiff
path: root/bin/run-riscv-qemu.sh
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mssola@mssola.com>2025-10-03 16:20:14 +0200
committerMiquel Sabaté Solà <mssola@mssola.com>2025-10-03 16:20:14 +0200
commiteb79d8fcf0632582dfcc0dea01cdf6f89a664926 (patch)
tree5b031eaa6fddb2259b0f5a365488f81985ad38f9 /bin/run-riscv-qemu.sh
parente2bf48465014ace4590d94ae66681fbeafbb23b3 (diff)
downloaddotfiles-eb79d8fcf0632582dfcc0dea01cdf6f89a664926.tar.gz
dotfiles-eb79d8fcf0632582dfcc0dea01cdf6f89a664926.zip
run-riscv-qemu.sh: Add '-k/--kernel' and '-d/--disk' parameters
This allows this script to be more specific on the kernel being used, less hiden (as it was on an environment variable), and it easies up the route for a disk already containing a Linux system. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'bin/run-riscv-qemu.sh')
-rwxr-xr-xbin/run-riscv-qemu.sh130
1 files changed, 83 insertions, 47 deletions
diff --git a/bin/run-riscv-qemu.sh b/bin/run-riscv-qemu.sh
index 5098beb..77bf808 100755
--- a/bin/run-riscv-qemu.sh
+++ b/bin/run-riscv-qemu.sh
@@ -1,40 +1,21 @@
#!/usr/bin/env bash
-set -e
+set -ex
-# Enfore BUSYBOX_SRC and QEMU_KERNEL.
-if [ -z "$BUSYBOX_SRC" ]; then
- echo "run-riscv-qemu (error): you need to define BUSYBOX_SRC."
- exit 1
-fi
-
-# If the $QEMU_KERNEL environment variable is not explicitely set, then guess
-# one based on the current working directory.
-if [ -z "$QEMU_KERNEL" ]; then
- if [ ! -z "$ARCH" ]; then
- if [ -f "arch/$ARCH/boot/Image" ]; then
- QEMU_KERNEL=$(realpath "arch/$ARCH/boot/Image")
- else
- echo "run-riscv-qemu (error): you need to define QEMU_KERNEL with the Image to be used."
+EXTRA=""
+KERNEL=""
+DISK=""
+while [[ $# -gt 0 ]]; do
+ case $1 in
+ -d | --disk)
+ if [ -z "$2" ]; then
+ echo "run-riscv-qemu (error): you have to provide a value for -d/--disk"
exit 1
fi
- echo "run-riscv-qemu (info): using '$QEMU_KERNEL' as the Linux kernel Image."
- else
- echo "run-riscv-qemu (error): you need to define QEMU_KERNEL with the Image to be used."
- exit 1
- fi
-fi
-# Configurable variables.
-QEMU_CPU=${QEMU_CPU:-max}
-QEMU_SMP=${QEMU_SMP:-4}
-EXTRA=${EXTRA:-}
-
-# Cleanup older environments.
-rm -rf "$BUSYBOX_SRC/initramfs/home"
-
-while [[ $# -gt 0 ]]; do
- case $1 in
+ DISK="$2"
+ shift 2
+ ;;
-g | --gdb)
EXTRA="-s -S"
shift
@@ -43,14 +24,26 @@ while [[ $# -gt 0 ]]; do
SHOW_HELP=1
shift
;;
+ -k | --kernel)
+ if [ -z "$2" ]; then
+ echo "run-riscv-qemu (error): you have to provide a value for -k/--kernel"
+ exit 1
+ fi
+
+ KERNEL="$2"
+ shift 2
+ ;;
-*)
echo "run-riscv-qemu (error): unknown option '$1'."
exit 1
;;
*)
- mkdir -p "$BUSYBOX_SRC/initramfs/home/$(whoami)"
- cp "$1" "$BUSYBOX_SRC/initramfs/home/$(whoami)/"
- shift # past argument
+ if [ ! -z "$BUSYBOX_SRC" ]; then
+ mkdir -p "$BUSYBOX_SRC/initramfs/home/$(whoami)"
+ cp "$1" "$BUSYBOX_SRC/initramfs/home/$(whoami)/"
+ fi
+
+ shift
;;
esac
done
@@ -58,24 +51,67 @@ done
# Print the show message if needed.
if [ -n "$SHOW_HELP" ]; then
cat <<HERE
-usage: run-riscv-qemu.sh [OPTIONS]
+usage: run-riscv-qemu.sh [OPTIONS] [files]
Run QEMU/KVM tailored to my needs for testing the Linux Kernel.
+ -d, --disk Path to the disk to be passed into QEMU.
+ -g, --gdb Wait for a GDB connection.
+ -h, --help Show this message.
+ -k, --kernel Path to the Linux kernel image to be used.
- -g, --gdb Wait for a GDB connection.
- -h, --help Show this message.
+After the options you can pass paths to files you want to have inside of your
+/home directory (unless '-d/--disk' is defined).
+HERE
+ exit 0
+fi
+
+# If the $KERNEL argument has not been set, then guess one based on the current
+# working directory.
+if [ -z "$KERNEL" ]; then
+ if [ ! -z "$ARCH" ]; then
+ if [ -f "arch/$ARCH/boot/Image" ]; then
+ KERNEL=$(realpath "arch/$ARCH/boot/Image")
+ else
+ echo "run-riscv-qemu (error): you need to define QEMU_KERNEL with the Image to be used."
+ exit 1
+ fi
+ echo "run-riscv-qemu (info): using '$QEMU_KERNEL' as the Linux kernel Image."
+ else
+ echo "run-riscv-qemu (error): you need to define QEMU_KERNEL with the Image to be used."
+ exit 1
+ fi
+fi
-Environment variables that can be used the further configure the run:
+##
+# If the user provided a disk option, run QEMU with that disk in mind.
+
+if [ ! -z "$DISK" ]; then
+ qemu-system-riscv64 -machine virt -nographic \
+ -cpu max -smp 4 -m 8G \
+ -drive file="$DISK",format=qcow2,id=hd0 \
+ -device virtio-net-device,netdev=usernet \
+ -netdev user,id=usernet,hostfwd=tcp::22222-:22 \
+ -kernel "$KERNEL" \
+ -append nokaslr \
+ -append ftrace_dump_on_oops \
+ -append sysctl.kernel.panic_on_rcu_stall=1 \
+ $EXTRA
- - EXTRA: extra arguments to pass. Default: (empty).
- - QEMU_CPU: CPU model to use (i.e. argument for '-cpu'). Default: 'max'.
- - QEMU_SMP: number of cores (i.e. argument for '-smp'). Default: 4.
-HERE
exit 0
fi
##
-# Generate the initramfs.
+# No disk provided, let's generate an initramfs of our own run run in /dev/ram
+# with a tmpfs.
+
+# Enfore BUSYBOX_SRC.
+if [ -z "$BUSYBOX_SRC" ]; then
+ echo "run-riscv-qemu (error): you need to define BUSYBOX_SRC."
+ exit 1
+fi
+
+# Cleanup older environments.
+rm -rf "$BUSYBOX_SRC/initramfs/home"
pushd "$BUSYBOX_SRC/initramfs"
find . -print0 | cpio --null -o --format=newc | gzip -9 >../initramfs.cpio.gz
@@ -87,10 +123,10 @@ popd
set -x
qemu-system-riscv64 -machine virt -nographic \
- -cpu "$QEMU_CPU" \
- -m 8G \
- -smp "$QEMU_SMP" \
- -kernel "$QEMU_KERNEL" \
+ -cpu max -m 8G -smp 4 \
+ -device virtio-net-device,netdev=usernet \
+ -netdev user,id=usernet,hostfwd=tcp::22222-:22 \
+ -kernel "$KERNEL" \
-initrd "$BUSYBOX_SRC/initramfs.cpio.gz" \
-append "root=/dev/ram" \
-append nokaslr \