You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kernel-remover 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #!/bin/sh
  2. # Copyright (C) 2005-2015, Stefan Lippers-Hollmann <s.l-h@gmx.de>
  3. # Copyright (C) 2007-2010, Joaquim Boura <x-un-i@berlios.de>
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # we need gettext (is loaded in ssft.sh or cloned...)
  14. if [ -f /usr/bin/gettext.sh ]; then
  15. . /usr/bin/gettext.sh || exit 1
  16. else
  17. exit 1
  18. fi
  19. #---------------------------------------------------------------------
  20. # we need root rights
  21. #---------------------------------------------------------------------
  22. if [ "$(id -u)" -ne 0 ]; then
  23. if [ -x "$(which su-to-root)" ]; then
  24. [ -n "$DISPLAY" ] && exec su-to-root -X -c "${0} $@"
  25. exec su-to-root -c "${0} $@"
  26. fi
  27. printf "ERROR: $0 needs root capabilities, please start it as root.\n\n" >&2
  28. exit 1
  29. else
  30. if test -n "$DISPLAY" && ! xset q > /dev/null 2>&1 ; then unset DISPLAY; fi
  31. fi
  32. TEXTDOMAIN="kernel-remover"
  33. export TEXTDOMAIN
  34. TEXTDOMAINDIR=/usr/share/locale
  35. export TEXTDOMAINDIR
  36. #---------------------------------------------------------------------
  37. usage()
  38. {
  39. echo "$(basename $0)"
  40. echo " -G parameter use parameter as the graphical frontend"
  41. echo " one of text | dialog | kdialog | zenity "
  42. echo " -f proceed without asking, do complete cleanup"
  43. echo " -h show this usage"
  44. exit 1
  45. }
  46. #---------------------------------------------------------------------
  47. force=0
  48. xtra=0
  49. unset frontend
  50. while getopts fhxG: name; do
  51. case $name in
  52. f)
  53. force=1
  54. ;;
  55. G)
  56. frontend="$OPTARG"
  57. ;;
  58. h) usage
  59. ;;
  60. x)
  61. xtra=1
  62. ;;
  63. *)
  64. usage
  65. ;;
  66. esac
  67. done
  68. shift $(($OPTIND - 1))
  69. #---------------------------------------------------------------------
  70. prepare()
  71. {
  72. if [ "$force" -eq 0 ]; then
  73. # we need ssft
  74. if [ -f /usr/bin/ssft.sh ]; then
  75. . /usr/bin/ssft.sh || exit 1
  76. else
  77. echo "Please install the package \"ssft\"."
  78. exit 1
  79. fi
  80. fi
  81. if [ -n "${frontend}" ]; then
  82. case $frontend in
  83. text)
  84. SSFT_FRONTEND="${frontend}"
  85. ;;
  86. dialog)
  87. SSFT_FRONTEND="${frontend}"
  88. ;;
  89. kdialog)
  90. SSFT_FRONTEND="${frontend}"
  91. ;;
  92. zenity)
  93. SSFT_FRONTEND="${frontend}"
  94. ;;
  95. *)
  96. echo unknown frontend
  97. exit 1
  98. ;;
  99. esac
  100. fi
  101. if [ -z "$DISPLAY" ] && ! /usr/bin/xdpyinfo >/dev/null 2>&1 ; then
  102. unset DISPLAY
  103. fi
  104. if [ -z "$DISPLAY" ]; then
  105. [ -x /usr/bin/dialog ] && SSFT_FRONTEND=${SSFT_FRONTEND:-"dialog"} || \
  106. SSFT_FRONTEND=${SSFT_FRONTEND:-"text"}
  107. else
  108. if [ -n "$DISPLAY" ] && [ ! -x /usr/bin/zenity ]; then
  109. if [ -x /usr/bin/dialog ]; then
  110. SSFT_FRONTEND="dialog"
  111. else
  112. SSFT_FRONTEND="text"
  113. fi
  114. DISPLAY=""
  115. else
  116. SSFT_FRONTEND=${SSFT_FRONTEND:-"zenity"}
  117. fi
  118. fi
  119. # the current one will not be shown in the list ....
  120. CURRENTKERNEL=$(uname -r)
  121. TITLE="$(gettext "Removing installed kernels")"
  122. PAD="======="
  123. if [ "${SSFT_FRONTEND}" = "dialog" ] || [ -z "${SSFT_FRONTEND}" ]; then
  124. CURRENT="$(eval_gettext "The current (active) kernel is ${PAD}$(uname -r)${PAD}")"
  125. else
  126. CURRENT="$(eval_gettext "The current (active) kernel is $(uname -r)")"
  127. fi
  128. }
  129. #---------------------------------------------------------------------
  130. # some useful functions
  131. #---------------------------------------------------------------------
  132. inputbox()
  133. {
  134. # inputbox Title Text dummy default
  135. Title="$1"
  136. Text="$2"
  137. # $3 not used
  138. # The default value if used
  139. SSFT_DEFAULT=$4
  140. ssft_read_string "${Title}" "${Text}";
  141. }
  142. #---------------------------------------------------------------------
  143. msgbox()
  144. {
  145. # msgbox title text
  146. Title="$1"
  147. Text="$2"
  148. ssft_display_message "${Title}" "${Text}"
  149. }
  150. #---------------------------------------------------------------------
  151. select_more()
  152. {
  153. # select one of a list
  154. Title=$1
  155. Text=$2
  156. shift 2
  157. if ssft_select_multiple "$Title" "$Text" $@ ; then
  158. Selected=$SSFT_RESULT
  159. return 0
  160. else
  161. return 1
  162. fi
  163. }
  164. #---------------------------------------------------------------------
  165. yesno()
  166. {
  167. # yesno title text width
  168. Title=$1
  169. Text=$2
  170. ssft_yesno "${Title}" "${Text}"
  171. return $?
  172. }
  173. #---------------------------------------------------------------------
  174. remove_one_kernel()
  175. {
  176. Kernel=$1
  177. if [ "${Kernel}" != "${CURRENTKERNEL}" ]; then
  178. apt-get remove --purge --yes $(dpkg -l | grep ${Kernel} | awk '{print $2}')
  179. dpkg -l "linux-headers-${Kernel}-common" >/dev/null 2>&1 && \
  180. apt-get remove --purge --yes "linux-headers-${Kernel}-common"
  181. dpkg -l "linux-support-${Kernel}" >/dev/null 2>&1 && \
  182. apt-get remove --purge --yes "linux-support-${Kernel}"
  183. # dispose make install artefacts
  184. if [ ! -e "/boot/vmlinuz-${Kernel}" ]; then
  185. rm -rf /lib/modules/${Kernel}
  186. fi
  187. fi
  188. }
  189. #---------------------------------------------------------------------
  190. get_KernelList()
  191. {
  192. for v in /boot/vmlinuz-*; do
  193. Kernel="$(basename $v | sed s/vmlinuz-//)"
  194. if [ "${Kernel}" != "${CURRENTKERNEL}" ]; then
  195. meta_package="$(echo $(grep-status \
  196. -F Depends -s Package \
  197. linux-image-${Kernel}) | cut -d: -f 2)"
  198. case $meta_package in
  199. *$Kernel* )
  200. # this is an old style kernel
  201. KernelList="${KernelList} ${Kernel}"
  202. continue
  203. ;;
  204. esac
  205. [ -n "${meta_package}" ] && \
  206. meta_status="$(dpkg-query -f='${STATUS}\n' \
  207. -W ${meta_package}|\
  208. cut -d ' ' -f 3)" || \
  209. meta_status="not-installed"
  210. if [ "${meta_status}" = "not-installed" ]; then
  211. [ -z "${KernelList}" ] && KernelList="${Kernel}" ||\
  212. KernelList="${KernelList} ${Kernel}"
  213. # echo KernelList="$KernelList"
  214. fi
  215. fi
  216. done
  217. }
  218. #---------------------------------------------------------------------
  219. # Main
  220. #---------------------------------------------------------------------
  221. if [ "${xtra}" -eq 1 ]; then
  222. KernelList="$@"
  223. for i in ${KernelList} ; do
  224. removing="$(eval_gettext "removing kernel ${i}")"
  225. echo $removing
  226. remove_one_kernel "$i"
  227. done
  228. MSG="$(eval_gettext "the following kernels have been removed: \"${KernelList}\"")"
  229. echo $MSG
  230. exit 0
  231. fi
  232. prepare
  233. get_KernelList
  234. if [ -z "${KernelList}" ]; then
  235. MSG="$(gettext "There is only one kernel installed on this system. Nothing to be done!")"
  236. if [ "${force}" -eq 1 ]; then
  237. echo "${MSG}"
  238. else
  239. msgbox "${TITLE}" "${MSG}"
  240. fi
  241. exit 0
  242. fi
  243. if [ "${force}" -eq 1 ]; then
  244. current="$(eval_gettext "The current (active) kernel is $(uname -r)")"
  245. echo ${current}
  246. for i in ${KernelList} ; do
  247. removing="$(eval_gettext "removing kernel ${i}")"
  248. echo $removing
  249. remove_one_kernel "$i"
  250. done
  251. MSG="$(eval_gettext "the following kernels have been removed: \"${KernelList}\"")"
  252. echo $MSG
  253. else
  254. select_more "${TITLE}" "${CURRENT}" ${KernelList}
  255. if [ "$?" -ne 0 ]; then
  256. exit 10
  257. fi
  258. if [ -z "${SSFT_RESULT}" ]; then
  259. exit 12
  260. fi
  261. one_removed=false
  262. for i in ${SSFT_RESULT}; do
  263. MSG="$i : $(gettext "Should I remove this kernel?") "
  264. yesno "${TITLE}" "${MSG}"
  265. if [ "$?" -eq 0 ]; then
  266. remove_one_kernel "$i"
  267. one_removed=true
  268. else
  269. msgbox "${TiTLE}" "$(gettext "Kernel not removed:") $i"
  270. fi
  271. done
  272. fi
  273. exit 0