grub_auto_install.sh 4.9 KB

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