ci: test qemu start

This commit is contained in:
2026-04-22 16:25:06 +08:00
parent bba3f5af67
commit 249e5ffc0a
2 changed files with 96 additions and 2 deletions

View File

@@ -8,11 +8,12 @@ env:
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-qemu
timeout-minutes: 4320 timeout-minutes: 4320
steps: steps:
- name: install tools - name: install tools
run: apt install -y wget && pip3 install requests --break-system-packages run: apt update && apt install -y wget iproute2 expect && pip3 install requests --break-system-packages
- uses: https://gitee.com/ci-actions/checkout.git@v3
- name: get the latest qemu image - name: get the latest qemu image
run: | run: |
BASE_URL="${QEMU_IMG%/*}" BASE_URL="${QEMU_IMG%/*}"
@@ -32,6 +33,12 @@ jobs:
echo "Download $FILE_NAME ..." echo "Download $FILE_NAME ..."
rm -f openruyi-virt_riscv64.qcow2* rm -f openruyi-virt_riscv64.qcow2*
wget -q --show-progress -O "/test/images/openruyi/openruyi-virt_riscv64.qcow2" "${QEMU_IMG}" wget -q --show-progress -O "/test/images/openruyi/openruyi-virt_riscv64.qcow2" "${QEMU_IMG}"
export PATH="$PATH:/usr/local/qemu/bin"
bash "$GITHUB_WORKSPACE/.github/workflows/scripts/qemu.sh" "${{ secrets.RISCV_DEFAULT_USERNAME }}" "${{ secrets.RISCV_DEFAULT_PASSWORD }}"
if [ $? -ne 0 ]; then
echo "Fail to start QEMU VM!"
exit $?
fi
xz -9 -T 0 -k openruyi-virt_riscv64.qcow2 xz -9 -T 0 -k openruyi-virt_riscv64.qcow2
popd popd
- name: run tests - name: run tests

87
.github/workflows/scripts/qemu.sh vendored Normal file
View File

@@ -0,0 +1,87 @@
#!/bin/bash
USER="$1"
PASS="$2"
START_PORT=12060
PORT=$START_PORT
while ss -tln | grep -qE ":${PORT}\b"; do
((PORT++))
done
QEMU_CMD="qemu-system-riscv64"
VM_ARGS="-nographic -machine virt,pflash0=pflash0,pflash1=pflash1 \
-smp 8 -m 12G \
-cpu rva23s64 \
-blockdev node-name=pflash0,driver=file,read-only=on,filename=RISCV_VIRT_CODE.fd \
-blockdev node-name=pflash1,driver=file,filename=RISCV_VIRT_VARS.fd \
-drive file=openRuyi-2026.03-Server-cloud.qcow2,format=qcow2,id=hd0,if=none \
-object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-vga \
-device virtio-rng-device,rng=rng0 \
-device virtio-blk-device,drive=hd0 \
-device virtio-net-device,netdev=usernet \
-netdev user,id=usernet,hostfwd=tcp::${PORT}-:22 \
-device qemu-xhci -usb -device usb-kbd -device usb-tablet"
expect <<-EOF
set timeout 600
puts "正在启动虚拟机..."
spawn $QEMU_CMD $VM_ARGS
expect {
"Press Control-D to continue" {
puts "\n检测到 Emergency Mode发送 Ctrl+D..."
send "\004"
exp_continue
}
-re "login:|Username:" {
puts "\n发现登录界面输入用户名..."
send "${USER}\r"
exp_continue
}
-re "\[Pp\]assword:" {
puts "\n输入密码..."
send "${PASS}\r"
exp_continue
}
"Login incorrect" {
puts "\n登录失败用户名或密码错误"
exit 2
}
-re {[#$]|root@.*#} {
puts "\n登录成功"
exit 0
}
-re {\x1b\[[0-9;]*[RfH]} {
exp_continue
}
timeout {
puts "\n虚拟机启动或登录超时"
exit 1
}
eof {
puts "\nQEMU 进程意外关闭"
exit 1
}
}
EOF
RET=$?
if [ $RET -eq 0 ]; then
echo "虚拟机启动成功"
elif [ $RET -eq 2 ]; then
echo "用户名或密码错误,登录失败"
else
echo "启动失败,退出码 $RET"
fi
exit $RET