xref: /DragonOS/tools/write_disk_image.sh (revision 9284987850c1da2ce607a539eeae8a353a8f6df9)
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
118cp -r ${root_folder}/bin/user/* ${root_folder}/bin/disk_mount/bin
119touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
120cp -r ${root_folder}/bin/sysroot/* ${root_folder}/bin/disk_mount/
121
122# 设置 grub 相关数据
123if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
124
125    touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
126cfg_content='set timeout=15
127    set default=0
128    insmod efi_gop
129    menuentry "DragonOS" {
130    multiboot2 /boot/kernel.elf "KERNEL_ELF"
131}'
132
133# 增加insmod efi_gop防止32位uefi启动报错
134echo "echo '${cfg_content}' >  ${boot_folder}/grub/grub.cfg" | sh
135fi
136
137install_riscv64_efi(){
138    ${GRUB_PATH_RISCV64_EFI_INSTALL} --target=riscv64-efi --efi-directory=${mount_folder}  --boot-directory=${boot_folder}  --removable
139}
140
141if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then
142
143    case "$1" in
144        --bios)
145            case "$2" in
146                    uefi) #uefi
147                    if [ ${ARCH} == "i386" ];then
148                        ${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi  --efi-directory=${mount_folder}  --boot-directory=${boot_folder}  --removable
149                    elif [ ${ARCH} == "x86_64" ];then
150                        ${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder}  --boot-directory=${boot_folder}   --removable
151                    elif [ ${ARCH} == "riscv64" ];then
152                        install_riscv64_efi
153                    else
154                        echo "grub install: 不支持的架构"
155                    fi
156                ;;
157                    legacy) #传统bios
158                    if [ ${ARCH} == "x86_64" ];then
159                        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
160                    elif [ ${ARCH} == "riscv64" ];then
161                        install_riscv64_efi
162                    else
163                        echo "grub install: 不支持的架构"
164                    fi
165                ;;
166            esac
167            ;;
168        *)
169        #传统bios
170        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
171        ;;
172
173    esac
174fi
175
176sync
177bash umount_virt_disk.sh
178