forked from ClangBuiltLinux/boot-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
boot-qemu.sh
executable file
·199 lines (159 loc) · 5.65 KB
/
boot-qemu.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
# Root of the repo
BASE=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)
# Prints an error message in bold red then exits
function die() {
printf "\n\033[01;31m%s\033[0m\n" "${1}"
exit 1
}
# Check that a binary is found
function checkbin() {
command -v "${1}" &>/dev/null || die "${1} could not be found, please install it!"
}
# Parse inputs to the script
function parse_parameters() {
while (( ${#} )); do
case ${1} in
-a|--arch|--architecture)
shift
case ${1} in
arm32_v5|arm32_v6|arm32_v7|arm64|mips|mipsel|ppc32|ppc64|ppc64le|x86_64) ARCH=${1} ;;
*) die "Invalid --arch value '${1}'" ;;
esac ;;
-d|--debug)
set -x ;;
-h|--help)
echo
cat "${BASE}"/boot-qemu-help.txt
echo
exit 0 ;;
-i|--interactive|--shell)
INTERACTIVE=true ;;
-k|--kbuild-folder)
shift && KBUILD_DIR=${1} ;;
-t|--timeout)
shift && TIMEOUT=${1} ;;
*)
die "Invalid parameter '${1}'" ;;
esac
shift
done
}
# Sanity check parameters and required tools
function sanity_check() {
# Kernel build folder and architecture are required paramters
# We are not interactive by default and the default timeout is 3m
: "${ARCH:?}" "${KBUILD_DIR:?}" "${INTERACTIVE:=false}" "${TIMEOUT:=3m}"
# KBUILD_DIR could be a relative path; turn it into an absolute one with readlink
KBUILD_DIR=$(readlink -f "${KBUILD_DIR}")
# Let the user know if the kernel build folder does not exist
[[ -d ${KBUILD_DIR} ]] || die "${KBUILD_DIR} does not exist!"
# Make sure zstd is install
checkbin zstd
}
# Decompress rootfs images
function decomp_rootfs() {
[[ ${ARCH} =~ arm32 ]] && ARCH_RTFS_DIR=arm
IMAGES_DIR=${BASE}/images/${ARCH_RTFS_DIR:-${ARCH}}
ROOTFS=${IMAGES_DIR}/rootfs.cpio
rm -rf "${ROOTFS}"
zstd -d "${ROOTFS}".zst -o "${ROOTFS}"
}
# Boot QEMU
function setup_qemu_args() {
if ${INTERACTIVE}; then
RDINIT=" rdinit=/bin/sh"
APPEND_RDINIT=( -append "${RDINIT}" )
fi
case ${ARCH} in
arm32_v5)
ARCH=arm
QEMU_ARCH_ARGS=( "${APPEND_RDINIT[@]}"
-dtb "${KBUILD_DIR}"/arch/arm/boot/dts/aspeed-bmc-opp-palmetto.dtb
-machine palmetto-bmc
-no-reboot )
QEMU=( qemu-system-arm ) ;;
arm32_v6)
ARCH=arm
QEMU_ARCH_ARGS=( "${APPEND_RDINIT[@]}"
-dtb "${KBUILD_DIR}"/arch/arm/boot/dts/aspeed-bmc-opp-romulus.dtb
-machine romulus-bmc
-no-reboot )
QEMU=( qemu-system-arm ) ;;
arm32_v7)
ARCH=arm
QEMU_ARCH_ARGS=( -append "console=ttyAMA0${RDINIT}"
-machine virt
-no-reboot )
QEMU=( qemu-system-arm ) ;;
arm64)
KIMAGE=Image.gz
QEMU_ARCH_ARGS=( -append "console=ttyAMA0${RDINIT}"
-cpu cortex-a57
-machine virt )
QEMU=( qemu-system-aarch64 ) ;;
mips|mipsel)
KIMAGE=vmlinux
QEMU_ARCH_ARGS=( "${APPEND_RDINIT[@]}"
-cpu 24Kf
-machine malta )
QEMU=( qemu-system-"${ARCH}" )
ARCH=mips ;;
ppc32)
ARCH=powerpc
QEMU_ARCH_ARGS=( -append "console=ttyS0${RDINIT}"
-machine bamboo
-no-reboot )
QEMU_RAM=128m
QEMU=( qemu-system-ppc ) ;;
ppc64)
ARCH=powerpc
KIMAGE=vmlinux
QEMU_ARCH_ARGS=( "${APPEND_RDINIT[@]}"
-machine pseries
-vga none )
QEMU_RAM=1G
QEMU=( qemu-system-ppc64 ) ;;
ppc64le)
ARCH=powerpc
KIMAGE=zImage.epapr
QEMU_ARCH_ARGS=( "${APPEND_RDINIT[@]}"
-device "ipmi-bmc-sim,id=bmc0"
-device "isa-ipmi-bt,bmc=bmc0,irq=10"
-L "${IMAGES_DIR}/" -bios skiboot.lid
-machine powernv )
QEMU_RAM=2G
QEMU=( qemu-system-ppc64 ) ;;
x86_64)
KIMAGE=bzImage
QEMU_ARCH_ARGS=( -append "console=ttyS0${RDINIT}" )
# Use KVM if the processor supports it (first part) and the KVM module is loaded (second part)
[[ $(grep -c -E 'vmx|svm' /proc/cpuinfo) -gt 0 && $(lsmod 2>/dev/null | grep -c kvm) -gt 0 ]] && \
QEMU_ARCH_ARGS=( "${QEMU_ARCH_ARGS[@]}" -cpu host -d "unimp,guest_errors" -enable-kvm )
QEMU=( qemu-system-x86_64 ) ;;
esac
checkbin "${QEMU[*]}"
[[ ${KIMAGE:=zImage} = "vmlinux" ]] || BOOT_DIR=arch/${ARCH}/boot/
KERNEL=${KBUILD_DIR}/${BOOT_DIR}${KIMAGE}
[[ -f ${KERNEL} ]] || die "${KERNEL} does not exist!"
}
# Invoke QEMU
function invoke_qemu() {
${INTERACTIVE} || QEMU=( timeout "${TIMEOUT}" unbuffer "${QEMU[@]}" )
set -x
"${QEMU[@]}" \
"${QEMU_ARCH_ARGS[@]}" \
-display none \
-initrd "${ROOTFS}" \
-kernel "${KERNEL}" \
-m "${QEMU_RAM:=512m}" \
-serial mon:stdio
RET=${?}
set +x
return ${RET}
}
parse_parameters "${@}"
sanity_check
decomp_rootfs
setup_qemu_args
invoke_qemu