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.

50 lines
956 B

  1. #!/bin/sh
  2. set -eu
  3. apt_opt=
  4. if [ $# -eq 0 ]; then
  5. : # interactive
  6. elif [ "${1:-}" = "-y" ]; then
  7. apt_opt="-y"
  8. shift
  9. fi
  10. obsolete_kernels() {
  11. local current= latest=
  12. local v= p=
  13. if [ $# -gt 2 ]; then
  14. current="$1" latest="$2"
  15. shift 2
  16. for v; do
  17. COLUMNS=500 dpkg -l "linux*$v*" | sed -n -e 's|^.. \(linux[^ \]\+\)[ \t].*|\1|p'
  18. done | while read p; do
  19. case "$p" in
  20. *-$current|*-$current-*|*-$latest|*-$latest-*)
  21. echo "$p: SKIP" >&2
  22. ;;
  23. *)
  24. printf "$p "
  25. ;;
  26. esac
  27. done
  28. fi
  29. }
  30. remove=$(obsolete_kernels \
  31. "$(uname -r | sed -e 's|-generic$||')" \
  32. $(ls -1 /boot/vmlinuz-* | \
  33. sed -e 's|^/boot/vmlinuz-||' -e 's|\.efi\.signed$||' -e 's|-generic$||' | \
  34. sort -urV))
  35. aptget="apt-get${apt_opt:+ $apt_opt}"
  36. cmd="$aptget update"
  37. if [ -n "$remove" ]; then
  38. cmd="$cmd && dpkg -P $remove"
  39. fi
  40. cmd="$cmd && $aptget autoremove --purge"
  41. cmd="$cmd && $aptget upgrade && $aptget dist-upgrade"
  42. set -x
  43. exec sudo sh -c "$cmd"