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