write_disk_image.sh 5.3 KB

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