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