Files
RPS-Client/data/remove_unused_kernel.sh

146 lines
4.0 KiB
Bash

#!/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 "$(</boot/$DELETED_KERNELS_FILENAME)"
while IFS= read -r line; do
sudo apt -y install $line
set +e
(( errorOccured |= $? ))
set -e
done < /boot/$DELETED_KERNELS_FILENAME
if (( ! errorOccured )); then
sudo rm /boot/$DELETED_KERNELS_FILENAME
else
error "Errors occured when installing kernels"
fi
fi
}
echo "$MYSELF $VERSION ($GITREPO)"
if ! check4Pi; then
error "No RaspberryPi detected"
exit 1
fi
MODE_INSTALL=0
MODE_UNINSTALL=0
MODE_EXECUTE=0
if (( $# == 0 )); then
MODE_UNINSTALL=1
else
while getopts ":ehiuv?" opt; do
case "$opt" in
e) MODE_EXECUTE=1
;;
h|\?)
show_help
exit 0
;;
i) MODE_INSTALL=1
;;
u) MODE_UNINSTALL=1
;;
v) echo "$MYSELF $VERSION"
exit 0
;;
*) echo "Unknown option $opt"
show_help
exit 1
;;
esac
done
fi
if (( $MODE_INSTALL )); then
do_install
elif (( $MODE_UNINSTALL )); then
do_uninstall
else
show_help
fi