install_bootloader.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/bin/bash
  2. base_dir=$( dirname "${BASH_SOURCE[0]}" )
  3. work_dir=
  4. work_disk=
  5. root_part=
  6. boot_part=
  7. defkernel=
  8. menu_timeout=60
  9. prompt_timeout=60
  10. #
  11. declare -A menu
  12. declare -A kernelparams
  13. source "$( dirname "${BASH_SOURCE[0]}" )/functions.sh"
  14. gen_syslinux_cfg()
  15. {
  16. local to=
  17. local pt=
  18. local ui=
  19. for param in ${1}
  20. do
  21. IFS=':'
  22. read -r to pt ui <<< "${param}"
  23. IFS=$XIFS
  24. #echo part=${to} mountpoint=${pt} opt=${ui}
  25. echo -e "UI ${ui}" >> "${2}"
  26. echo -e "PROMPT ${pt}" >> "${2}"
  27. echo -e "TIMEOUT ${to}" >> "${2}"
  28. echo -e "MENU TITLE WELLCOME" >> "${2}"
  29. echo -e "TABMSG" >> "${2}"
  30. echo -e "MENU BACKGROUND #c00090f0" >> "${2}"
  31. echo -e "MENU WIDTH 160" >> "${2}"
  32. echo -e "MENU ROWS 5" >> "${2}"
  33. echo -e "MENU MARGIN 6" >> "${2}"
  34. echo -e "MENU VSHIFT 8" >> "${2}"
  35. echo -e "MENU RESOLUTION $(cat /sys/devices/platform/efi-framebuffer.0/width) $(cat /sys/devices/platform/efi-framebuffer.0/width) " >> "${2}"
  36. echo -e "MENU COLOR border 30,44 #00000000 #00000000 none" >> "${2}"
  37. echo -e "MENU COLOR title 0 #9033ccff #00000000 std" >> "${2}"
  38. echo -e "MENU COLOR disabled 0 #9033ccff #00000000 std" >> "${2}"
  39. echo -e "MENU COLOR sel 30,47 #ffffffff #e0000000 none" >> "${2}"
  40. echo -e "MENU COLOR unsel 30,47 #efffffff #00000000 none" >> "${2}"
  41. echo -e "DEFAULT" >> "${2}"
  42. done
  43. }
  44. gen_syslinux_cfg_entry()
  45. {
  46. local label=
  47. local entry=
  48. local append=
  49. for param in ${1}
  50. do
  51. IFS=':'
  52. read -r label entry append <<< "${param}"
  53. IFS=$XIFS
  54. #echo part=${label} mountpoint=${entry} opt=${append}
  55. echo -e "LABEL ${label}"
  56. echo -e "MENU LABEL ${label}" >> "${2}"
  57. echo -e "LINUX ${entry}" >> "${2}"
  58. echo -e "APPEND ${append}" >> "${2}"
  59. done
  60. }
  61. printf "\nThis script helps to install bootloader"
  62. execution_premission "Sure you want to run this? " || die
  63. if ! [ -a "${root_part}" ]; then
  64. options="$(get_partitions_list)"
  65. if prompt_select "Select root partition."; then
  66. root_part="${selected}"
  67. else
  68. die
  69. fi
  70. fi
  71. if ! [ -d "${work_dir}" ]; then
  72. options="/ $(find /mnt/* -maxdepth 0 -type d)"
  73. options="${options//${tempfs%\/*}/} new..."
  74. if prompt_select "Select directory you want to use as the installation mount point."; then
  75. case ${selected} in
  76. "new...") work_dir=$(prompt_new_dir /mnt);;
  77. *) work_dir=${selected};;
  78. esac
  79. fi
  80. work_dir=${work_dir%/}
  81. fi
  82. if ! [ -f ${work_dir}/etc/fstab ];
  83. then die "Cannot find fstab";
  84. fi
  85. boot_part=$(cat ${work_dir}/etc/fstab | grep -I /boot | awk '{ print $1 }')
  86. if [ -z "${boot_part##*=*}" ]; then
  87. boot_part=$(blkid -o device -t ${boot_part})
  88. fi
  89. root_part=$(cat /etc/fstab | grep -I "/ " | awk '{ print $1 }')
  90. work_disk="${boot_part%[[:digit:]]*}"
  91. disklabeltype="$(blkid -o value -s PTTYPE ${work_disk})"
  92. echo "Boot partition found: ${boot_part} disk: ${work_disk} type: ${disklabeltype} \n"
  93. printf "Mounting boot partition. \n"
  94. try mount ${boot_part} ${work_dir}/boot && { cleanup wait_umount ${work_dir}/boot; cleanup umount -d ${work_dir}/boot; } || { die "Cannot mount ${boot_part}"; }
  95. resc_kernel_list=$(find ${work_dir}/boot -maxdepth 1 -type f -name rescue* -printf '%P ')
  96. efi_kernel_list=$(find ${work_dir}/boot -maxdepth 1 -type f -name *.efi -printf '%P ')
  97. kernel_list="$(find ${work_dir}/boot -maxdepth 1 -type f -name kernel* -printf '%P ' \
  98. -or -name vmlinuz* -printf '%P ' -or -name bzImage* -printf '%P ') ${resc_kernel_list} ${efi_kernel_list}"
  99. initramfs_list=$(find ${work_dir}/boot -maxdepth 1 -type f -name "init*" -printf '%P ')
  100. if [ -n "${kernel_list}" ]; then
  101. options="${kernel_list}"
  102. if prompt_select "Select default kernel to load"; then
  103. defkernel="${selected}"
  104. fi
  105. for kernel in ${kernel_list}
  106. do
  107. read -p "Enter additional params (ro quiet splash ...) for ${kernel} " kernelparams[kernel]
  108. read -p "Enter menuentry ${kernel} " menu[kernel]
  109. done
  110. else
  111. echo "No kernels found in boot partition."
  112. fi
  113. if [ -n "${initramfs_list}" ]; then
  114. options="${initramfs_list} skip"
  115. if prompt_select "Select initramfs"; then
  116. initramfs="${selected}"
  117. fi
  118. fi
  119. options="syslinux grub u-boot uefi"
  120. if prompt_select "Select bootloader"; then
  121. case ${selected} in
  122. "syslinux")
  123. case ${disklabeltype} in
  124. "dos")
  125. conf_dir=${work_dir}/boot/extlinux
  126. conf_name=extlinux.conf
  127. syslinux_files="\{chain.c32,gfxboot.c32,vesamenu.c32,libutil.c32,libcom32.c32\}"
  128. try mkdir -vp ${conf_dir}
  129. try extlinux --install ${conf_dir}
  130. cp ${work_dir}/usr/share/syslinux/${syslinux_files} ${conf_dir}
  131. pv -s 440 -S -B 8 ${work_dir}/usr/share/syslinux/mbr.bin > ${work_disk}
  132. gen_syslinux_cfg "${menu_timeout}:${promt_timeout}:vesamenu.c32" ${conf_dir}/${conf_name}
  133. ;;
  134. "gpt")
  135. conf_dir=${work_dir}/boot/efi
  136. conf_name=syslinux.cfg
  137. try syslinux --install ${boot_part}
  138. try mkdir -vp ${work_dir}
  139. cp ${work_dir}/usr/share/syslinux/efi64/* ${conf_dir}
  140. mv ${work_dir}/usr/share/syslinux/efi64/syslinux.efi ${conf_dir}/bootx64
  141. gen_syslinux_cfg "${menu_timeout}:${promt_timeout}:vesamenu.c32" ${conf_dir}/${conf_name}
  142. ;;
  143. "*")
  144. echo -e "Boot loader code not installed! Unsupported disklabel type: ${disklabeltype}";;
  145. esac
  146. if [ -n "${resc_kernel_list}" ]; then
  147. for kernel in ${resc_kernel_list}
  148. do
  149. gen_syslinux_cfg_entry "Syslinux kernel:${kernel}:inird=initram.igz real_root=${root_part}" ${conf_dir}/${conf_name}
  150. done
  151. fi
  152. if [ -n "${kernel_list}" ]; then
  153. for kernel in ${kernel_list}
  154. do
  155. gen_syslinux_cfg_entry "${menu[kernel]}:${kernel}:${kernelparams[kernel]}" ${conf_dir}/${conf_name}
  156. if [ -n "${initramfs}" ]; then
  157. echo INITRD initrd=${initramfs} >> ${conf_dir}/${conf_name}
  158. echo APPEND real_root=${root_part} >> ${conf_dir}/${conf_name}
  159. else
  160. echo APPEND root=${root_part} >> ${conf_dir}/${conf_name}
  161. fi
  162. done
  163. fi
  164. ;;
  165. "grub")
  166. case ${disklabeltype} in
  167. "dos")
  168. try grub-install --target="i386-pc" --no-floppy ${work_disk}
  169. ;;
  170. "gpt")
  171. try grub-install --target="x86_64-efi" --efi-directory=/boot --bootloader-id="{$menu[defkernel]}" --recheck ${work_disk}
  172. ;;
  173. "*")
  174. echo -e "Boot loader code not installed! Unsupported disklabel type: ${disklabeltype}"
  175. ;;
  176. esac
  177. ;;
  178. "uboot")
  179. try mkimage -A arm -T script -C none -n "Boot.scr for android" -d boot.txt boot.scr
  180. ;;
  181. "efimem")
  182. try mkdir -vp ${work_dir}/boot/EFI/Boot
  183. try pv ${defkernel} > ${work_dir}/boot/EFI/Boot/bootx64.efi
  184. echo 'root=${root_part} add_efi_memmap -u initrd=${initramfs} ${kernelparams[defkernel]}' | iconv -f ascii -t ucs2 | efibootmgr -c -g -d ${work_disk} -p 1 -L '${menu[defkernel]}' -l '\EFI\Boot\bootx64.efi' -@ -
  185. ;;
  186. *) ;;
  187. esac
  188. fi
  189. proceed_cleanup
  190. exit 0