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
25GRUB_ABS_PREFIX=/opt/dragonos-grub
26GRUB_PATH_I386_LEGACY_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/sbin/grub-install
27GRUB_PATH_I386_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/efi/grub/sbin/grub-install
28GRUB_PATH_X86_64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/x86_64/efi/grub/sbin/grub-install
29
30GRUB_PATH_I386_LEGACY_FILE=${GRUB_ABS_PREFIX}/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
80
81LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}')
82echo $LOOP_DEVICE
83
84mkdir -p ${boot_folder}/grub
85cp ${kernel} ${root_folder}/bin/disk_mount/boot
86# 拷贝用户程序到磁盘镜像
87mkdir -p ${root_folder}/bin/disk_mount/bin
88mkdir -p ${root_folder}/bin/disk_mount/dev
89mkdir -p ${root_folder}/bin/disk_mount/proc
90mkdir -p ${root_folder}/bin/disk_mount/usr
91cp -r ${root_folder}/bin/user/* ${root_folder}/bin/disk_mount/bin
92touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
93cp -r ${root_folder}/bin/sysroot/usr/* ${root_folder}/bin/disk_mount/usr/
94
95# 设置 grub 相关数据
96if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
97
98    touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
99cfg_content='set timeout=15
100    set default=0
101    insmod efi_gop
102    menuentry "DragonOS" {
103    multiboot2 /boot/kernel.elf "KERNEL_ELF"
104}'
105
106# 增加insmod efi_gop防止32位uefi启动报错
107echo "echo '${cfg_content}' >  ${boot_folder}/grub/grub.cfg" | sh
108fi
109
110case "$1" in
111    --bios)
112        case "$2" in
113                uefi) #uefi
114                if [ ${ARCH} == "i386" ];then
115                	${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi  --efi-directory=${mount_folder}  --boot-directory=${boot_folder}  --removable
116                elif [ ${ARCH} == "x86_64" ];then
117                	${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder}  --boot-directory=${boot_folder}   --removable
118                fi
119            ;;
120                legacy) #传统bios
121            		${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
122            ;;
123        esac
124        ;;
125    *)
126    #传统bios
127    ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
128    ;;
129
130esac
131
132sync
133bash umount_virt_disk.sh
134