base-files: vkpurge: add wildcard support

closes #4614

Co-authored-by: maxice8 <thinkabit.ukim@gmail.com>
Co-authored-by: Duncaen <duncaen@voidlinux.org>
This commit is contained in:
Martin Dimov 2018-11-11 02:46:21 +01:00 committed by maxice8
parent a7fadf0e0a
commit f39996c2ff
3 changed files with 115 additions and 148 deletions

View file

@ -3,135 +3,92 @@
# A simple script to remove old kernel files/modules.
# Brought to you by yours truly Juan RP in the Public Domain.
#
: ${progname:=$(basename $0)}
: "${progname:="${0##*/}"}"
case "$(xbps-uhelper arch)" in
aarch64*|ppc64*) KIMAGE="vmlinux";;
*) KIMAGE="vmlinuz";;
esac
usage()
{
usage() {
cat <<_EOF
Usage: $progname <target> [<version>]
Targets:
list Lists old installed kernels.
rm <version...|all> Remove kernel <version> or all old kernels.
Example:
$ $progname list
$ $progname rm 4.9.81_1
Usage: $progname list [version ...]
$progname rm all
$progname rm <version ...>
_EOF
exit 1
}
list_kernels()
{
local k kpkg installed kver _f
list_kernels() {
running=$(uname -r)
installed=$(xbps-query -o "/boot/vmlinu[xz]-*" 2>/dev/null | awk '{print $2}')
for arg; do
case "$arg" in
all) pattern="*" ;;
*) pattern=$arg ;;
esac
for k in /boot/vmlinu[xz]-*; do
case "$installed" in
*"$k"*) continue ;;
esac
kver=${k##*-}
case "$kver" in
"$running") ;;
$pattern) printf "%s\n" "$kver" ;;
esac
done
done | sort -u
}
for k in /boot/$KIMAGE-*; do
_f=$(basename $k)
kver=$(echo ${_f}|sed -e 's|-rc|rc|')
installed=$(xbps-query -o "$k" 2>/dev/null)
[ -n "$installed" ] && continue
kpkg="$(xbps-uhelper getpkgversion ${_f} 2>/dev/null)"
[ "$(uname -r)" = "$kpkg" ] && continue
[ -n "$kpkg" ] && echo "$kpkg"
run_hooks() {
dir="$1"
kver="$2"
for d in /etc/kernel.d/"${dir}"/*; do
[ -x "$d" ] || continue
printf "Running %s kernel hook: %s...\n" "$dir" "${d##*/}"
"$d" kernel "$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/$KIMAGE-${rmkver} -a ! -d /lib/modules/${rmkver} ]; then
echo "Kernel ${rmkver} not installed."
exit 0
fi
installed=$(xbps-uhelper version "linux${rmkver%*.*}" 2>/dev/null)
if [ -n "$installed" -a "$installed" = "$rmkver" ]; then
echo "Kernel $rmkver is currently installed."
exit 0
fi
remove_kernel() {
rmkver="$1"
# Execute pre-remove kernel hooks.
run_hooks pre-remove $rmkver
# Remove kernel files in /boot.
for f in config System.map $KIMAGE; do
kfile="/boot/${f}-${rmkver}"
[ ! -f "${kfile}" ] && continue
echo "Removing ${kfile}..."
rm -f ${kfile}
run_hooks pre-remove "$rmkver"
for f in \
/boot/config-"${rmkver}" \
/boot/System.map-"${rmkver}" \
/boot/vmlinu[xz]-"${rmkver}" \
/usr/lib/modules/"${rmkver}"; do
[ -e "$f" ] || continue
printf "Removing %s...\n" "$f"
rm -rf "$f"
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
# Remove kernel-headers.
if [ -d /usr/src/kernel-headers-${rmkver} ]; then
rm -rf /usr/src/kernel-headers-${rmkver}
fi
# Remove debugging symbols.
dfile=/usr/lib/debug/boot/vmlinux-${rmkver}
if [ -f "${dfile}" ]; then
echo "Removing ${dfile}..."
rm -f ${dfile}
fi
if [ -d /usr/lib/debug/usr/lib/modules/${rmkver} ]; then
echo "Removing /usr/lib/debug/usr/lib/modules/${rmkver}..."
rm -rf /usr/lib/debug/usr/lib/modules/${rmkver}
fi
# Remove dtb files
if [ -d "/boot/dtbs/dtbs-${rmkver}" ]; then
echo "Removing /boot/dtbs/dtbs-${rmkver}..."
rm -rf "/boot/dtbs/dtbs-${rmkver}"
fi
run_hooks post-remove "$rmkver"
for f in \
/usr/src/kernel-headers-"${rmkver}" \
/usr/lib/debug/boot/vmlinu[xz]-"${rmkver}" \
/usr/lib/debug/usr/lib/modules/"${rmkver}" \
/boot/dtbs/dtbs-"${rmkver}"; do
[ -e "$f" ] || continue
printf "Removing %s...\n" "$f"
rm -rf "$f"
done
}
if [ "$1" = "list" ]; then
list_kernels
elif [ "$1" = "rm" ]; then
if [ "$(id -u)" -ne 0 ]; then
echo "You have to run this script as root!"
exit 1
fi
if [ -z "$2" ]; then
usage
elif [ "$2" = "all" ]; then
kernels=$(list_kernels)
for k in ${kernels}; do
echo "Removing kernel $k files ..."
remove_kernel "$k"
done
else
case "$1" in
list) list_kernels "${2:-all}" ;;
rm)
[ -z "$2" ] && usage
if [ "$(id -u)" -ne 0 ]; then
printf "You have to run this script as root!\n" >&2
exit 1
fi
shift
for k; do
echo "Removing kernel $k files ..."
remove_kernel "$k"
for kver in $(list_kernels "$@"); do
printf "Removing kernel %s...\n" "$kver"
remove_kernel "$kver"
done
fi
else
usage
fi
;;
*) usage ;;
esac
exit 0

View file

@ -1,4 +1,4 @@
.Dd July 24, 2016
.Dd April 6, 2019
.Dt VKPURGE 8
.Os
.Sh NAME
@ -7,52 +7,62 @@
.Sh SYNOPSIS
.Nm
.Cm list
.Op Ar version
.Nm
.Cm rm all
.Nm
.Cm rm
.Op Ar versions\ ... | Cm all
.Ar version ...
.Sh DESCRIPTION
The
.Nm
command is used to properly remove old or unwanted kernels from Void Linux
systems, by running the appropiate removal hooks and doing the removal.
.Ss Justification
utility lists and removes removable kernel versions leaved
behind by
.Xr xbps-install 1
will leave old kernels in place when the kernel package gets upgraded, and
.Xr xbps-remove 1
will only be able to remove the latest kernel in a version,
for example 4.3.4_4, but it would need to leave behind 4.3.4_3.
.Sh OPTIONS
.Bl -tag -width Ds
.It Ic list
Provide a list of installed kernel versions.
.It Ic rm
Remove kernels.
Must be followed by either a version number or the literal
.Sq Ic all .
and
.Xr xbps-remove 1 .
.Nm
runs the appropriate removal hooks and deletes the kernel,
modules and other miscellaneous files.
.Pp
.Nm
only removes kernel versions that are:
.Bl -bullet -compact
.It
not currently booted.
.It
not provided by any installed xbps package.
.El
.Pp
The
.Ar version
arguments support
.Xr sh 1
pattern matching.
.Pp
The following commands are available:
.Bl -tag -width 4n
.It Ic list Op Ar version
List removable kernel versions.
Optionally list only versions matching
.Ar version .
.It Ic rm all
Remove all removable kernel versions.
.It Ic rm Ar version ...
Remove all removable kernel versions matching
.Ar version .
.El
.Sh EXIT STATUS
.Nm
exits with a 0 code unless the help text was printed.
Since there is no proper invocation to trigger a print of the help,
this can be summarized: if there was a proper invocation of
.Nm
then the command will exit 0.
Otherwise, the exit code is 1.
.Ex -std
.Sh EXAMPLES
The following will list all installed kernels.
The following command lists installed kernels matching a pattern.
.Bd -literal -offset indent
$ vkpurge list
$ vkpurge list '4.18.*'
.Ed
.Pp
The following command will delete the kernel version 2.6.39_2 with all
kernel-specific files such as compiled modules.
The following command will delete the kernel versions matching the pattern and a specified version.
.Bd -literal -offset indent
# vkpurge rm 2.6.39_2
.Ed
.Pp
The following command will delete the kernels with version 2.6.39_2 and 4.3.4_1
.Bd -literal -offset indent
# vkpurge rm 2.6.39_2 4.3.4_1
# vkpurge rm '2.6.*' 4.3.4_1
.Ed
.Sh SEE ALSO
.Xr xbps-install 1 ,

View file

@ -1,7 +1,7 @@
# Template file for 'base-files'
pkgname=base-files
version=0.140
revision=3
revision=4
bootstrap=yes
depends="xbps-triggers"
short_desc="Void Linux base system files"