xref: /DragonOS/tools/grub_auto_install.sh (revision b66beefd4e9ead61ee55f335246ebeb8277d3011)
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下的自动安装
24if ! hash 2>/dev/null apt-get; then
25	echo "脚本暂不支持对该系统下grub的安装,请手动完成"
26	exit 0
27fi
28
29#下载grub2.06
30if [ ! -f "grub-2.06.tar.xz" ]; then
31    echo "开始下载grub2.06"
32    wget https://mirrors.ustc.edu.cn/gnu/grub/grub-2.06.tar.xz || exit 1
33    echo "下载完成"
34fi
35
36tar xvf grub-2.06.tar.xz
37#安装对应依赖
38sudo apt-get update
39sudo apt-get install -y \
40	make 	\
41  	binutils \
42  	bison 	\
43  	gcc 	\
44  	gettext \
45	flex	\
46	bison	\
47	automake	\
48	autoconf
49
50cd grub-2.06
51echo "开始安装grub2.06"
52#编译安装三个版本的grub
53./configure --target=i386 --prefix=${grub_dir_i386_legacy} --disable-werror || exit 1
54make -j $(nproc) || exit 1
55sudo make install || exit 1
56make clean || exit 1
57
58./configure --target=i386 --with-platform=efi --prefix=${grub_dir_i386_efi} --disable-werror ||	exit 1
59make -j $(nproc) || exit 1
60sudo make install || exit 1
61make clean || exit 1
62
63./configure --target=x86_64 --with-platform=efi --prefix=${grub_dir_x86_64_efi} --disable-werror || exit 1
64make -j $(nproc) || exit 1
65sudo make install || exit 1
66
67cd ..
68#解除权限限制
69sudo chmod -R 777 ${grub_dir_i386_legacy}
70sudo chmod -R 777 ${grub_dir_i386_efi}
71sudo chmod -R 777 ${grub_dir_x86_64_efi}
72rm -rf grub-2.06
73rm grub-2.06.tar.xz*
74echo "grub2.06安装完成"
75