run-qemu.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. check_dependencies()
  2. {
  3. # Check if qemu is installed
  4. if [ -z "$(which qemu-system-x86_64)" ]; then
  5. echo "Please install qemu first!"
  6. exit 1
  7. fi
  8. # Check if brctl is installed
  9. if [ -z "$(which brctl)" ]; then
  10. echo "Please install bridge-utils first!"
  11. exit 1
  12. fi
  13. # Check if dnsmasq is installed
  14. if [ -z "$(which dnsmasq)" ]; then
  15. echo "Please install dnsmasq first!"
  16. exit 1
  17. fi
  18. # Check if iptable is installed
  19. if [ -z "$(which iptables)" ]; then
  20. echo "Please install iptables first!"
  21. exit 1
  22. fi
  23. }
  24. check_dependencies
  25. # 进行启动前检查
  26. flag_can_run=1
  27. ARGS=`getopt -o p -l bios:,display: -- "$@"`
  28. eval set -- "${ARGS}"
  29. echo "$@"
  30. allflags=
  31. # allflags=$(qemu-system-x86_64 -cpu help | awk '/flags/ {y=1; getline}; y {print}' | tr ' ' '\n' | grep -Ev "^$" | sed -r 's|^|+|' | tr '\n' ',' | sed -r "s|,$||")
  32. # 设置ARCH环境变量,如果没有设置,就默认为x86_64
  33. export ARCH=${ARCH:=x86_64}
  34. echo "ARCH=${ARCH}"
  35. #ARCH="i386"
  36. # 请根据自己的需要,在-d 后方加入所需的 trace 事件
  37. # 标准的trace events
  38. qemu_trace_std=cpu_reset,guest_errors,trace:virtio*,trace:e1000e_rx*,trace:e1000e_tx*,trace:e1000e_irq*
  39. # 调试usb的trace
  40. qemu_trace_usb=trace:usb_xhci_reset,trace:usb_xhci_run,trace:usb_xhci_stop,trace:usb_xhci_irq_msi,trace:usb_xhci_irq_msix,trace:usb_xhci_port_reset,trace:msix_write_config,trace:usb_xhci_irq_msix,trace:usb_xhci_irq_msix_use,trace:usb_xhci_irq_msix_unuse,trace:usb_xhci_irq_msi,trace:usb_xhci_*
  41. # 根据架构设置qemu的加速方式
  42. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  43. qemu_accel="kvm"
  44. if [ $(uname) == Darwin ]; then
  45. qemu_accel=hvf
  46. fi
  47. fi
  48. # uboot版本
  49. UBOOT_VERSION="v2023.10"
  50. RISCV64_UBOOT_PATH="arch/riscv64/u-boot-${UBOOT_VERSION}-riscv64"
  51. DISK_NAME="disk-${ARCH}.img"
  52. QEMU=qemu-system-${ARCH}
  53. QEMU_DISK_IMAGE="../bin/${DISK_NAME}"
  54. QEMU_MEMORY="512M"
  55. QEMU_MEMORY_BACKEND="dragonos-qemu-shm.ram"
  56. QEMU_MEMORY_BACKEND_PATH_PREFIX="/dev/shm"
  57. QEMU_SHM_OBJECT="-object memory-backend-file,size=${QEMU_MEMORY},id=${QEMU_MEMORY_BACKEND},mem-path=${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND},share=on "
  58. QEMU_SMP="2,cores=2,threads=1,sockets=1"
  59. QEMU_MONITOR="-monitor stdio"
  60. QEMU_TRACE="${qemu_trace_std}"
  61. QEMU_CPU_FEATURES=""
  62. QEMU_RTC_CLOCK=""
  63. QEMU_SERIAL="-serial file:../serial_opt.txt"
  64. QEMU_DRIVE="id=disk,file=${QEMU_DISK_IMAGE},if=none"
  65. QEMU_ACCELARATE=""
  66. QEMU_ARGUMENT=""
  67. QEMU_DEVICES=""
  68. BIOS_TYPE=""
  69. #这个变量为true则使用virtio磁盘
  70. VIRTIO_BLK_DEVICE=false
  71. # 如果qemu_accel不为空
  72. if [ -n "${qemu_accel}" ]; then
  73. QEMU_ACCELARATE="-machine accel=${qemu_accel} -enable-kvm "
  74. fi
  75. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  76. QEMU_MACHINE=" -machine q35,memory-backend=${QEMU_MEMORY_BACKEND} "
  77. QEMU_CPU_FEATURES+="-cpu IvyBridge,apic,x2apic,+fpu,check,+vmx,${allflags}"
  78. QEMU_RTC_CLOCK+=" -rtc clock=host,base=localtime"
  79. if [ ${VIRTIO_BLK_DEVICE} == false ]; then
  80. QEMU_DEVICES_DISK+="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 "
  81. else
  82. QEMU_DEVICES_DISK="-device virtio-blk-pci,drive=disk -device pci-bridge,chassis_nr=1,id=pci.1 -device pcie-root-port "
  83. fi
  84. else
  85. QEMU_MACHINE=" -machine virt,memory-backend=${QEMU_MEMORY_BACKEND} -cpu sifive-u54 "
  86. QEMU_DEVICES_DISK="-device virtio-blk-device,drive=disk "
  87. fi
  88. if [ ${ARCH} == "riscv64" ]; then
  89. # 如果是riscv64架构,就不需要图形界面
  90. QEMU_ARGUMENT+=" --nographic "
  91. # 从控制台显示
  92. QEMU_MONITOR=""
  93. QEMU_SERIAL=""
  94. fi
  95. while true;do
  96. case "$1" in
  97. --bios)
  98. case "$2" in
  99. uefi) #uefi启动新增ovmf.fd固件
  100. BIOS_TYPE=uefi
  101. ;;
  102. legacy)
  103. BIOS_TYPE=legacy
  104. ;;
  105. esac;shift 2;;
  106. --display)
  107. case "$2" in
  108. vnc)
  109. QEMU_ARGUMENT+=" -display vnc=:00 "
  110. ;;
  111. window)
  112. ;;
  113. nographic)
  114. QEMU_SERIAL=" -serial mon:stdio "
  115. QEMU_MONITOR=""
  116. QEMU_ARGUMENT+=" --nographic "
  117. QEMU_ARGUMENT+=" -kernel ../bin/kernel/kernel.elf "
  118. ;;
  119. esac;shift 2;;
  120. *) break
  121. esac
  122. done
  123. # ps: 下面这条使用tap的方式,无法dhcp获取到ip,暂时不知道为什么
  124. # QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -net nic,netdev=nic0 -netdev tap,id=nic0,model=virtio-net-pci,script=qemu/ifup-nat,downscript=qemu/ifdown-nat -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
  125. QEMU_DEVICES+="${QEMU_DEVICES_DISK} "
  126. QEMU_DEVICES+=" -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580 -device virtio-net-pci,vectors=5,netdev=hostnet0,id=net0 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
  127. # E1000E
  128. # QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580 -net nic,model=e1000e,netdev=hostnet0,id=net0 -netdev user,id=hostnet1,hostfwd=tcp::12581-:12581 -device virtio-net-pci,vectors=5,netdev=hostnet1,id=net1 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
  129. QEMU_ARGUMENT+="-d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d ${QEMU_MONITOR} -d ${qemu_trace_std} "
  130. QEMU_ARGUMENT+="-s ${QEMU_MACHINE} ${QEMU_CPU_FEATURES} ${QEMU_RTC_CLOCK} ${QEMU_SERIAL} -drive ${QEMU_DRIVE} ${QEMU_DEVICES} "
  131. QEMU_ARGUMENT+=" ${QEMU_SHM_OBJECT} "
  132. QEMU_ARGUMENT+=" ${QEMU_ACCELARATE} "
  133. # 安装riscv64的uboot
  134. install_riscv_uboot()
  135. {
  136. if [ ! -d ${RISCV64_UBOOT_PATH} ]; then
  137. echo "正在下载u-boot..."
  138. uboot_tar_name="u-boot-${UBOOT_VERSION}-riscv64.tar.xz"
  139. uboot_parent_path=$(dirname ${RISCV64_UBOOT_PATH}) || (echo "获取riscv u-boot 版本 ${UBOOT_VERSION} 的父目录失败" && exit 1)
  140. if [ ! -f ${uboot_tar_name} ]; then
  141. wget https://mirrors.dragonos.org.cn/pub/third_party/u-boot/${uboot_tar_name} || (echo "下载riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1)
  142. fi
  143. echo "下载完成"
  144. echo "正在解压u-boot到 '$uboot_parent_path'..."
  145. mkdir -p $uboot_parent_path
  146. tar xvf u-boot-${UBOOT_VERSION}-riscv64.tar.xz -C ${uboot_parent_path} || (echo "解压riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1)
  147. echo "解压完成"
  148. rm -rf u-boot-${UBOOT_VERSION}-riscv64.tar.xz
  149. fi
  150. echo "riscv u-boot 版本 ${UBOOT_VERSION} 已经安装"
  151. }
  152. if [ $flag_can_run -eq 1 ]; then
  153. # 删除共享内存
  154. sudo rm -rf ${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND}
  155. if [ ${BIOS_TYPE} == uefi ] ;then
  156. if [ ${ARCH} == x86_64 ] ;then
  157. sudo ${QEMU} -bios arch/x86_64/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
  158. elif [ ${ARCH} == i386 ] ;then
  159. sudo ${QEMU} -bios arch/i386/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
  160. elif [ ${ARCH} == riscv64 ] ;then
  161. install_riscv_uboot
  162. sudo ${QEMU} -kernel ${RISCV64_UBOOT_PATH}/u-boot.bin ${QEMU_ARGUMENT}
  163. else
  164. echo "不支持的架构: ${ARCH}"
  165. fi
  166. else
  167. # 如果是i386架构或者x86_64架构,就直接启动
  168. if [ ${ARCH} == x86_64 ] || [ ${ARCH} == i386 ] ;then
  169. sudo ${QEMU} ${QEMU_ARGUMENT}
  170. elif [ ${ARCH} == riscv64 ] ;then
  171. # 如果是riscv64架构,就与efi启动一样
  172. install_riscv_uboot
  173. sudo ${QEMU} -kernel ${RISCV64_UBOOT_PATH}/u-boot.bin ${QEMU_ARGUMENT}
  174. else
  175. echo "不支持的架构: ${ARCH}"
  176. fi
  177. fi
  178. # 删除共享内存
  179. sudo rm -rf ${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND}
  180. else
  181. echo "不满足运行条件"
  182. fi