17f439c5dSYJwu2023############################################### 2c7508928SLoGin# 该脚本用于将disk_mount目录下的文件写入到disk-${ARCH}.img的第一个分区中, 37f439c5dSYJwu2023# 并在磁盘镜像中安装grub引导程序 47f439c5dSYJwu2023# 57f439c5dSYJwu2023# 用法:bash write_disk_image.sh --bios legacy/uefi 6c7508928SLoGin# 如果之前创建的 disk-${ARCH}.img 是MBR分区表,那么请这样运行它:bash write_disk_image.sh --bios legacy 7c7508928SLoGin# 如果之前创建的 disk-${ARCH}.img 是GPT分区表,那么请这样运行它:bash write_disk_image.sh --bios uefi 8c7508928SLoGin# 通过设置ARCH为x86_64/i386/riscv64,进行64/32位uefi的install,但是请记住该处的ARCH应与run-qemu.sh中的一致 97f439c5dSYJwu2023############################################### 107f439c5dSYJwu2023 11c7508928SLoGinecho "ARCH=${ARCH}" 12c7508928SLoGin# 给ARCH变量赋默认值 13c7508928SLoGinexport ARCH=${ARCH:=x86_64} 14c7508928SLoGin 15c7508928SLoGinDISK_NAME=disk-${ARCH}.img 16c7508928SLoGin 172813126eSlogin# 内核映像 182813126eSloginroot_folder=$(dirname $(pwd)) 192813126eSloginkernel="${root_folder}/bin/kernel/kernel.elf" 202813126eSloginboot_folder="${root_folder}/bin/disk_mount/boot" 21485e2487SLoGinGRUB_INSTALL_PATH="${boot_folder}/grub" 227f439c5dSYJwu2023mount_folder="${root_folder}/bin/disk_mount" 237f439c5dSYJwu2023ARGS=`getopt -o p -l bios: -- "$@"` 247f439c5dSYJwu2023eval set -- "${ARGS}" 257f439c5dSYJwu2023#echo formatted parameters=[$@] 262813126eSloginecho "开始写入磁盘镜像..." 272813126eSlogin 2801090de7SLoGinif [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then 2901090de7SLoGin 30485e2487SLoGinINSTALL_GRUB_TO_IMAGE="1" 312813126eSlogin 3201090de7SLoGinelse 3301090de7SLoGinINSTALL_GRUB_TO_IMAGE="0" 3401090de7SLoGinfi 3501090de7SLoGin 3601090de7SLoGin 372813126eSlogin# toolchain 385ed4cd46SguanjinquanGRUB_ABS_PREFIX=/opt/dragonos-grub 395ed4cd46SguanjinquanGRUB_PATH_I386_LEGACY_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/sbin/grub-install 405ed4cd46SguanjinquanGRUB_PATH_I386_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/efi/grub/sbin/grub-install 415ed4cd46SguanjinquanGRUB_PATH_X86_64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/x86_64/efi/grub/sbin/grub-install 42c7508928SLoGinGRUB_PATH_RISCV64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/riscv64/efi/grub/sbin/grub-install 43d02e6ea4Slogin 445ed4cd46SguanjinquanGRUB_PATH_I386_LEGACY_FILE=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/bin/grub-file 45d02e6ea4Slogin 462813126eSlogin 472813126eSlogin# ==============检查文件是否齐全================ 482813126eSlogin 492813126eSloginbins[0]=${kernel} 502813126eSlogin 512813126eSloginfor file in ${bins[*]};do 522813126eSloginif [ ! -x $file ]; then 532813126eSloginecho "$file 不存在!" 547f439c5dSYJwu2023exit 552813126eSloginfi 562813126eSlogindone 572813126eSlogin 582813126eSlogin# ===============文件检查完毕=================== 592813126eSlogin 602813126eSlogin# 如果是 i386/x86_64,需要判断是否符合 multiboot2 标准 612813126eSloginif [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then 62d02e6ea4Slogin if ${GRUB_PATH_I386_LEGACY_FILE} --is-x86-multiboot2 ${kernel}; then 632813126eSlogin echo Multiboot2 Confirmed! 642813126eSlogin else 652813126eSlogin echo NOT Multiboot2! 662813126eSlogin exit 672813126eSlogin fi 682813126eSloginfi 692813126eSlogin 702813126eSlogin# 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建) 71c7508928SLoGinif [ ! -f "${root_folder}/bin/${DISK_NAME}" ]; then 722813126eSlogin echo "创建硬盘镜像文件..." 737f439c5dSYJwu2023 case "$1" in 747f439c5dSYJwu2023 --bios) 757f439c5dSYJwu2023 case "$2" in 767f439c5dSYJwu2023 uefi) 77d46c6d27S裕依2439 sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #GPT分区 用GPT分区uefi启动不了 内核没有针对gpt分区表来做处理 787f439c5dSYJwu2023 ;; 797f439c5dSYJwu2023 legacy) 80c7508928SLoGin sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区 817f439c5dSYJwu2023 ;; 827f439c5dSYJwu2023 esac 837f439c5dSYJwu2023 ;; 847f439c5dSYJwu2023 *) 857f439c5dSYJwu2023 # 默认创建MBR分区 86c7508928SLoGin sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区 877f439c5dSYJwu2023 ;; 887f439c5dSYJwu2023 esac 892813126eSloginfi 902813126eSlogin 917f439c5dSYJwu2023# 拷贝程序到硬盘 922813126eSloginmkdir -p ${root_folder}/bin/disk_mount 932813126eSloginbash mount_virt_disk.sh || exit 1 945ed4cd46Sguanjinquan 955ed4cd46SguanjinquanLOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}') 965ed4cd46Sguanjinquanecho $LOOP_DEVICE 975ed4cd46Sguanjinquan 98485e2487SLoGin# mkdir -p ${GRUB_INSTALL_PATH} 99485e2487SLoGin 100485e2487SLoGin# 检测grub文件夹是否存在 10101090de7SLoGinif [ -d "${GRUB_INSTALL_PATH}" ] || [ "${INSTALL_GRUB_TO_IMAGE}" = "0" ]; then 10201090de7SLoGin echo "无需安装grub" 103485e2487SLoGin INSTALL_GRUB_TO_IMAGE="0" 104485e2487SLoGinelse 105485e2487SLoGin mkdir -p ${GRUB_INSTALL_PATH} 106cf442324SLoGinfi 107cf442324SLoGin 108cf442324SLoGin 109cf442324SLoGinif [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then 11001090de7SLoGin cp ${kernel} ${root_folder}/bin/disk_mount/boot/ 111485e2487SLoGinfi 112485e2487SLoGin 1132813126eSlogin# 拷贝用户程序到磁盘镜像 1142813126eSloginmkdir -p ${root_folder}/bin/disk_mount/bin 1152813126eSloginmkdir -p ${root_folder}/bin/disk_mount/dev 11627a97abdSDaJiYuQiamkdir -p ${root_folder}/bin/disk_mount/proc 1172224c93eSguanjinquanmkdir -p ${root_folder}/bin/disk_mount/usr 1182813126eSlogintouch ${root_folder}/bin/disk_mount/dev/keyboard.dev 1197285c927Slogincp -r ${root_folder}/bin/sysroot/* ${root_folder}/bin/disk_mount/ 1202813126eSlogin 1212813126eSlogin# 设置 grub 相关数据 1222813126eSloginif [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then 1232813126eSlogin 1242813126eSlogin touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg 1252813126eSlogincfg_content='set timeout=15 1262813126eSlogin set default=0 12738b341b8SYJwu2023 insmod efi_gop 1282813126eSlogin menuentry "DragonOS" { 129*f9fe30beSLoGin multiboot2 /boot/kernel.elf init=/bin/dragonreach 1302813126eSlogin}' 13138b341b8SYJwu2023# 增加insmod efi_gop防止32位uefi启动报错 1322813126eSloginecho "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh 1332813126eSloginfi 1342813126eSlogin 135c7508928SLoGininstall_riscv64_efi(){ 136c7508928SLoGin ${GRUB_PATH_RISCV64_EFI_INSTALL} --target=riscv64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 137c7508928SLoGin} 138c7508928SLoGin 139485e2487SLoGinif [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then 140485e2487SLoGin 1417f439c5dSYJwu2023 case "$1" in 1427f439c5dSYJwu2023 --bios) 1437f439c5dSYJwu2023 case "$2" in 1447f439c5dSYJwu2023 uefi) #uefi 14538b341b8SYJwu2023 if [ ${ARCH} == "i386" ];then 146d02e6ea4Slogin ${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 14738b341b8SYJwu2023 elif [ ${ARCH} == "x86_64" ];then 148d02e6ea4Slogin ${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 149c7508928SLoGin elif [ ${ARCH} == "riscv64" ];then 150c7508928SLoGin install_riscv64_efi 151c7508928SLoGin else 152c7508928SLoGin echo "grub install: 不支持的架构" 15338b341b8SYJwu2023 fi 1547f439c5dSYJwu2023 ;; 1557f439c5dSYJwu2023 legacy) #传统bios 156c7508928SLoGin if [ ${ARCH} == "x86_64" ];then 157d02e6ea4Slogin ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE 158c7508928SLoGin elif [ ${ARCH} == "riscv64" ];then 159c7508928SLoGin install_riscv64_efi 160c7508928SLoGin else 161c7508928SLoGin echo "grub install: 不支持的架构" 162c7508928SLoGin fi 1637f439c5dSYJwu2023 ;; 1647f439c5dSYJwu2023 esac 1657f439c5dSYJwu2023 ;; 1667f439c5dSYJwu2023 *) 1672224c93eSguanjinquan #传统bios 1682224c93eSguanjinquan ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE 1697f439c5dSYJwu2023 ;; 1702813126eSlogin 1717f439c5dSYJwu2023 esac 172485e2487SLoGinfi 1732813126eSlogin 1742813126eSloginsync 1752813126eSloginbash umount_virt_disk.sh 176