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 6 7sudo mkdir -p ${grub_dir_i386_efi} 8sudo mkdir -p ${grub_dir_i386_legacy} 9sudo mkdir -p ${grub_dir_x86_64_efi} 10 11# 防止外层声明了环境变量,影响到grub的编译 12export CC=gcc 13export LD=ld 14export AS=as 15export NM=nm 16export OBJCOPY=objcopy 17 18 19#检测grub是否已经安装 20if [ -d ${grub_dir_i386_efi}/bin ] && [ -d ${grub_dir_i386_legacy}/bin ] && [ -d ${grub_dir_x86_64_efi}/bin ] ; then 21 exit 0 22fi 23#仅支持Ubuntu/Debain, Arch下的自动安装 24supported_package_manager="apt-get pacman" 25packages=("make binutils bison gcc gettext flex bison automake autoconf wget" \ 26 "make binutils bison gcc gettext flex bison automake autoconf wget") 27update_options=("update" \ 28 "-Sy") 29install_options=("install -y" \ 30 "-S --needed --noconfirm") 31found_pm=0 32pm_index=0 33for pm in ${supported_package_manager}; do 34 if hash 2>/dev/null ${pm}; then 35 found_pm=1 36 break 37 fi 38 let pm_index=$pm_index+1 39done 40if [ ${found_pm} = "1" ]; then 41 echo "found package manager: ${pm}" 42else 43 echo "找不到任何支持的包管理器: ${supported_package_manager}" 44 echo "脚本暂不支持对该系统下grub的安装,请手动完成" 45 exit 0 46fi 47 48#下载grub2.06 49if [ ! -f "grub-2.06.tar.xz" ]; then 50 echo "开始下载grub2.06" 51 wget https://mirrors.ustc.edu.cn/gnu/grub/grub-2.06.tar.xz || exit 1 52 echo "下载完成" 53fi 54 55tar xvf grub-2.06.tar.xz 56#安装对应依赖 57sudo ${pm} ${update_options[$pm_index]} 58sudo ${pm} ${install_options[$pm_index]} ${packages[$pm_index]} 59 60cd grub-2.06 61echo "开始安装grub2.06" 62#编译安装三个版本的grub 63./configure --target=i386 --prefix=${grub_dir_i386_legacy} --disable-werror || exit 1 64make -j $(nproc) || exit 1 65sudo make install || exit 1 66make clean || exit 1 67 68./configure --target=i386 --with-platform=efi --prefix=${grub_dir_i386_efi} --disable-werror || exit 1 69make -j $(nproc) || exit 1 70sudo make install || exit 1 71make clean || exit 1 72 73./configure --target=x86_64 --with-platform=efi --prefix=${grub_dir_x86_64_efi} --disable-werror || exit 1 74make -j $(nproc) || exit 1 75sudo make install || exit 1 76 77cd .. 78#解除权限限制 79sudo chmod -R 777 ${grub_dir_i386_legacy} 80sudo chmod -R 777 ${grub_dir_i386_efi} 81sudo chmod -R 777 ${grub_dir_x86_64_efi} 82rm -rf grub-2.06 83rm grub-2.06.tar.xz* 84echo "grub2.06安装完成" 85