bundles/apt: add a bit of code to remove old, unused kernel images

This commit is contained in:
Franzi 2021-04-02 08:11:17 +02:00
parent 8a0c8f32ae
commit 7ca24d27d3
Signed by: kunsi
GPG key ID: 12E3D2136B818350
2 changed files with 21 additions and 0 deletions

View file

@ -48,6 +48,26 @@ fi
DEBIAN_FRONTEND=noninteractive apt-get -y -q autoclean
DEBIAN_FRONTEND=noninteractive apt-get -y -q autoremove
% if clean_old_kernels:
existing=$(dpkg --get-selections | grep -E '^linux-(image|headers)-[0-9]' || true)
if [[ -z "$existing" ]]
then
echo "ERROR: No installed kernels found! Aborting!" >&2
exit 1
fi
current=$(uname -r | sed -r 's/-[a-zA-Z]+$//')
latest=$(echo "$existing" | sort --version-sort -t- -k 3,4 | tail -n 1 | sed -r 's/[^0-9]+([0-9]\.[^-]+-[0-9]+).*/\1/')
todelete=$(echo "$existing" | grep -v -E "($current|$latest)" | awk '{ print $1 }' || true)
if [[ -n "$todelete" ]]
then
DEBIAN_FRONTEND=noninteractive apt-get -qy purge $todelete
fi
% endif
if [[ -f /var/run/reboot-required ]]
then
% if 'mail' in data:

View file

@ -31,6 +31,7 @@ files = {
'content_type': 'mako',
'mode': '0700',
'context': {
'clean_old_kernels': node.metadata.get('apt/clean_old_kernels', True),
'data': node.metadata.get('apt/unattended-upgrades', {}),
}
},