upd_kernel 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/bin/bash
  2. #set -x
  3. ########################################################################
  4. #
  5. ## [Kernel] Downloader stable version for $Buntu
  6. #
  7. ########################################################################
  8. # @Author: Stephane HUC
  9. # @mail: devs@stephane-huc.net
  10. # @licence: GNU/GPL 3
  11. # Date: 2017/03/13
  12. ########################################################################
  13. clear
  14. arch="$(uname -m)";
  15. email="" # change by your mail...
  16. k_v="" # version kernel
  17. file_chattr="" # change if you use a chattr script
  18. nb=24
  19. pwd="$(pwd)"
  20. if [ -e /dev/urandom ]; then random=/dev/urandom;
  21. elif [ -e /dev/random ]; then random=/dev/random;
  22. fi
  23. secure_sys=0 # change to 0, if you are not use chattr and your /boot is not on 'ro' permission!
  24. tampon="$(mktemp)"
  25. url_kernel="https://www.kernel.org/"
  26. url_ppa="http://kernel.ubuntu.com/~kernel-ppa/mainline/v"
  27. version="$(awk -F ':' '/^# Date/ { print $2 }' $0)"
  28. # manage colors
  29. green="$(tput setaf 2)"
  30. neutral="$(tput sgr0)"
  31. red="$(tput setaf 1)"
  32. ########################################################################
  33. ###
  34. ##
  35. # FUNCTIONS
  36. ##
  37. ###
  38. ########################################################################
  39. byebye() {
  40. _warning "$1"
  41. _remount_sys
  42. exit 1
  43. }
  44. _chmod_boot() {
  45. _info "### Securing System.map files!"
  46. chmod 0400 /boot/System.map*
  47. _info "### Securing abi files!"
  48. chmod 0640 /boot/abi*
  49. _info "### Securing config files!"
  50. chmod 0640 /boot/config*
  51. }
  52. delete_file() {
  53. if [ -f "$1" ]; then
  54. if [ -x "$(which shred)" ]; then _shred "$1"; else rm -f "$1"; fi
  55. fi
  56. }
  57. delete_tmpdir() {
  58. cd "${pwd}" && rm -rf "/tmp/${arch}"
  59. }
  60. get_arch() {
  61. _info "### Get architecture of your station!"
  62. if [ "${arch}" = "i686" ] || [ "${arch}" = "i386" ]; then arch="i386"
  63. elif [ "${arch}" = "x86_64" ]; then arch="amd64"
  64. else byebye "Unsupported architecture!"
  65. fi
  66. _info "### Arch: ${green}${arch}${neutral}"
  67. }
  68. _info() {
  69. printf "%s \n" "$1"
  70. }
  71. _notify() {
  72. printf "[ ${green}%s${neutral} ] %s \n" "OK" "$1"
  73. }
  74. _send_mail() {
  75. _info "### Attempt to send email to inform you about new kernel installed!"
  76. if [ -n "${email}" ]; then
  77. echo "Please, inspect your computer... to reboot? Kernel ${k_v} is installed!" | mail -t "From: Siou <zou@siou.stephane-huc.net>" -t "Subject: Updating Kernel!" "${email}"
  78. fi
  79. }
  80. _shred(){
  81. file="$1"
  82. if [ ${random} ]; then
  83. shred -f -n ${nb} -z -u --random-source=${random} "${file}"
  84. else
  85. shred -f -n ${nb} -z -u "${file}"
  86. fi
  87. if [ $? = 0 ]; then
  88. _notify "File ${file} secure deleted!";
  89. else
  90. _warning "An error during secure delete: file ${file} is not correctly deleted. Verify!";
  91. fi
  92. }
  93. _remount_sys() {
  94. if [ ${secure_sys} -eq 1 ]; then
  95. sync
  96. if [[ "$(awk '/\/boot/ { print $4 }' /proc/mounts)" =~ 'rw' ]]; then mount -o remount /boot; fi
  97. if [[ "$(awk '/\/usr/ { print $4 }' /proc/mounts)" =~ 'rw' ]]; then mount -o remount /usr; fi
  98. if [[ "$(awk '/\/tmp/ { print $4 }' /proc/mounts)" =~ 'rw' ]]; then mount -o remount /tmp; fi
  99. [ -f "${file_chattr}" ] && "${file_chattr}" true
  100. fi
  101. }
  102. verify_uid() {
  103. if [ $(id -u) -ne 0 ]; then byebye "Need to get rights admins!"; fi
  104. }
  105. _warning() {
  106. printf "[ ${red}%s${neutral} ] %s \n" "KO" "$1"
  107. }
  108. ########################################################################
  109. ###
  110. ##
  111. # EXECUTIONS
  112. ##
  113. ###
  114. ########################################################################
  115. _info "
  116. ########################################################################
  117. ### Downloader actual stable kernel: ${version}
  118. ########################################################################
  119. "
  120. verify_uid
  121. get_arch
  122. if [ ${secure_sys} -eq 1 ]; then
  123. # verif /boot rw
  124. if [[ "$(awk '/\/boot/ { print $4 }' /proc/mounts)" =~ 'ro' ]]; then mount -o remount,rw /boot; fi
  125. # verify /usr
  126. if [[ "$(awk '/\/usr/ { print $4 }' /proc/mounts)" =~ 'ro' ]]; then mount -o remount,rw /usr; fi
  127. # verify /tmp
  128. if [[ "$(awk '/\/tmp/ { print $4 }' /proc/mounts)" =~ 'ro' ]]; then mount -o remount,rw /tmp; fi
  129. # get chattr_sys false
  130. [ -f "${file_chattr}" ] && "${file_chattr}" false
  131. fi
  132. # create tmp dir and cd into
  133. _info "### Create temporary directory needed, and move in!"
  134. if mkdir "/tmp/${arch}"; then cd "/tmp/${arch}"; else byebye "Cant create temporary directory needed!"; fi
  135. # get version kernel https://kernel.org/
  136. _info "
  137. ### Download homepage kernel.org to obtain version kernel!
  138. "
  139. if curl "${url_kernel}" -o "${tampon}"; then
  140. # get information version kernel
  141. k_v="$(egrep -A1 "<td id=\"latest_link\">" "${tampon}" | tail -n1 | sed -e "s#<a href=\"\([^>]*\)\">\(.*\)</a>#\2#;s/ //g")"
  142. if [ -n "${k_v}" ]; then
  143. _info "### kernel version: ${green}${k_v}${neutral}"
  144. else
  145. delete_tmpdir
  146. byebye "Bad Info kernel version!"
  147. fi
  148. sleep 1
  149. # verify if kernel is installed!
  150. if dpkg -l | egrep -i "linux-(.*)-${k_v}"; then
  151. delete_tmpdir
  152. byebye "Script stop, because kernel ${k_v} seems installed!"
  153. fi
  154. fi
  155. # get file README to obtain filenames linux
  156. _info "
  157. ### Get files linux kernel needed!
  158. "
  159. if curl "${url_ppa}${k_v}/README" -O; then
  160. if [ -f "/tmp/${arch}/README" ]; then
  161. linux_headers_all="$(grep "linux-headers-${k_v}\(.*\)_all.deb" /tmp/${arch}/README | tail -n1 | tr -d ' ')"
  162. _info "### Linux hearders all: ${green}${linux_headers_all}${neutral}"
  163. linux_headers_arch="$(grep "linux-headers-${k_v}\(.*\)-generic_\(.*\)_${arch}.deb" /tmp/${arch}/README | tr -d ' ')"
  164. _info "### Linux headers ${arch}: ${green}${linux_headers_arch}${neutral}"
  165. linux_image_arch="$(grep "linux-image-${k_v}\(.*\)-generic_\(.*\)_${arch}.deb" /tmp/${arch}/README | tr -d ' ')"
  166. _info "### Linux image ${arch}: ${green}${linux_image_arch}${neutral}"
  167. else
  168. delete_tmpdir
  169. byebye "Script stop here, because file README seems could not be read!"
  170. fi
  171. sleep 1
  172. fi
  173. # get files linux needed
  174. if [ "${linux_headers_all}" ]; then curl "${url_ppa}${k_v}/${linux_headers_all}" -O; fi
  175. if [ "${linux_headers_arch}" ]; then curl "${url_ppa}${k_v}/${linux_headers_arch}" -O; fi
  176. if [ "${linux_image_arch}" ]; then curl "${url_ppa}${k_v}/${linux_image_arch}" -O; fi
  177. sleep 1
  178. if [ -f "/tmp/${arch}/${linux_headers_all}" ] && [ -f "/tmp/${arch}/${linux_headers_all}" ] && [ -f "/tmp/${arch}/${linux_headers_all}" ]; then
  179. # install packages
  180. _info "
  181. ### Attempt to install packages linux by dpkg tool!
  182. "
  183. if dpkg -i linux-*.deb; then
  184. _info "### Deleting all downloaded files!"
  185. for file in $(find "/tmp/${arch}" -type f); do delete_file "${file}"; done
  186. delete_tmpdir
  187. _chmod_boot
  188. _send_mail
  189. fi
  190. else
  191. delete_tmpdir
  192. byebye "Script stop here, because all kernel files *.deb seem not be avalaibles!"
  193. fi
  194. _remount_sys
  195. _info "
  196. ########################################################################
  197. ### Process ended!
  198. ########################################################################
  199. "