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 11echo "ARCH=${ARCH}" 12# 给ARCH变量赋默认值 13export ARCH=${ARCH:=x86_64} 14 15DISK_NAME=disk-${ARCH}.img 16 17# 内核映像 18root_folder=$(dirname $(pwd)) 19kernel="${root_folder}/bin/kernel/kernel.elf" 20boot_folder="${root_folder}/bin/disk_mount/boot" 21GRUB_INSTALL_PATH="${boot_folder}/grub" 22mount_folder="${root_folder}/bin/disk_mount" 23ARGS=`getopt -o p -l bios: -- "$@"` 24eval set -- "${ARGS}" 25#echo formatted parameters=[$@] 26echo "开始写入磁盘镜像..." 27 28INSTALL_GRUB_TO_IMAGE="1" 29 30# toolchain 31GRUB_ABS_PREFIX=/opt/dragonos-grub 32GRUB_PATH_I386_LEGACY_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/sbin/grub-install 33GRUB_PATH_I386_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/efi/grub/sbin/grub-install 34GRUB_PATH_X86_64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/x86_64/efi/grub/sbin/grub-install 35GRUB_PATH_RISCV64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/riscv64/efi/grub/sbin/grub-install 36 37GRUB_PATH_I386_LEGACY_FILE=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/bin/grub-file 38 39 40# ==============检查文件是否齐全================ 41 42bins[0]=${kernel} 43 44for file in ${bins[*]};do 45if [ ! -x $file ]; then 46echo "$file 不存在!" 47exit 48fi 49done 50 51# ===============文件检查完毕=================== 52 53# 如果是 i386/x86_64,需要判断是否符合 multiboot2 标准 54if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then 55 if ${GRUB_PATH_I386_LEGACY_FILE} --is-x86-multiboot2 ${kernel}; then 56 echo Multiboot2 Confirmed! 57 else 58 echo NOT Multiboot2! 59 exit 60 fi 61fi 62 63# 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建) 64if [ ! -f "${root_folder}/bin/${DISK_NAME}" ]; then 65 echo "创建硬盘镜像文件..." 66 case "$1" in 67 --bios) 68 case "$2" in 69 uefi) 70 sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #GPT分区 71 ;; 72 legacy) 73 sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区 74 ;; 75 esac 76 ;; 77 *) 78 # 默认创建MBR分区 79 sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区 80 ;; 81 esac 82fi 83 84# 拷贝程序到硬盘 85mkdir -p ${root_folder}/bin/disk_mount 86bash mount_virt_disk.sh || exit 1 87 88LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}') 89echo $LOOP_DEVICE 90 91# mkdir -p ${GRUB_INSTALL_PATH} 92 93# 检测grub文件夹是否存在 94if [ -d "${GRUB_INSTALL_PATH}" ]; then 95 echo "grub已安装" 96 INSTALL_GRUB_TO_IMAGE="0" 97else 98 mkdir -p ${GRUB_INSTALL_PATH} 99fi 100 101cp ${kernel} ${root_folder}/bin/disk_mount/boot 102# 拷贝用户程序到磁盘镜像 103mkdir -p ${root_folder}/bin/disk_mount/bin 104mkdir -p ${root_folder}/bin/disk_mount/dev 105mkdir -p ${root_folder}/bin/disk_mount/proc 106mkdir -p ${root_folder}/bin/disk_mount/usr 107cp -r ${root_folder}/bin/user/* ${root_folder}/bin/disk_mount/bin 108touch ${root_folder}/bin/disk_mount/dev/keyboard.dev 109cp -r ${root_folder}/bin/sysroot/* ${root_folder}/bin/disk_mount/ 110 111# 设置 grub 相关数据 112if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then 113 114 touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg 115cfg_content='set timeout=15 116 set default=0 117 insmod efi_gop 118 menuentry "DragonOS" { 119 multiboot2 /boot/kernel.elf "KERNEL_ELF" 120}' 121 122# 增加insmod efi_gop防止32位uefi启动报错 123echo "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh 124fi 125 126install_riscv64_efi(){ 127 ${GRUB_PATH_RISCV64_EFI_INSTALL} --target=riscv64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 128} 129 130if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then 131 132 case "$1" in 133 --bios) 134 case "$2" in 135 uefi) #uefi 136 if [ ${ARCH} == "i386" ];then 137 ${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 138 elif [ ${ARCH} == "x86_64" ];then 139 ${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable 140 elif [ ${ARCH} == "riscv64" ];then 141 install_riscv64_efi 142 else 143 echo "grub install: 不支持的架构" 144 fi 145 ;; 146 legacy) #传统bios 147 if [ ${ARCH} == "x86_64" ];then 148 ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE 149 elif [ ${ARCH} == "riscv64" ];then 150 install_riscv64_efi 151 else 152 echo "grub install: 不支持的架构" 153 fi 154 ;; 155 esac 156 ;; 157 *) 158 #传统bios 159 ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE 160 ;; 161 162 esac 163fi 164 165sync 166bash umount_virt_disk.sh 167