#!/bin/bash set -eou pipefail readonly VERSION="v0.2.2" readonly GITREPO="https://github.com/framps/raspberryTools" readonly MYSELF="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" readonly MYNAME=${MYSELF%.*} readonly DELETED_KERNELS_FILENAME="$MYNAME.krnl" function show_help() { echo "$MYSELF $VERSION ($GITREPO)" echo echo "Check for unnecessary kernels for the existing Raspberry hardware." echo "Optionally delete and save installed kernels for later reinstallation or reinstall the previously deleted kernels" echo echo "Usage: $MYSELF -i [-e] | -u [-e] | -h | -? | -v" echo "-e: deactivate dry run mode and modify system" echo "-i: reinstall kernels deleted with option -u" echo "-u: uninstall any unused kernels" echo "-h: display this help" echo "-v: display version" } function info() { echo "--- $@" } function error() { echo "??? $@" } function yesNo() { local answer=${1:0:1} answer=${1:-"n"} [[ "Yy" =~ $answer ]] return } function check4Pi() { dpkg --print-architecture | grep -q -E "arm(hf|64)" } function do_uninstall() { local availableKernels="$(dpkg --list | awk '/^ii[[:space:]]+linux-image/ { print $2 }')" local usedKernel="$(uname -a | awk '{ print "linux-image-" $3 }')" local unusedKernels="$(grep -v "$usedKernel" <<< "$availableKernels" | xargs -I {} echo "{}")" if [[ -z "$unusedKernels" ]]; then error "No unused kernels detected" exit 1 fi local keptKernel="$(grep "$usedKernel" <<< "$availableKernels" | xargs -I {} echo "{}")" if [[ -z "$keptKernel" ]]; then error "No kernel will be kept" exit 1 fi info "Following kernel will be kept" echo "$keptKernel" local numUnusedKernels="$(wc -l <<< "$unusedKernels")" info "Following $numUnusedKernels unused kernels will be deleted" echo "$unusedKernels" if (( $MODE_EXECUTE )); then local answer read -p "Are you sure to delete all $numUnusedKernels unused kernels ? (y/N) " answer if ! yesNo "$answer"; then exit 1 fi read -p "Do you have a backup ? (y/N) " answer if ! yesNo "$answer"; then exit 1 fi set +e if [[ -e /boot/$DELETED_KERNELS_FILENAME ]]; then sudo rm /boot/$DELETED_KERNELS_FILENAME (( $? )) && { error "Failure deleting /boot/$DELETED_KERNELS_FILENAME"; exit 42; } fi info "Saving $numUnusedKernels unused kernel names in /boot/$DELETED_KERNELS_FILENAME" echo "$unusedKernels" >> $DELETED_KERNELS_FILENAME; sudo mv $DELETED_KERNELS_FILENAME /boot (( $? )) && { error "Failure collecting kernels"; exit 42; } info "Removing $numUnusedKernels unused kernels" echo "$unusedKernels" | xargs sudo apt -y remove (( $? )) && { error "Failure removing kernels"; exit 42; } set -e fi } function do_install() { if [[ ! -e /boot/$DELETED_KERNELS_FILENAME ]]; then info "No unused kernels found" exit 0 fi local numUnusedKernels=$(wc -l /boot/$DELETED_KERNELS_FILENAME | cut -f 1 -d ' ') if (( ! $MODE_EXECUTE )); then info "Following $numUnusedKernels unused kernels will be installed" while IFS= read -r line; do echo "$line" done < /boot/$DELETED_KERNELS_FILENAME else local errorOccured=0 info "Installing $numUnusedKernels unused kernels" echo "$(