xref: /DragonOS/tools/write_disk_image.sh (revision fae6e9ade46a52976ad5d099643d51cc20876448)
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分区    用GPT分区uefi启动不了 内核没有针对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}
106fi
107
108
109if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
110    cp ${kernel} ${root_folder}/bin/disk_mount/boot/
111fi
112
113# 拷贝用户程序到磁盘镜像
114mkdir -p ${root_folder}/bin/disk_mount/bin
115mkdir -p ${root_folder}/bin/disk_mount/dev
116mkdir -p ${root_folder}/bin/disk_mount/proc
117mkdir -p ${root_folder}/bin/disk_mount/usr
118touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
119cp -r ${root_folder}/bin/sysroot/* ${root_folder}/bin/disk_mount/
120
121# 设置 grub 相关数据
122if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
123
124    touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
125cfg_content='set timeout=15
126    set default=0
127    insmod efi_gop
128    menuentry "DragonOS" {
129    multiboot2 /boot/kernel.elf init=/bin/dragonreach
130}'
131# 增加insmod efi_gop防止32位uefi启动报错
132echo "echo '${cfg_content}' >  ${boot_folder}/grub/grub.cfg" | sh
133fi
134
135install_riscv64_efi(){
136    ${GRUB_PATH_RISCV64_EFI_INSTALL} --target=riscv64-efi --efi-directory=${mount_folder}  --boot-directory=${boot_folder}  --removable
137}
138
139if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then
140
141    case "$1" in
142        --bios)
143            case "$2" in
144                    uefi) #uefi
145                    if [ ${ARCH} == "i386" ];then
146                        ${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi  --efi-directory=${mount_folder}  --boot-directory=${boot_folder}  --removable
147                    elif [ ${ARCH} == "x86_64" ];then
148                        ${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder}  --boot-directory=${boot_folder}   --removable
149                    elif [ ${ARCH} == "riscv64" ];then
150                        install_riscv64_efi
151                    else
152                        echo "grub install: 不支持的架构"
153                    fi
154                ;;
155                    legacy) #传统bios
156                    if [ ${ARCH} == "x86_64" ];then
157                        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
158                    elif [ ${ARCH} == "riscv64" ];then
159                        install_riscv64_efi
160                    else
161                        echo "grub install: 不支持的架构"
162                    fi
163                ;;
164            esac
165            ;;
166        *)
167        #传统bios
168        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
169        ;;
170
171    esac
172fi
173
174sync
175bash umount_virt_disk.sh
176