run-qemu.sh 7.8 KB

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