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,exec,cpu,trace:virtio*
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_SMP="2,cores=2,threads=1,sockets=1"
55QEMU_MONITOR="stdio"
56QEMU_TRACE="${qemu_trace_std}"
57QEMU_CPU_FEATURES="IvyBridge,apic,x2apic,+fpu,check,${allflags}"
58QEMU_RTC_CLOCK="clock=host,base=localtime"
59QEMU_SERIAL="file:../serial_opt.txt"
60QEMU_DRIVE="id=disk,file=${QEMU_DISK_IMAGE},if=none"
61
62
63# ps: 下面这条使用tap的方式,无法dhcp获取到ip,暂时不知道为什么
64# 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 -machine accel=${qemu_accel} -machine q35 "
65QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -nic user,model=virtio-net-pci,hostfwd=tcp::12580-:12580 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 -machine accel=${qemu_accel} -machine q35 "
66
67QEMU_ARGUMENT="-d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d -monitor ${QEMU_MONITOR} -d ${qemu_trace_std} "
68
69QEMU_ARGUMENT+="-s -S -cpu ${QEMU_CPU_FEATURES} -rtc ${QEMU_RTC_CLOCK} -serial ${QEMU_SERIAL} -drive ${QEMU_DRIVE} ${QEMU_DEVICES}"
70
71if [ $flag_can_run -eq 1 ]; then
72  while true;do
73    case "$1" in
74        --bios)
75        case "$2" in
76              uefi) #uefi启动新增ovmf.fd固件
77              BIOS_TYPE=uefi
78            ;;
79              legacy)
80              BIOS_TYPE=lagacy
81              ;;
82        esac;shift 2;;
83        --display)
84        case "$2" in
85              vnc)
86              QEMU_ARGUMENT+=" -display vnc=:00"
87              ;;
88              window)
89              ;;
90        esac;shift 2;;
91        *) break
92      esac
93  done
94
95if [ ${BIOS_TYPE} == uefi ] ;then
96  if [ ${ARCH} == x86_64 ] ;then
97    sudo ${QEMU} -bios arch/x86_64/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
98  elif [ ${ARCH} == i386 ] ;then
99    sudo ${QEMU} -bios arch/i386/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
100  fi
101else
102  sudo ${QEMU} ${QEMU_ARGUMENT}
103fi
104
105else
106  echo "不满足运行条件"
107fi
108