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