run-qemu.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. else
  47. # 判断系统kvm模块是否加载
  48. if [ ! -e /dev/kvm ]; then
  49. # kvm模块未加载,使用tcg加速
  50. qemu_accel="tcg"
  51. fi
  52. fi
  53. fi
  54. # uboot版本
  55. UBOOT_VERSION="v2023.10"
  56. RISCV64_UBOOT_PATH="arch/riscv64/u-boot-${UBOOT_VERSION}-riscv64"
  57. DISK_NAME="disk-${ARCH}.img"
  58. QEMU=qemu-system-${ARCH}
  59. QEMU_DISK_IMAGE="../bin/${DISK_NAME}"
  60. QEMU_MEMORY="512M"
  61. QEMU_MEMORY_BACKEND="dragonos-qemu-shm.ram"
  62. QEMU_MEMORY_BACKEND_PATH_PREFIX="/dev/shm"
  63. 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 "
  64. QEMU_SMP="2,cores=2,threads=1,sockets=1"
  65. QEMU_MONITOR="-monitor stdio"
  66. QEMU_TRACE="${qemu_trace_std}"
  67. QEMU_CPU_FEATURES=""
  68. QEMU_RTC_CLOCK=""
  69. QEMU_SERIAL="-serial file:../serial_opt.txt"
  70. QEMU_DRIVE="id=disk,file=${QEMU_DISK_IMAGE},if=none"
  71. QEMU_ACCELARATE=""
  72. QEMU_ARGUMENT=""
  73. QEMU_DEVICES=""
  74. BIOS_TYPE=""
  75. #这个变量为true则使用virtio磁盘
  76. VIRTIO_BLK_DEVICE=false
  77. # 如果qemu_accel不为空
  78. if [ -n "${qemu_accel}" ]; then
  79. QEMU_ACCELARATE=" -machine accel=${qemu_accel} "
  80. if [ "${qemu_accel}" == "kvm" ]; then
  81. QEMU_ACCELARATE+=" -enable-kvm "
  82. fi
  83. fi
  84. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  85. QEMU_MACHINE=" -machine q35,memory-backend=${QEMU_MEMORY_BACKEND} "
  86. QEMU_CPU_FEATURES+="-cpu IvyBridge,apic,x2apic,+fpu,check,+vmx,${allflags}"
  87. QEMU_RTC_CLOCK+=" -rtc clock=host,base=localtime"
  88. if [ ${VIRTIO_BLK_DEVICE} == false ]; then
  89. QEMU_DEVICES_DISK+="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 "
  90. else
  91. QEMU_DEVICES_DISK="-device virtio-blk-pci,drive=disk -device pci-bridge,chassis_nr=1,id=pci.1 -device pcie-root-port "
  92. fi
  93. else
  94. QEMU_MACHINE=" -machine virt,memory-backend=${QEMU_MEMORY_BACKEND} -cpu sifive-u54 "
  95. QEMU_DEVICES_DISK="-device virtio-blk-device,drive=disk "
  96. fi
  97. if [ ${ARCH} == "riscv64" ]; then
  98. # 如果是riscv64架构,就不需要图形界面
  99. QEMU_ARGUMENT+=" --nographic "
  100. # 从控制台显示
  101. QEMU_MONITOR=""
  102. QEMU_SERIAL=""
  103. fi
  104. while true;do
  105. case "$1" in
  106. --bios)
  107. case "$2" in
  108. uefi) #uefi启动新增ovmf.fd固件
  109. BIOS_TYPE=uefi
  110. ;;
  111. legacy)
  112. BIOS_TYPE=legacy
  113. ;;
  114. esac;shift 2;;
  115. --display)
  116. case "$2" in
  117. vnc)
  118. QEMU_ARGUMENT+=" -display vnc=:00 "
  119. ;;
  120. window)
  121. ;;
  122. nographic)
  123. QEMU_SERIAL=" -serial mon:stdio "
  124. QEMU_MONITOR=""
  125. QEMU_ARGUMENT+=" --nographic "
  126. QEMU_ARGUMENT+=" -kernel ../bin/kernel/kernel.elf "
  127. ;;
  128. esac;shift 2;;
  129. *) break
  130. esac
  131. done
  132. # ps: 下面这条使用tap的方式,无法dhcp获取到ip,暂时不知道为什么
  133. # 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 "
  134. QEMU_DEVICES+="${QEMU_DEVICES_DISK} "
  135. 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 "
  136. # E1000E
  137. # 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 "
  138. QEMU_ARGUMENT+="-d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d ${QEMU_MONITOR} -d ${qemu_trace_std} "
  139. QEMU_ARGUMENT+="-s ${QEMU_MACHINE} ${QEMU_CPU_FEATURES} ${QEMU_RTC_CLOCK} ${QEMU_SERIAL} -drive ${QEMU_DRIVE} ${QEMU_DEVICES} "
  140. QEMU_ARGUMENT+=" ${QEMU_SHM_OBJECT} "
  141. QEMU_ARGUMENT+=" ${QEMU_ACCELARATE} "
  142. # 安装riscv64的uboot
  143. install_riscv_uboot()
  144. {
  145. if [ ! -d ${RISCV64_UBOOT_PATH} ]; then
  146. echo "正在下载u-boot..."
  147. uboot_tar_name="u-boot-${UBOOT_VERSION}-riscv64.tar.xz"
  148. uboot_parent_path=$(dirname ${RISCV64_UBOOT_PATH}) || (echo "获取riscv u-boot 版本 ${UBOOT_VERSION} 的父目录失败" && exit 1)
  149. if [ ! -f ${uboot_tar_name} ]; then
  150. wget https://mirrors.dragonos.org.cn/pub/third_party/u-boot/${uboot_tar_name} || (echo "下载riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1)
  151. fi
  152. echo "下载完成"
  153. echo "正在解压u-boot到 '$uboot_parent_path'..."
  154. mkdir -p $uboot_parent_path
  155. tar xvf u-boot-${UBOOT_VERSION}-riscv64.tar.xz -C ${uboot_parent_path} || (echo "解压riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1)
  156. echo "解压完成"
  157. rm -rf u-boot-${UBOOT_VERSION}-riscv64.tar.xz
  158. fi
  159. echo "riscv u-boot 版本 ${UBOOT_VERSION} 已经安装"
  160. }
  161. if [ $flag_can_run -eq 1 ]; then
  162. # 删除共享内存
  163. sudo rm -rf ${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND}
  164. if [ ${BIOS_TYPE} == uefi ] ;then
  165. if [ ${ARCH} == x86_64 ] ;then
  166. sudo ${QEMU} -bios arch/x86_64/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
  167. elif [ ${ARCH} == i386 ] ;then
  168. sudo ${QEMU} -bios arch/i386/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
  169. elif [ ${ARCH} == riscv64 ] ;then
  170. install_riscv_uboot
  171. sudo ${QEMU} -kernel ${RISCV64_UBOOT_PATH}/u-boot.bin ${QEMU_ARGUMENT}
  172. else
  173. echo "不支持的架构: ${ARCH}"
  174. fi
  175. else
  176. # 如果是i386架构或者x86_64架构,就直接启动
  177. if [ ${ARCH} == x86_64 ] || [ ${ARCH} == i386 ] ;then
  178. sudo ${QEMU} ${QEMU_ARGUMENT}
  179. elif [ ${ARCH} == riscv64 ] ;then
  180. # 如果是riscv64架构,就与efi启动一样
  181. install_riscv_uboot
  182. sudo ${QEMU} -kernel ${RISCV64_UBOOT_PATH}/u-boot.bin ${QEMU_ARGUMENT}
  183. else
  184. echo "不支持的架构: ${ARCH}"
  185. fi
  186. fi
  187. # 删除共享内存
  188. sudo rm -rf ${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND}
  189. else
  190. echo "不满足运行条件"
  191. fi