1############################################### 2# 该脚本用于将disk_mount目录下的文件写入到disk.img的第一个分区中, 3# 并在磁盘镜像中安装grub引导程序 4# 5# 用法:bash write_disk_image.sh --bios legacy/uefi 6# 如果之前创建的disk.img是MBR分区表,那么请这样运行它:bash write_disk_image.sh --bios legacy 7# 如果之前创建的disk.img是GPT分区表,那么请这样运行它:bash write_disk_image.sh --bios uefi 8# 通过设置ARCH为x86_64或i386,进行64/32位uefi的install,但是请记住该处的ARCH应与run-qemu.sh中的一致 9############################################### 10 11ARCH="x86_64" 12#ARCH="i386" 13# 内核映像 14root_folder=$(dirname $(pwd)) 15kernel="${root_folder}/bin/kernel/kernel.elf" 16boot_folder="${root_folder}/bin/disk_mount/boot" 17mount_folder="${root_folder}/bin/disk_mount" 18ARGS=`getopt -o p -l bios: -- "$@"` 19eval set -- "${ARGS}" 20#echo formatted parameters=[$@] 21echo "开始写入磁盘镜像..." 22 23 24# toolchain 25 26GRUB_PATH_I386_LEGACY_INSTALL=${root_folder}/tools/arch/i386/legacy/grub/sbin/grub-install 27GRUB_PATH_I386_EFI_INSTALL=${root_folder}/tools/arch/i386/efi/grub/sbin/grub-install 28GRUB_PATH_X86_64_EFI_INSTALL=${root_folder}/tools/arch/x86_64/efi/grub/sbin/grub-install 29 30GRUB_PATH_I386_LEGACY_FILE=${root_folder}/tools/arch/i386/legacy/grub/bin/grub-file 31 32 33# ==============检查文件是否齐全================ 34 35bins[0]=${kernel} 36 37for file in ${bins[*]};do 38if [ ! -x $file ]; then 39echo "$file 不存在!" 40exit 41fi 42done 43 44# ===============文件检查完毕=================== 45 46# 如果是 i386/x86_64,需要判断是否符合 multiboot2 标准 47if [ ${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 54fi 55 56# 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建) 57if [ ! -f "${root_folder}/bin/disk.img" ]; then 58 echo "创建硬盘镜像文件..." 59 case "$1" in 60 --bios) 61 case "$2" in 62 uefi) 63 sudo bash ./create_hdd_image.sh -P GPT #GPT分区 64 ;; 65 legacy) 66 sudo bash ./create_hdd_image.sh -P MBR #MBR分区 67 ;; 68 esac 69 ;; 70 *) 71 # 默认创建MBR分区 72 sudo bash ./create_hdd_image.sh -P MBR #MBR分区 73 ;; 74 esac 75fi 76 77# 拷贝程序到硬盘 78mkdir -p ${root_folder}/bin/disk_mount 79bash mount_virt_disk.sh || exit 1 80mkdir -p ${boot_folder}/grub 81cp ${kernel} ${root_folder}/bin/disk_mount/boot 82# 拷贝用户程序到磁盘镜像 83mkdir -p ${root_folder}/bin/disk_mount/bin 84mkdir -p ${root_folder}/bin/disk_mount/dev 85mkdir -p ${root_folder}/bin/disk_mount/proc 86cp -r ${root_folder}/bin/user/* ${root_folder}/bin/disk_mount/bin 87touch ${root_folder}/bin/disk_mount/dev/keyboard.dev 88 89# 设置 grub 相关数据 90if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then 91 92 touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg 93cfg_content='set timeout=15 94 set default=0 95 insmod efi_gop 96 menuentry "DragonOS" { 97 multiboot2 /boot/kernel.elf "KERNEL_ELF" 98}' 99# 增加insmod efi_gop防止32位uefi启动报错 100echo "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh 101fi 102 103# rm -rf ${iso_folder} 104LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}') 105echo $LOOP_DEVICE 106case "$1" in 107 --bios) 108 case "$2" in 109 uefi) #uefi 110 if [ ${ARCH} == "i386" ];then 111 ${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 112 elif [ ${ARCH} == "x86_64" ];then 113 ${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 114 fi 115 ;; 116 legacy) #传统bios 117 ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE 118 ;; 119 esac 120 ;; 121 *) 122 echo "参数错误" 123 ;; 124 125esac 126 127sync 128bash umount_virt_disk.sh 129