1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#!/usr/bin/env bash
set -ex
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
DISK="$2"
shift 2
;;
-g | --gdb)
EXTRA="-s -S"
shift
;;
-h | --help)
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
;;
*)
if [ ! -z "$BUSYBOX_SRC" ]; then
mkdir -p "$BUSYBOX_SRC/initramfs/home/$(whoami)"
cp "$1" "$BUSYBOX_SRC/initramfs/home/$(whoami)/"
fi
shift
;;
esac
done
# Print the show message if needed.
if [ -n "$SHOW_HELP" ]; then
cat <<HERE
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.
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
##
# 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
exit 0
fi
##
# 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
popd
##
# Actual run.
set -x
qemu-system-riscv64 -machine virt -nographic \
-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 \
-append ftrace_dump_on_oops \
-append sysctl.kernel.panic_on_rcu_stall=1 \
$EXTRA
|