hugetlb_setup.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/bin/bash
  2. set -o errexit
  3. pushd $(dirname $(readlink -f "$BASH_SOURCE")) > /dev/null
  4. CUR_DIR=$(pwd)
  5. ROOT_DIR=$(cd .. && pwd)
  6. popd > /dev/null
  7. export LC_CTYPE="en_US.UTF-8"
  8. ################################################
  9. ensure_file_exist() {
  10. local file="$1"
  11. if [ ! -e "$file" ]; then
  12. error_exit "$file not exists"
  13. fi
  14. }
  15. ensure_file_writable() {
  16. local file="$1"
  17. ensure_file_exist "$file"
  18. if [ ! -w "$file" ]; then
  19. error_exit "$file not writable"
  20. fi
  21. }
  22. ensure_file_readable() {
  23. local file="$1"
  24. ensure_file_exist "$file"
  25. if [ ! -r "$file" ]; then
  26. error_exit "$file not readable"
  27. fi
  28. }
  29. GREEN="\033[1;32m"
  30. RED="\033[1;31m"
  31. YELLOW="\033[1;33m"
  32. UCYAN="\033[4;36m"
  33. NOCOLOR="\033[0m"
  34. REVERSECOLOR="\e[7m"
  35. function error_exit() {
  36. error "$@"
  37. exit 1
  38. }
  39. function info_exit() {
  40. info "$@"
  41. exit 0
  42. }
  43. function info() {
  44. echo
  45. echo -e "${GREEN}$@${NOCOLOR}"
  46. echo
  47. }
  48. function warn() {
  49. echo
  50. echo -e "${YELLOW}$@${NOCOLOR}"
  51. echo
  52. }
  53. function error() {
  54. echo
  55. echo -e "${RED}$@${NOCOLOR}"
  56. echo
  57. }
  58. declare -A NEW_KERNEL_PARAMS=(
  59. [default_hugepagesz]=1G
  60. [hugepagesz]=1G
  61. )
  62. OLD_KERNEL_PARAMS_FILE="/tmp/ocboot_hugetbl_old_kernel_params_file.txt"
  63. _fill_old_kernel_params() {
  64. rm -rf $OLD_KERNEL_PARAMS_FILE && touch $OLD_KERNEL_PARAMS_FILE
  65. local cmdline_param=$*
  66. for param in $cmdline_param; do
  67. local key
  68. local val
  69. key=${param%=*}
  70. val=${param#*=}
  71. if [[ "$key" == "$val" ]]; then
  72. echo "$key" >> $OLD_KERNEL_PARAMS_FILE
  73. else
  74. echo "$key=$val" >> $OLD_KERNEL_PARAMS_FILE
  75. fi
  76. done
  77. }
  78. _merge_new_kernel_params() {
  79. local new_tmp_val
  80. for key in "${!NEW_KERNEL_PARAMS[@]}"; do
  81. new_tmp_val="${NEW_KERNEL_PARAMS[$key]}"
  82. if grep -q "^$key=" $OLD_KERNEL_PARAMS_FILE; then
  83. sed -i "s|^$key=.*|$key=$new_tmp_val|g" $OLD_KERNEL_PARAMS_FILE
  84. else
  85. echo "$key=$new_tmp_val" >> $OLD_KERNEL_PARAMS_FILE
  86. fi
  87. done
  88. }
  89. _generate_kernel_cmdline() {
  90. cat $OLD_KERNEL_PARAMS_FILE | tr '\n' ' '
  91. }
  92. get_distro() {
  93. distro=($(awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }' | tr -d \"))
  94. echo "${distro[@]}"
  95. }
  96. function findStringInArray() {
  97. local search="$1"
  98. shift
  99. for element in "$@"; do
  100. if [[ "$element" == "$search" ]]; then
  101. return 0
  102. fi
  103. done
  104. return 1
  105. }
  106. env_check() {
  107. if [[ "$(uname -m)" != "x86_64" ]]; then
  108. info_exit "is not x86 machine"
  109. fi
  110. if [[ $EUID -ne 0 ]]; then
  111. error_exit "You need sudo or root to run this script."
  112. fi
  113. local supported_distros=("centos" "debian" "openeuler" "ubuntu")
  114. local distros=($(get_distro))
  115. local found_supported_distro=false
  116. local unsupported_distros=()
  117. for distro in "${distros[@]}"; do
  118. if findStringInArray "${distro}" "${supported_distros[@]}"; then
  119. found_supported_distro=true
  120. echo "${distro}"
  121. break
  122. else
  123. unsupported_distros+=("${distro}")
  124. fi
  125. done
  126. if [[ $found_supported_distro == false ]]; then
  127. error_exit "The following Linux distributions are not supported: ${unsupported_distros[*]}, only support ${supported_distros[*]}"
  128. fi
  129. }
  130. mk_grub2(){
  131. if [ -d /sys/firmware/efi ]; then
  132. mkdir -p /boot/efi/EFI/centos
  133. grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
  134. else
  135. grub2-mkconfig -o /boot/grub2/grub.cfg
  136. fi
  137. }
  138. mk_grub2_openeuler(){
  139. if [ -d /sys/firmware/efi ]; then
  140. mkdir -p /boot/efi/EFI/openEuler
  141. grub2-mkconfig -o /boot/efi/EFI/openEuler/grub.cfg
  142. else
  143. grub2-mkconfig -o /boot/grub2/grub.cfg
  144. fi
  145. }
  146. mk_grub_legacy(){
  147. update-grub
  148. }
  149. mk_grub(){
  150. local distro=${1}
  151. if [[ "${distro}" == "centos" ]]; then
  152. mk_grub2
  153. elif [[ "${distro}" == "debian" ]] || [[ "${distro}" == "ubuntu" ]]; then
  154. mk_grub_legacy
  155. elif [[ "${distro}" == "openeuler" ]]; then
  156. mk_grub2_openeuler
  157. else
  158. error_exit "unsupport distro ${distro}!"
  159. fi
  160. }
  161. grub_setup() {
  162. info "Configure grub option..."
  163. local grub_cfg="/etc/default/grub"
  164. local cmdline_param
  165. local idx
  166. local distro=${1}
  167. ensure_file_writable "$grub_cfg"
  168. cmdline_param=$(grep GRUB_CMDLINE_LINUX $grub_cfg | cut -d'"' -f2)
  169. _fill_old_kernel_params $cmdline_param
  170. _merge_new_kernel_params
  171. cmdline_param=$(_generate_kernel_cmdline)
  172. sed -i "s|GRUB_CMDLINE_LINUX=.*|GRUB_CMDLINE_LINUX=\"$cmdline_param\"|g" $grub_cfg
  173. mk_grub ${distro}
  174. }
  175. main() {
  176. distro=$(env_check)
  177. grub_setup ${distro}
  178. systemctl enable oc-hugetlb-gigantic-pages
  179. info "All done, ${UCYAN}REBOOT to make it work"
  180. }
  181. main