xref: /DragonOS/tools/grub_auto_install.sh (revision fae6e9ade46a52976ad5d099643d51cc20876448)
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, Centos/RHEL8/Fedora gentoo下的自动安装
42supported_package_manager="apt-get pacman dnf yum emerge"
43packages=("make binutils bison gcc gettext flex bison automake autoconf wget gawk" \
44              "make binutils bison gcc gettext flex bison automake autoconf wget gawk" \
45	      "make binutils bison gcc gettext flex bison automake autoconf wget gawk" \
46	      "make binutils bison gcc gettext flex bison automake autoconf wget gawk" \
47	      "dev-build/make sys-devel/binutils sys-devel/bison sys-devel/gcc sys-devel/gettext sys-devel/flex dev-build/automake dev-build/autoconf net-misc/wget sys-apps/gawk")
48update_options=("update" \
49                    "-Sy" \
50		    "update" \
51		    "update" \
52		    "--sync"
53	       )
54install_options=("install -y" \
55                     "-S --needed --noconfirm" \
56		     "install -y" \
57		     "install -y" \
58		     ""
59		)
60found_pm=0
61pm_index=0
62for pm in ${supported_package_manager}; do
63    if hash 2>/dev/null ${pm}; then
64        found_pm=1
65        break
66    fi
67    let pm_index=$pm_index+1
68done
69if [ ${found_pm} = "1" ]; then
70	echo "found package manager: ${pm}"
71else
72	echo "找不到任何支持的包管理器: ${supported_package_manager}"
73	echo "脚本暂不支持对该系统下grub的安装,请手动完成"
74	exit 0
75fi
76
77#下载grub2.06
78if [ ! -f "grub-2.06.tar.xz" ]; then
79    echo "开始下载grub2.06"
80    wget https://mirrors.ustc.edu.cn/gnu/grub/grub-2.06.tar.xz || exit 1
81    echo "下载完成"
82fi
83
84tar xvf grub-2.06.tar.xz
85#安装对应依赖
86sudo ${pm} ${update_options[$pm_index]}
87sudo ${pm} ${install_options[$pm_index]} ${packages[$pm_index]}
88
89cd grub-2.06
90echo "开始安装grub2.06"
91#编译安装三个版本的grub
92
93# 如果不存在i386_legacy的grub,就编译安装
94if [ ! -d ${grub_dir_i386_legacy}/bin ]; then
95    echo "开始编译安装i386_legacy版本的grub"
96    ./configure --target=i386 --prefix=${grub_dir_i386_legacy} --disable-werror || exit 1
97    make -j $(nproc) || exit 1
98    sudo make install || exit 1
99    make clean || exit 1
100    sudo chmod -R 777 ${grub_dir_i386_legacy}
101fi
102
103# 如果不存在i386_efi的grub,就编译安装
104if [ ! -d ${grub_dir_i386_efi}/bin ]; then
105    echo "开始编译安装i386_efi版本的grub"
106    ./configure --target=i386 --with-platform=efi --prefix=${grub_dir_i386_efi} --disable-werror ||	exit 1
107    make -j $(nproc) || exit 1
108    sudo make install || exit 1
109    make clean || exit 1
110    sudo chmod -R 777 ${grub_dir_i386_efi}
111fi
112
113# 如果不存在x86_64_efi的grub,就编译安装
114if [ ! -d ${grub_dir_x86_64_efi}/bin ]; then
115    echo "开始编译安装x86_64_efi版本的grub"
116    ./configure --target=x86_64 --with-platform=efi --prefix=${grub_dir_x86_64_efi} --disable-werror || exit 1
117    make -j $(nproc) || exit 1
118    sudo make install || exit 1
119    make clean || exit 1
120    sudo chmod -R 777 ${grub_dir_x86_64_efi}
121fi
122
123# 如果不存在riscv64_efi的grub,就编译安装
124# riscv64目前使用DragonStub进行启动,不需要grub
125# if [ ! -d ${grub_dir_riscv64_efi}/bin ]; then
126#     echo "开始编译安装riscv64_efi版本的grub"
127#     # 使用which命令判断,如果不存在riscv64-linux-musl交叉编译工具链,则报错
128#     if [ ! -n "$(which riscv64-linux-musl-gcc)" ]; then
129#         echo "riscv64-linux-musl-gcc不存在,请先安装riscv64-linux-musl交叉编译工具链"
130#         exit 1
131#     fi
132
133#     ./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
134#     make -j $(nproc) || exit 1
135#     sudo make install || exit 1
136#     make clean || exit 1
137#     sudo chmod -R 777 ${grub_dir_riscv64_efi}
138# fi
139
140
141cd ..
142
143rm -rf grub-2.06
144rm grub-2.06.tar.xz*
145echo "grub2.06安装完成"
146