xref: /DragonOS/tools/write_disk_image.sh (revision 2291ffdece1dc5a703602f79f74df8a4854d215b)
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###############################################
9
10ARCH="x86_64"
11# 内核映像
12root_folder=$(dirname $(pwd))
13kernel="${root_folder}/bin/kernel/kernel.elf"
14boot_folder="${root_folder}/bin/disk_mount/boot"
15mount_folder="${root_folder}/bin/disk_mount"
16ARGS=`getopt -o p -l bios: -- "$@"`
17eval set -- "${ARGS}"
18#echo formatted parameters=[$@]
19echo "开始写入磁盘镜像..."
20
21
22# toolchain
23OS=`uname -s`
24if [ "${OS}" == "Linux" ]; then
25    GRUB_PATH="$(dirname $(which grub-file))"
26elif [ "${OS}" == "Darwin" ]; then
27    GRUB_PATH="${root_folder}/tools/grub-2.06/build/grub/bin"
28fi
29export PATH="${GRUB_PATH}:$PATH"
30
31# ==============检查文件是否齐全================
32
33bins[0]=${kernel}
34
35for file in ${bins[*]};do
36if [ ! -x $file ]; then
37echo "$file 不存在!"
38exit
39fi
40done
41
42# ===============文件检查完毕===================
43
44# 如果是 i386/x86_64,需要判断是否符合 multiboot2 标准
45if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
46    if ${GRUB_PATH}/grub-file --is-x86-multiboot2 ${kernel}; then
47        echo Multiboot2 Confirmed!
48    else
49        echo NOT Multiboot2!
50        exit
51    fi
52fi
53
54# 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建)
55if [ ! -f "${root_folder}/bin/disk.img" ]; then
56    echo "创建硬盘镜像文件..."
57    case "$1" in
58        --bios)
59        case "$2" in
60                uefi)
61            sudo bash ./create_hdd_image.sh -P GPT #GPT分区
62            ;;
63                legacy)
64            sudo bash ./create_hdd_image.sh -P MBR #MBR分区
65            ;;
66            esac
67        ;;
68    *)
69        # 默认创建MBR分区
70        sudo bash ./create_hdd_image.sh -P MBR #MBR分区
71        ;;
72    esac
73fi
74
75# 拷贝程序到硬盘
76mkdir -p ${root_folder}/bin/disk_mount
77bash mount_virt_disk.sh || exit 1
78mkdir -p ${boot_folder}/grub
79cp ${kernel} ${root_folder}/bin/disk_mount/boot
80# 拷贝用户程序到磁盘镜像
81mkdir -p ${root_folder}/bin/disk_mount/bin
82mkdir -p ${root_folder}/bin/disk_mount/dev
83mkdir -p ${root_folder}/bin/disk_mount/proc
84cp -r ${root_folder}/bin/user/* ${root_folder}/bin/disk_mount/bin
85touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
86
87# 设置 grub 相关数据
88if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
89
90    touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
91cfg_content='set timeout=15
92    set default=0
93    menuentry "DragonOS" {
94    multiboot2 /boot/kernel.elf "KERNEL_ELF"
95}'
96echo "echo '${cfg_content}' >  ${boot_folder}/grub/grub.cfg" | sh
97fi
98
99# rm -rf ${iso_folder}
100LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}')
101echo $LOOP_DEVICE
102case "$1" in
103    --bios)
104        case "$2" in
105                uefi) #uefi
106            grub-install --target=x86_64-efi  --efi-directory=${mount_folder}  --boot-directory=${boot_folder}  --removable
107            ;;
108                legacy) #传统bios
109            grub-install --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
110            ;;
111        esac
112        ;;
113    *)
114    echo "参数错误"
115    ;;
116
117esac
118
119sync
120bash umount_virt_disk.sh
121