xref: /DragonOS/tools/run-qemu.sh (revision be8cdf4b8edcd9579572672411f4489039dea313)
1check_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
9    # Check if brctl is installed
10    if [ -z "$(which brctl)" ]; then
11        echo "Please install bridge-utils first!"
12        exit 1
13    fi
14
15    # Check if dnsmasq is installed
16    if [ -z "$(which dnsmasq)" ]; then
17        echo "Please install dnsmasq first!"
18        exit 1
19    fi
20
21    # Check if iptable is installed
22    if [ -z "$(which iptables)" ]; then
23        echo "Please install iptables first!"
24        exit 1
25    fi
26
27}
28
29check_dependencies
30
31# 进行启动前检查
32flag_can_run=1
33ARGS=`getopt -o p -l bios:,display: -- "$@"`
34eval set -- "${ARGS}"
35echo "$@"
36allflags=
37# 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|,$||")
38ARCH="x86_64"
39#ARCH="i386"
40# 请根据自己的需要,在-d 后方加入所需的 trace 事件
41
42# 标准的trace events
43qemu_trace_std=cpu_reset,guest_errors,trace:virtio*,trace:e1000e_rx*,trace:e1000e_tx*,trace:e1000e_irq*
44# 调试usb的trace
45qemu_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_*
46qemu_accel="kvm"
47if [ $(uname) == Darwin ]; then
48    qemu_accel=hvf
49fi
50
51QEMU=qemu-system-x86_64
52QEMU_DISK_IMAGE="../bin/disk.img"
53QEMU_MEMORY="512M"
54QEMU_MEMORY_BACKEND="dragonos-qemu-shm.ram"
55QEMU_MEMORY_BACKEND_PATH_PREFIX="/dev/shm"
56QEMU_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 "
57QEMU_SMP="2,cores=2,threads=1,sockets=1"
58QEMU_MONITOR="stdio"
59QEMU_TRACE="${qemu_trace_std}"
60QEMU_CPU_FEATURES="IvyBridge,apic,x2apic,+fpu,check,+vmx,${allflags}"
61QEMU_RTC_CLOCK="clock=host,base=localtime"
62QEMU_SERIAL="file:../serial_opt.txt"
63QEMU_DRIVE="id=disk,file=${QEMU_DISK_IMAGE},if=none"
64QEMU_ACCELARATE=""
65
66# 如果qemu_accel不为空
67if [ -n "${qemu_accel}" ]; then
68    QEMU_ACCELARATE="-machine accel=${qemu_accel} -enable-kvm "
69fi
70
71QEMU_MACHINE=" -machine q35,memory-backend=${QEMU_MEMORY_BACKEND} "
72
73# ps: 下面这条使用tap的方式,无法dhcp获取到ip,暂时不知道为什么
74# 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 "
75QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -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 "
76# E1000E
77# 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 "
78QEMU_ARGUMENT="-d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d -monitor ${QEMU_MONITOR} -d ${qemu_trace_std} "
79
80QEMU_ARGUMENT+="-s ${QEMU_MACHINE} -cpu ${QEMU_CPU_FEATURES} -rtc ${QEMU_RTC_CLOCK} -serial ${QEMU_SERIAL} -drive ${QEMU_DRIVE} ${QEMU_DEVICES}"
81QEMU_ARGUMENT+=" ${QEMU_SHM_OBJECT} "
82QEMU_ARGUMENT+=" ${QEMU_ACCELARATE} "
83
84
85if [ $flag_can_run -eq 1 ]; then
86  while true;do
87    case "$1" in
88        --bios)
89        case "$2" in
90              uefi) #uefi启动新增ovmf.fd固件
91              BIOS_TYPE=uefi
92            ;;
93              legacy)
94              BIOS_TYPE=lagacy
95              ;;
96        esac;shift 2;;
97        --display)
98        case "$2" in
99              vnc)
100              QEMU_ARGUMENT+=" -display vnc=:00"
101              ;;
102              window)
103              ;;
104        esac;shift 2;;
105        *) break
106      esac
107  done
108
109# 删除共享内存
110sudo rm -rf ${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND}
111
112if [ ${BIOS_TYPE} == uefi ] ;then
113  if [ ${ARCH} == x86_64 ] ;then
114    sudo ${QEMU} -bios arch/x86_64/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
115  elif [ ${ARCH} == i386 ] ;then
116    sudo ${QEMU} -bios arch/i386/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
117  fi
118else
119  sudo ${QEMU} ${QEMU_ARGUMENT}
120fi
121# 删除共享内存
122sudo rm -rf ${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND}
123else
124  echo "不满足运行条件"
125fi
126