2011-07-10 13:02:46 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# A simple script to remove old kernel files/modules.
|
|
|
|
# Brought to you by yours truly Juan RP in the Public Domain.
|
|
|
|
#
|
|
|
|
: ${progname:=$(basename $0)}
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
cat <<_EOF
|
|
|
|
Usage: $progname <target> [<version>]
|
|
|
|
|
|
|
|
Targets:
|
2011-07-10 13:12:00 +02:00
|
|
|
list Lists old installed kernels.
|
|
|
|
rm <version|all> Remove kernel <version> or all old kernels.
|
2011-07-10 13:02:46 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
$ $progname list
|
|
|
|
$ $progname rm 2.6.39.2
|
|
|
|
_EOF
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
list_kernels()
|
|
|
|
{
|
2011-11-08 08:18:09 +01:00
|
|
|
local k kpkg installed kver kvertmp skip
|
2011-07-10 13:02:46 +02:00
|
|
|
|
|
|
|
for k in /var/lib/initramfs-tools/*; do
|
2011-11-08 08:18:09 +01:00
|
|
|
kver=$(basename $k)
|
|
|
|
kvertmp=$(basename $k|sed -e 's|-rc|rc|')
|
2011-07-10 13:02:46 +02:00
|
|
|
for kpkg in kernel kernel-snapshot; do
|
|
|
|
installed=$(xbps-uhelper -r / version $kpkg)
|
2011-11-08 08:18:09 +01:00
|
|
|
if [ "$installed" = "$kvertmp" ]; then
|
2011-07-20 18:13:09 +02:00
|
|
|
skip=1
|
|
|
|
break
|
2011-07-10 13:02:46 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ -n "$skip" ]; then
|
|
|
|
unset skip
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
echo "$kver"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
run_hooks()
|
|
|
|
{
|
|
|
|
local dir="$1"
|
|
|
|
local kver="$2"
|
|
|
|
local d
|
|
|
|
|
|
|
|
for d in /etc/kernel.d/${dir}/*; do
|
|
|
|
[ ! -x "$d" ] && continue
|
|
|
|
echo "Running ${dir} kernel hook: $(basename $d)..."
|
|
|
|
$d kernel $kver
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_kernel()
|
|
|
|
{
|
|
|
|
local rmkver="$1"
|
|
|
|
local installed f kfile
|
|
|
|
|
|
|
|
if [ ! -f /boot/vmlinuz-${rmkver} -a ! -d /lib/modules/${rmkver} ]; then
|
|
|
|
echo "Kernel ${rmkver} not installed."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
installed=$(xbps-uhelper -r / version $rmkver)
|
|
|
|
if [ -n "$installed" ]; then
|
|
|
|
echo "Kernel $rmkver is currently installed."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Execute pre-remove kernel hooks.
|
|
|
|
run_hooks pre-remove $rmkver
|
|
|
|
# Remove kernel files in /boot.
|
|
|
|
for f in config System.map vmlinuz; do
|
|
|
|
kfile="/boot/${f}-${rmkver}"
|
|
|
|
[ ! -f "${kfile}" ] && continue
|
|
|
|
echo "Removing ${kfile}..."
|
|
|
|
rm -f ${kfile}
|
|
|
|
done
|
|
|
|
# Remove kernel modules
|
|
|
|
if [ -d "/lib/modules/${rmkver}" ]; then
|
|
|
|
echo "Removing /lib/modules/${rmkver}..."
|
|
|
|
rm -rf /lib/modules/${rmkver}
|
|
|
|
fi
|
|
|
|
# Execute post-remove kernel hooks.
|
|
|
|
run_hooks post-remove $rmkver
|
2011-11-08 08:18:09 +01:00
|
|
|
|
|
|
|
# Make sure there are no stale files.
|
|
|
|
if [ -f /var/lib/initramfs-tools/${rmkver} ]; then
|
|
|
|
rm -f /var/lib/initramfs-tools/${rmkver}
|
|
|
|
fi
|
2011-07-10 13:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$1" = "list" ]; then
|
|
|
|
list_kernels
|
|
|
|
elif [ "$1" = "rm" ]; then
|
2011-07-10 13:12:00 +02:00
|
|
|
if [ -z "$2" ]; then
|
|
|
|
usage
|
|
|
|
elif [ "$2" = "all" ]; then
|
|
|
|
kernels=$(list_kernels)
|
|
|
|
for k in ${kernels}; do
|
|
|
|
remove_kernel "$k"
|
|
|
|
done
|
|
|
|
else
|
|
|
|
remove_kernel "$2"
|
|
|
|
fi
|
2011-07-10 13:02:46 +02:00
|
|
|
else
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|