yurez_nsk

Скрипт атоматического удаления старых ядер Debian/Ubuntu

May 23rd, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.24 KB | None | 0 0
  1. #!/bin/bash
  2. # this is a script for purging unused old kernels installed in your system,
  3. # you should use this script under newest kernel installed!!!
  4. # Please, run it with sudo or as a root user (needed only for apt-get purge operation)!
  5.  
  6. # checking for root privileges
  7. if [ "$(id -u)" != "0" ]; then
  8. echo 'This script must be run as root or with sudo command!!!' 2>&1
  9. exit 1
  10. fi
  11.  
  12. # let's get installed and removed kernels list
  13. # and exclude removed kernels
  14. # and exclude kernel you are using now
  15. # excluding kernel metapackage too (list only linux-image-2....),
  16. # then we are going to write this list to a temporary file
  17. dpkg --get-selections linux-image*\
  18. | grep -v deinstall\
  19. | grep -v `uname -r`\
  20. | grep 2 > /tmp/tmp.txt
  21. # asking user if he wants to purge kernels found...
  22. echo "Old kernels found:"
  23. cat /tmp/tmp.txt
  24. echo "You are now running kernel:"
  25. echo linux-image-`uname -r`
  26. CORRECT_INPUT=0
  27. while [ $CORRECT_INPUT -ne 1 ]
  28. do
  29.     echo -n "Would you like to purge old kernels found? (y/n): "
  30.     read CHOISE
  31.     case $CHOISE in
  32.     y)
  33.     echo "Purging kernels!..."
  34.     CORRECT_INPUT=1
  35.     ;;
  36.     n)
  37.     rm /tmp/tmp.txt
  38.     echo "Exiting..."
  39.     CORRECT_INPUT=1
  40.     exit 0
  41.     ;;
  42.     *)
  43.     echo "Please enter y or n and press enter!"
  44.     ;;
  45.     esac
  46. done
  47. # let's purge unused kernels which are in our tempfile
  48. apt-get -s purge `cat /tmp/tmp.txt | cut --fields=1 -`
  49.  
  50. echo -n 'Done! Press any key to continue!'
  51. read CONTINUE
  52.  
  53. # finding old kernel-headers
  54. dpkg --get-selections linux-headers*\
  55. | grep -v deinstall\
  56. | grep 2 > /tmp/tmp.txt
  57. # asking user if he wants to remove old kernel haders too...
  58. echo "Old kernel headers found, script can purge them too:"
  59. cat /tmp/tmp.txt
  60. echo "Your current kernel headers version:"
  61. echo linux-headers-`uname -r`
  62. CORRECT_INPUT2=0
  63. while [ $CORRECT_INPUT2 -ne 1 ]
  64. do
  65.     echo -n "Would you like to purge old kernels headers? (y/n): "
  66.     read CHOISE
  67.     case $CHOISE in
  68.     y)
  69.     echo "Purging kernel headers!..."
  70.     CORRECT_INPUT2=1
  71.     ;;
  72.     n)
  73.     rm /tmp/tmp.txt
  74.     echo "Exiting..."
  75.     CORRECT_INPUT2=1
  76.     exit 0
  77.     ;;
  78.     *)
  79.     echo "Please enter y or n and press enter!"
  80.     ;;
  81.     esac
  82. done
  83. # purging kernel-headers which are in our tempfile
  84. apt-get -s purge `cat /tmp/tmp.txt | cut --fields=1 -`
  85.  
  86. rm /tmp/tmp.txt
  87. echo 'All done! Exiting script...'
  88. exit 0
Advertisement
Add Comment
Please, Sign In to add comment