run.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # ======检查是否以sudo运行=================
  2. uid=`id -u`
  3. if [ ! $uid == "0" ];then
  4. echo "请以sudo权限运行"
  5. exit
  6. fi
  7. GENERATE_ISO=0
  8. IN_DOCKER=0
  9. # 第一个参数如果是--notbuild 那就不构建,直接运行
  10. if [ ! "$1" == "--nobuild" ]; then
  11. if [ "$1" == "--nomake" ]; then
  12. echo "将会跳过make过程直接构建镜像..."
  13. echo "开始构建..."
  14. GENERATE_ISO=1
  15. elif [ "$1" == "--docker" ]; then
  16. echo "使用docker构建"
  17. echo "开始构建..."
  18. sudo bash tools/build_in_docker.sh
  19. GENERATE_ISO=0
  20. elif [ "$1" == "--current_in_docker" ]; then
  21. echo "运行在docker内"
  22. echo "开始构建..."
  23. IN_DOCKER=1
  24. make all -j 16
  25. make clean
  26. GENERATE_ISO=1
  27. else
  28. echo "开始构建..."
  29. make all -j 16
  30. make clean
  31. GENERATE_ISO=1
  32. fi
  33. fi
  34. IA32_USE_QEMU=1
  35. bochsrc="./bochsrc"
  36. ARCH="x86_64"
  37. # 内核映像
  38. root_folder="$(pwd)"
  39. kernel="${root_folder}/bin/kernel/kernel.elf"
  40. boot_folder="${root_folder}/bin/disk_mount/boot"
  41. if [ "${GENERATE_ISO}" == "1" ]; then
  42. echo "开始生成iso..."
  43. # toolchain
  44. OS=`uname -s`
  45. if [ "${OS}" == "Linux" ]; then
  46. GRUB_PATH="$(dirname $(which grub-file))"
  47. elif [ "${OS}" == "Darwin" ]; then
  48. GRUB_PATH="$(pwd)/tools/grub-2.06/build/grub/bin"
  49. fi
  50. export PATH="${GRUB_PATH}:$PATH"
  51. # ==============检查文件是否齐全================
  52. bins[0]=${kernel}
  53. for file in ${bins[*]};do
  54. if [ ! -x $file ]; then
  55. echo "$file 不存在!"
  56. exit
  57. fi
  58. done
  59. # ===============文件检查完毕===================
  60. # 如果是 i386/x86_64,需要判断是否符合 multiboot2 标准
  61. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  62. if ${GRUB_PATH}/grub-file --is-x86-multiboot2 ${kernel}; then
  63. echo Multiboot2 Confirmed!
  64. else
  65. echo NOT Multiboot2!
  66. exit
  67. fi
  68. fi
  69. # 拷贝程序到硬盘
  70. cd tools
  71. # 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建)
  72. if [ ! -f "${root_folder}/bin/disk.img" ]; then
  73. echo "创建硬盘镜像文件..."
  74. bash ./create_hdd_image.sh
  75. fi
  76. mkdir -p ${root_folder}/bin/disk_mount
  77. bash mount_virt_disk.sh || exit 1
  78. mkdir -p ${boot_folder}/grub
  79. cp ${kernel} ${root_folder}/bin/disk_mount/boot
  80. cp ${root_folder}/bin/user/shell.elf ${root_folder}/bin/disk_mount
  81. cp ${root_folder}/bin/user/about.elf ${root_folder}/bin/disk_mount
  82. mkdir -p ${root_folder}/bin/disk_mount/dev
  83. touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
  84. # 设置 grub 相关数据
  85. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  86. touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
  87. cfg_content='set timeout=15
  88. set default=0
  89. menuentry "DragonOS" {
  90. multiboot2 /boot/kernel.elf "KERNEL_ELF"
  91. }'
  92. echo "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh
  93. fi
  94. # ${GRUB_PATH}/grub-mkrescue -o ${iso} ${iso_folder}
  95. # rm -rf ${iso_folder}
  96. LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}')
  97. echo $LOOP_DEVICE
  98. grub-install --target=i386-pc --boot-directory=${root_folder}/bin/disk_mount/boot/ /dev/$LOOP_DEVICE
  99. sync
  100. bash umount_virt_disk.sh
  101. cd ..
  102. if [ "${IN_DOCKER}" == "1" ]; then
  103. echo "运行在docker中, 构建结束"
  104. exit 0
  105. fi
  106. fi
  107. # 进行启动前检查
  108. flag_can_run=1
  109. 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|,$||")
  110. # 调试usb的trace
  111. 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
  112. if [ $flag_can_run -eq 1 ]; then
  113. if [ ${IA32_USE_QEMU} == 0 ]; then
  114. bochs -q -f ${bochsrc} -rc ./tools/bochsinit
  115. else
  116. qemu-system-x86_64 -d bin/disk.img -m 512M -smp 2,cores=2,threads=1,sockets=1 \
  117. -boot order=d \
  118. -monitor stdio -d cpu_reset,guest_errors,trace:check_exception,exec,cpu,out_asm,in_asm,${qemu_trace_usb} \
  119. -s -S -cpu "IvyBridge,+apic,+x2apic,+fpu,check,${allflags}" --enable-kvm -rtc clock=host,base=localtime -serial file:serial_opt.txt \
  120. -drive id=disk,file=bin/disk.img,if=none \
  121. -device ahci,id=ahci \
  122. -device ide-hd,drive=disk,bus=ahci.0 \
  123. -usb \
  124. -device qemu-xhci,id=xhci,p2=8,p3=4
  125. fi
  126. else
  127. echo "不满足运行条件"
  128. fi