forked from redrsoe2100/os-autotest
88 lines
2.0 KiB
Bash
88 lines
2.0 KiB
Bash
#!/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
|