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