write_disk_image.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ###############################################
  2. # 该脚本用于将disk_mount目录下的文件写入到disk-${ARCH}.img的第一个分区中,
  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. DISK_NAME=disk-${ARCH}.img
  14. # 内核映像
  15. root_folder=$(dirname $(pwd))
  16. kernel="${root_folder}/bin/kernel/kernel.elf"
  17. boot_folder="${root_folder}/bin/disk_mount/boot"
  18. GRUB_INSTALL_PATH="${boot_folder}/grub"
  19. mount_folder="${root_folder}/bin/disk_mount"
  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. # 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建)
  55. if [ ! -f "${root_folder}/bin/${DISK_NAME}" ]; then
  56. echo "创建硬盘镜像文件..."
  57. case "$1" in
  58. --bios)
  59. case "$2" in
  60. uefi)
  61. sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #GPT分区 用GPT分区uefi启动不了 内核没有针对gpt分区表来做处理
  62. ;;
  63. legacy)
  64. sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区
  65. ;;
  66. esac
  67. ;;
  68. *)
  69. # 默认创建MBR分区
  70. sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区
  71. ;;
  72. esac
  73. fi
  74. # 拷贝程序到硬盘
  75. mkdir -p ${root_folder}/bin/disk_mount
  76. bash mount_virt_disk.sh || exit 1
  77. LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}')
  78. echo $LOOP_DEVICE
  79. # mkdir -p ${GRUB_INSTALL_PATH}
  80. # 检测grub文件夹是否存在
  81. if [ -d "${GRUB_INSTALL_PATH}" ] || [ "${INSTALL_GRUB_TO_IMAGE}" = "0" ]; then
  82. echo "无需安装grub"
  83. INSTALL_GRUB_TO_IMAGE="0"
  84. else
  85. mkdir -p ${GRUB_INSTALL_PATH}
  86. fi
  87. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  88. cp ${kernel} ${root_folder}/bin/disk_mount/boot/
  89. fi
  90. # 拷贝用户程序到磁盘镜像
  91. mkdir -p ${root_folder}/bin/disk_mount/bin
  92. mkdir -p ${root_folder}/bin/disk_mount/dev
  93. mkdir -p ${root_folder}/bin/disk_mount/proc
  94. mkdir -p ${root_folder}/bin/disk_mount/usr
  95. touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
  96. cp -r ${root_folder}/bin/sysroot/* ${root_folder}/bin/disk_mount/
  97. # 设置 grub 相关数据
  98. if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
  99. touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
  100. cfg_content='set timeout=15
  101. set default=0
  102. insmod efi_gop
  103. menuentry "DragonOS" {
  104. multiboot2 /boot/kernel.elf "KERNEL_ELF"
  105. }'
  106. # 增加insmod efi_gop防止32位uefi启动报错
  107. echo "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh
  108. fi
  109. install_riscv64_efi(){
  110. ${GRUB_PATH_RISCV64_EFI_INSTALL} --target=riscv64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
  111. }
  112. if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then
  113. case "$1" in
  114. --bios)
  115. case "$2" in
  116. uefi) #uefi
  117. if [ ${ARCH} == "i386" ];then
  118. ${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
  119. elif [ ${ARCH} == "x86_64" ];then
  120. ${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
  121. elif [ ${ARCH} == "riscv64" ];then
  122. install_riscv64_efi
  123. else
  124. echo "grub install: 不支持的架构"
  125. fi
  126. ;;
  127. legacy) #传统bios
  128. if [ ${ARCH} == "x86_64" ];then
  129. ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
  130. elif [ ${ARCH} == "riscv64" ];then
  131. install_riscv64_efi
  132. else
  133. echo "grub install: 不支持的架构"
  134. fi
  135. ;;
  136. esac
  137. ;;
  138. *)
  139. #传统bios
  140. ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
  141. ;;
  142. esac
  143. fi
  144. sync
  145. bash umount_virt_disk.sh