pacman cache helper: automatic reminders, manual package cache cleaner

Today my machine refused to boot as a result of the pacman package cache filling up my disk (I blame the texlive updates). Ironically, a few days ago I saw (and ignored) a post on /r/archlinux by someone sharing that they just found out about cleaning the cache.

Like with many aspects of Arch Linux, cleaning the package cache is not done automatically. Arch is about putting the power (mostly through choices) in the hands of the user. Having the system decide which old packages to keep and which to throw away goes against this idea. Especially, on a bleeding-edge rolling-release distro it is convenient to have multiple recent versions of the packages you have installed. I have often rolled back packages and sometimes several versions in the past.

The downside is that, if unattended, the package cache (at /var/cache/pacman/pkg) will slowly grow in size and eventually fill up the (hopefully separate /var) partition. Problems resulting from no free space can manifest itself in many different and ambiguous ways. This time my system refused to boot because it wouldn’t start the rfkill service (a tool for enabling and disabling wireless devices). Go figure! Arch Linux provides some simple but effective tools for cleaning cache, but this needs to be done manually. Most users learn the hard way to routinely check and monitor disk usage or even develop a sixth sense for when it’s cleaning time. Some users don’t learn at all… like me.

So to finally remedy this, I wrote a small shell script that wraps around pacman (or pacaur if you like) and tells the user when the partition containing the package cache is above a certain threshold and offers to run the default clean commands. The reminder is automatic, while the cleaning remains a choice for the user. It is possible to achieve similar results with the pacman hooks, but I find that scripts provide more flexibility. You can easily modify this script to, for instance, only present the warning or clean up automatically without intervention. There are some basic settings:

  • THRESHOLD: threshold at which to start warning and offer clean up (80% by default)
  • RETENTION: number of recent versions of packages to keep (3 by default)
  • RETENTION_UNINSTALLED: number of recent versions of uninstalled packages to keep (0 by default)

The script is given below. It blindly passes all arguments given to pacman. Likewise, it returns the exit code that pacman throws. For usage, I recommend aliasing the command to pacman by putting alias pacman='/path/to/pacmancachehelper.sh' in your ~/.bashrc. Hope you find this useful!

#!/bin/sh
# pacmancachehelper - pacman wrapper offering cache clean-up when disk gets full
# Copyright (C) 2017 runninghighonspace.com - All Rights Reserved
# Permission to copy and modify is granted under the GNU General Public License, version 2.
# Last revised 8/11/2017

# Capacity warning threshold in percent
THRESHOLD=80

# Number of recent version of installed and uninstalled packages to keep.
RETENTION=3
RETENTION_UNINSTALLED=0 

# Autodetect cache dir, change this if this doesn't work for you.
CACHEDIR=$(awk '/Cache/ {print $3}' /etc/pacman.conf)

# Call pacman with the arguments supplied
pacman "$@"
PACSTATUS=$?

# Find out usage of partition which contains the cache dir
USAGE=$(df -P "$CACHEDIR" | tail -1 | awk '{ print $5 }' | sed 's/%//')

# Test if usage is above threshold
if [ "$USAGE" -ge "$THRESHOLD" ]; then

    # Prompt warning and get response from user
    read -n 1 -r -p "$(tput bold):: Ruh-roh, it looks like your pkg cache is getting full (${USAGE}%/${THRESHOLD}%). Would you like to clean up? [Y/n]$(tput sgr0) " RESPONSE

    # Echo for newline if enter was not pressed
    [ "$RESPONSE" != "" ] && echo ""

    # Take action based on the response
    case "$RESPONSE" in
        [yY]|"")
            paccache -ruk "$RETENTION_UNINSTALLED"
            paccache -rk "$RETENTION"
            ;;
        *)
            ;;
    esac
fi

# Exit with the exit status of pacman
exit $PACSTATUS

Recent Posts

Categories

koffieanon Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.