write_disk_image.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ###############################################
  2. # 该脚本用于将文件拷贝到磁盘镜像中,
  3. # 并在磁盘镜像中安装grub引导程序
  4. #
  5. # 用法:bash write_disk_image.sh --bios legacy/uefi
  6. # 如果之前创建的 disk-${ARCH}.img 是MBR分区表,那么请这样运行它:bash write_disk_image.sh --bios legacy
  7. # 如果之前创建的 disk-${ARCH}.img 是GPT分区表,那么请这样运行它:bash write_disk_image.sh --bios uefi
  8. # 通过设置ARCH为x86_64/i386/riscv64,进行64/32位uefi的install,但是请记住该处的ARCH应与run-qemu.sh中的一致
  9. ###############################################
  10. echo "ARCH=${ARCH}"
  11. # 给ARCH变量赋默认值
  12. export ARCH=${ARCH:=x86_64}
  13. export DADK=${DADK:=dadk}
  14. # 内核映像
  15. root_folder=$(dirname $(pwd))
  16. kernel="${root_folder}/bin/kernel/kernel.elf"
  17. mount_folder=$($DADK -w $root_folder rootfs show-mountpoint || exit 1)
  18. boot_folder="${mount_folder}/boot"
  19. GRUB_INSTALL_PATH="${boot_folder}/grub"
  20. ARGS=`getopt -o p -l bios: -- "$@"`
  21. eval set -- "${ARGS}"
  22. #echo formatted parameters=[$@]
  23. echo "开始写入磁盘镜像..."
  24. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  25. INSTALL_GRUB_TO_IMAGE="1"
  26. else
  27. INSTALL_GRUB_TO_IMAGE="0"
  28. fi
  29. # toolchain
  30. GRUB_ABS_PREFIX=/opt/dragonos-grub
  31. GRUB_PATH_I386_LEGACY_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/sbin/grub-install
  32. GRUB_PATH_I386_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/efi/grub/sbin/grub-install
  33. GRUB_PATH_X86_64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/x86_64/efi/grub/sbin/grub-install
  34. GRUB_PATH_RISCV64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/riscv64/efi/grub/sbin/grub-install
  35. GRUB_PATH_I386_LEGACY_FILE=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/bin/grub-file
  36. # ==============检查文件是否齐全================
  37. bins[0]=${kernel}
  38. for file in ${bins[*]};do
  39. if [ ! -x $file ]; then
  40. echo "$file 不存在!"
  41. exit
  42. fi
  43. done
  44. # ===============文件检查完毕===================
  45. # 如果是 i386/x86_64,需要判断是否符合 multiboot2 标准
  46. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  47. if ${GRUB_PATH_I386_LEGACY_FILE} --is-x86-multiboot2 ${kernel}; then
  48. echo Multiboot2 Confirmed!
  49. else
  50. echo NOT Multiboot2!
  51. exit
  52. fi
  53. fi
  54. # 判断是否存在硬盘镜像文件,如果不存在,就创建一个
  55. echo "创建硬盘镜像文件..."
  56. $DADK -w $root_folder rootfs create --skip-if-exists || exit 1
  57. $DADK -w $root_folder rootfs mount || exit 1
  58. LOOP_DEVICE=$($DADK -w $root_folder rootfs show-loop-device || exit 1)
  59. echo $LOOP_DEVICE
  60. echo ${mount_folder}
  61. # mkdir -p ${GRUB_INSTALL_PATH}
  62. # 检测grub文件夹是否存在
  63. if [ -d "${GRUB_INSTALL_PATH}" ] || [ "${INSTALL_GRUB_TO_IMAGE}" = "0" ]; then
  64. echo "无需安装grub"
  65. INSTALL_GRUB_TO_IMAGE="0"
  66. else
  67. mkdir -p ${GRUB_INSTALL_PATH}
  68. fi
  69. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  70. cp ${kernel} ${mount_folder}/boot/
  71. fi
  72. # 拷贝用户程序到磁盘镜像
  73. mkdir -p ${mount_folder}/bin
  74. mkdir -p ${mount_folder}/dev
  75. mkdir -p ${mount_folder}/proc
  76. mkdir -p ${mount_folder}/usr
  77. cp -r ${root_folder}/bin/sysroot/* ${mount_folder}/
  78. # 设置 grub 相关数据
  79. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  80. touch ${mount_folder}/boot/grub/grub.cfg
  81. cfg_content='set timeout=15
  82. set default=0
  83. insmod efi_gop
  84. menuentry "DragonOS" {
  85. multiboot2 /boot/kernel.elf init=/bin/dragonreach
  86. }'
  87. # 增加insmod efi_gop防止32位uefi启动报错
  88. echo "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh
  89. fi
  90. install_riscv64_efi(){
  91. ${GRUB_PATH_RISCV64_EFI_INSTALL} --target=riscv64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
  92. }
  93. if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then
  94. case "$1" in
  95. --bios)
  96. case "$2" in
  97. uefi) #uefi
  98. if [ ${ARCH} == "i386" ];then
  99. ${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
  100. elif [ ${ARCH} == "x86_64" ];then
  101. ${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
  102. elif [ ${ARCH} == "riscv64" ];then
  103. install_riscv64_efi
  104. else
  105. echo "grub install: 不支持的架构"
  106. fi
  107. ;;
  108. legacy) #传统bios
  109. if [ ${ARCH} == "x86_64" ];then
  110. ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} $LOOP_DEVICE
  111. elif [ ${ARCH} == "riscv64" ];then
  112. install_riscv64_efi
  113. else
  114. echo "grub install: 不支持的架构"
  115. fi
  116. ;;
  117. esac
  118. ;;
  119. *)
  120. #传统bios
  121. ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} $LOOP_DEVICE
  122. ;;
  123. esac
  124. fi
  125. sync
  126. $DADK -w $root_folder rootfs umount || exit 1