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