xref: /DragonOS/tools/grub_auto_install.sh (revision da152319797436368304cbc3f85a3b9ec049134b)
1#!/bin/bash
2
3if test -n "$ZSH_VERSION"; then
4  CURRENT_SHELL=zsh
5elif test -n "$BASH_VERSION"; then
6  CURRENT_SHELL=bash
7elif test -n "$KSH_VERSION"; then
8  CURRENT_SHELL=ksh
9elif test -n "$FCEDIT"; then
10  CURRENT_SHELL=ksh
11elif test -n "$PS3"; then
12  CURRENT_SHELL=unknown
13else
14  CURRENT_SHELL=sh
15fi
16
17source "$HOME/.$CURRENT_SHELL"rc
18
19ABS_PREFIX=/opt/dragonos-grub
20grub_dir_i386_efi=${ABS_PREFIX}/arch/i386/efi/grub
21grub_dir_i386_legacy=${ABS_PREFIX}/arch/i386/legacy/grub
22grub_dir_x86_64_efi=${ABS_PREFIX}/arch/x86_64/efi/grub
23grub_dir_riscv64_efi=${ABS_PREFIX}/arch/riscv64/efi/grub
24
25sudo mkdir -p ${grub_dir_i386_efi}
26sudo mkdir -p ${grub_dir_i386_legacy}
27sudo mkdir -p ${grub_dir_x86_64_efi}
28
29# 防止外层声明了环境变量,影响到grub的编译
30export CC=gcc
31export LD=ld
32export AS=as
33export NM=nm
34export OBJCOPY=objcopy
35
36
37#检测grub是否已经安装
38if [ -d ${grub_dir_i386_efi}/bin ] && [ -d ${grub_dir_i386_legacy}/bin ] && [ -d ${grub_dir_x86_64_efi}/bin ] ; then
39	exit 0
40fi
41#仅支持Ubuntu/Debain, Arch下的自动安装
42supported_package_manager="apt-get pacman"
43packages=("make binutils bison gcc gettext flex bison automake autoconf wget gawk" \
44          "make binutils bison gcc gettext flex bison automake autoconf wget gawk")
45update_options=("update" \
46                "-Sy")
47install_options=("install -y" \
48                 "-S --needed --noconfirm")
49found_pm=0
50pm_index=0
51for pm in ${supported_package_manager}; do
52    if hash 2>/dev/null ${pm}; then
53        found_pm=1
54        break
55    fi
56    let pm_index=$pm_index+1
57done
58if [ ${found_pm} = "1" ]; then
59	echo "found package manager: ${pm}"
60else
61	echo "找不到任何支持的包管理器: ${supported_package_manager}"
62	echo "脚本暂不支持对该系统下grub的安装,请手动完成"
63	exit 0
64fi
65
66#下载grub2.06
67if [ ! -f "grub-2.06.tar.xz" ]; then
68    echo "开始下载grub2.06"
69    wget https://mirrors.ustc.edu.cn/gnu/grub/grub-2.06.tar.xz || exit 1
70    echo "下载完成"
71fi
72
73tar xvf grub-2.06.tar.xz
74#安装对应依赖
75sudo ${pm} ${update_options[$pm_index]}
76sudo ${pm} ${install_options[$pm_index]} ${packages[$pm_index]}
77
78cd grub-2.06
79echo "开始安装grub2.06"
80#编译安装三个版本的grub
81
82# 如果不存在i386_legacy的grub,就编译安装
83if [ ! -d ${grub_dir_i386_legacy}/bin ]; then
84    echo "开始编译安装i386_legacy版本的grub"
85    ./configure --target=i386 --prefix=${grub_dir_i386_legacy} --disable-werror || exit 1
86    make -j $(nproc) || exit 1
87    sudo make install || exit 1
88    make clean || exit 1
89    sudo chmod -R 777 ${grub_dir_i386_legacy}
90fi
91
92# 如果不存在i386_efi的grub,就编译安装
93if [ ! -d ${grub_dir_i386_efi}/bin ]; then
94    echo "开始编译安装i386_efi版本的grub"
95    ./configure --target=i386 --with-platform=efi --prefix=${grub_dir_i386_efi} --disable-werror ||	exit 1
96    make -j $(nproc) || exit 1
97    sudo make install || exit 1
98    make clean || exit 1
99    sudo chmod -R 777 ${grub_dir_i386_efi}
100fi
101
102# 如果不存在x86_64_efi的grub,就编译安装
103if [ ! -d ${grub_dir_x86_64_efi}/bin ]; then
104    echo "开始编译安装x86_64_efi版本的grub"
105    ./configure --target=x86_64 --with-platform=efi --prefix=${grub_dir_x86_64_efi} --disable-werror || exit 1
106    make -j $(nproc) || exit 1
107    sudo make install || exit 1
108    make clean || exit 1
109    sudo chmod -R 777 ${grub_dir_x86_64_efi}
110fi
111
112# 如果不存在riscv64_efi的grub,就编译安装
113# riscv64目前使用DragonStub进行启动,不需要grub
114# if [ ! -d ${grub_dir_riscv64_efi}/bin ]; then
115#     echo "开始编译安装riscv64_efi版本的grub"
116#     # 使用which命令判断,如果不存在riscv64-linux-musl交叉编译工具链,则报错
117#     if [ ! -n "$(which riscv64-linux-musl-gcc)" ]; then
118#         echo "riscv64-linux-musl-gcc不存在,请先安装riscv64-linux-musl交叉编译工具链"
119#         exit 1
120#     fi
121
122#     ./configure --target=riscv64 --with-platform=efi --prefix=${grub_dir_riscv64_efi} --host=x86_64-linux-gnu  --disable-werror  BUILD_CC=gcc HOST_CC=x86_64-linux-gnu-gcc  TARGET_CC=riscv64-linux-musl-gcc TARGET_OBJCOPY=riscv64-linux-musl-objcopy TARGET_STRIP=riscv64-linux-musl-strip TARGET_RANLIB=riscv64-linux-musl-ranlib TARGET_NM=riscv64-linux-musl-nm TARGET_LD=riscv64-linux-musl-ld
123#     make -j $(nproc) || exit 1
124#     sudo make install || exit 1
125#     make clean || exit 1
126#     sudo chmod -R 777 ${grub_dir_riscv64_efi}
127# fi
128
129
130cd ..
131
132rm -rf grub-2.06
133rm grub-2.06.tar.xz*
134echo "grub2.06安装完成"
135