#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" LOCK_DIR="/tmp/$(basename $0).$$.LOCK" log_file="${LOCK_DIR}/${script_name%%.*}.log" # ---------- # Base Function(s) # ---------- clean_up() { # Perform program exit housekeeping rm -rf "$LOCK_DIR" blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n " $*\\c" 1>&2 else echo -e -n " $*" 1>&2 fi rm /tmp/shprompt$$ fi } fatal(){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mFatal\033[m ] $*" else echo -e " [ Fatal ] $*" fi echo "" if $terminal ; then echo -e " \033[1mScript terminated\033[m.." else echo -e " Script terminated.." fi echo "" rm -rf $LOCK_DIR exit 1 } error (){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mError\033[m ] $*" else echo " [ Error ] $*" fi echo "" } warn (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[33m\033[1mWarn\033[m ] $*" else echo " [ Warn ] $*" fi echo "" fi } info (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mInfo\033[m ] $*" else echo " [ Info ] $*" fi echo "" fi } ok (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mOk\033[m ] $*" else echo " [ Ok ] $*" fi echo "" fi } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[75G[ \033[32mok\033[m ] " fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" fi } echo_wait(){ if $terminal ; then echo -en "\033[75G[ \033[5m\033[1m...\033[m ]" fi } blank_line() { if $terminal ; then echo "" fi } detect_os () { if $(which lsb_release > /dev/null 2>&1) ; then os_dist="$(lsb_release -i | awk '{print tolower($3)}')" os_version="$(lsb_release -r | awk '{print tolower($2)}')" os_codename="$(lsb_release -c | awk '{print tolower($2)}')" if [[ "$os_dist" = "debian" ]]; then if $(echo "$os_version" | grep -q '\.') ; then os_version=$(echo "$os_version" | cut --delimiter='.' -f1) fi fi elif [[ -e "/etc/os-release" ]]; then . /etc/os-release os_dist=$ID os_version=${VERSION_ID} fi # remove whitespace from os_dist and os_version os_dist="${os_dist// /}" os_version="${os_version// /}" } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # - Create lock directory '$LOCK_DIR" # mkdir "$LOCK_DIR" # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi # - Detedt Os # - detect_os if [[ "${os_dist,,}" != ubuntu ]] ; then fatal "Linux Distribution '${os_dist}' is NOT supported (only Ubuntu). Exiting now ..!" fi # - Give your default values here # - LOGGING=false BATCH_MODE=false # ========== # - Begin Main Script # ========== # ---------- # - Headline # ---------- if $terminal ; then echo "" echo -e "\033[1m----------\033[m" echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" echo -e "\033[1m----------\033[m" fi # ---------- # - Some pre-script tasks .. # ---------- if $terminal ; then echo "" echo "" echo -e " \033[1mDoing some pre-script tasks ..\033[m" echo "" fi echononl "Detecting not fully deinstalled kernel packages (rc).." rc_kernel_pakages="$(dpkg -l linux* | grep -E "^rc" | awk '{print$2}')" if [[ $? -eq 0 ]] ; then echo_ok elif [[ -z "$rc_kernel_pakages" ]]; then echo_ok else echo_failed fatal "Detecting not fully deinstalled kernel packages failed." fi blank_line echononl "Get actual used kernel version.." kernel_version="$(uname -r | grep -o -E "^[[:digit:]]{1}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\-[[:digit:]]{1,3}")" if [[ $? -eq 0 ]] && [[ -n "$kernel_version" ]]; then echo_ok else echo_done fi echononl "Detecting old not used kernels.." if [[ -f "/var/run/reboot-required" ]]; then unused_kernel_pakages="" warn "Reboot befor removing unused kernels.." echo_skipped else unused_kernel_pakages="$(dpkg -l linux* | grep -E "^ii" | awk '{print$2}' | grep -v "$kernel_version" | grep -E "linux\-(image|modules|headers).*\-[[:digit:]]{1}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\-[[:digit:]]{1,3}")" if [[ $? -eq 0 ]] ; then echo_ok elif [[ -z "$unused_kernel_pakages" ]]; then echo_ok else echo_failed fatal "Detecting old unused kernel packages failed." fi fi if $terminal && ! $BATCH_MODE ; then echo "" echo "" echo -e " \033[32m\033[1mThe following Packages are detected for removing:\033[m" echo "" echo -e " \033[1mNot fully deinstalled Packages:\033[m" if [[ -z "$rc_kernel_pakages" ]]; then echo -e " \033[33m- No Packages found -\033[m" else for _package in $rc_kernel_pakages ; do echo -e " ${_package}" done fi echo "" echo -e " \033[1mUnused kernel packages:\033[m" if [[ -z "$unused_kernel_pakages" ]]; then echo -e " \033[33m- No Packages found -\033[m" else for _package in $unused_kernel_pakages ; do echo -e " ${_package}" done fi if [[ -z "$rc_kernel_pakages" ]] && [[ -z "$unused_kernel_pakages" ]] ; then info "No package for deletion found." clean_up 0 fi echo "" if [[ -n "$rc_kernel_pakages" ]]; then echononl "Remove not fully deinstalled kernel packages? [\033[1myes/no\033[m]: " read OK while [[ "${OK,,}" != "yes" ]] && [[ "${OK,,}" != "no" ]] ; do echononl "\033[33mWrong entry!\033[m [\033[1myes/no\033[m]: " read OK done [[ "${OK,,}" = "yes" ]] || rc_kernel_pakages="" fi if [[ -n "$unused_kernel_pakages" ]]; then echononl "Remove unsed kernel packages? [\033[1myes/no\033[m]: " read OK while [[ "${OK,,}" != "yes" ]] && [[ "${OK,,}" != "no" ]] ; do echononl "\033[33mWrong entry!\033[m [\033[1myes/no\033[m]: " read OK done [[ "${OK,,}" = "yes" ]] || unused_kernel_pakages="" fi fi blank_line echononl "Removing not fully deinstalled kernel packages.." if [[ -z "$rc_kernel_pakages" ]] ; then echo_skipped else echo_wait apt-get remove --purge -y $rc_kernel_pakages > $log_file 2>&1 if [[ $? -eq 0 ]] ; then echo_ok else echo_failed error "$(cat "$log_file")" fatal "Removing not fully deinstalled kernel packages failed." fi fi echononl "Removing unsed kernel packages.." if [[ -z "$unused_kernel_pakages" ]] ; then echo_skipped else echo_wait apt-get remove --purge -y $unused_kernel_pakages > $log_file 2>&1 if [[ $? -eq 0 ]] ; then echo_ok else echo_failed error "$(cat "$log_file")" fatal "Removing unused kernel packages failed." fi fi echononl "Update grub boot manager.." update-grub > $log_file 2>&1 if [[ $? -eq 0 ]] ; then echo_ok else echo_failed error "$(cat "$log_file")" fi blank_line clean_up 0