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