Maintaining /var/cache/pacman/pkg for System Safety

From Manjaro Linux
Jump to: navigation, search


Introduction:

This is an How-To that may hopefully introduce what will be seen as good policy for the maintenance of the package cache /var/cache/pacman/pkg on your system.

(Not only) I have found that when using a rolling release upgrade system, as Manjaro does, there can be situations that crop up from time to time where after upgrading one package or your whole system, you find that bug(s) (programming errors, not viruses) have infected your system.

In this situation we need to be able to downgrade the offending package or packages to get rid of the bug(s).

If you have the previously installed version of the offending package's installation files still sitting in your /var/cache/pacman/pkg directory; getting out of this kind of problem is pretty quick & easy. (Once you've done it once or twice anyway.)


Introducing pkgCacheClean:

Note: The previous tool called CacheClean has been replaced by pkgCacheClean as it uses pacman's method to check package versions & also checks to see if a package is actually installed.

A great tool for managing how many versions you want to keep in your cache is pkgCacheClean. The default setup for pkgCacheClean will keep the two most recent versions of the installation files in the /var/cache/pacman/pkg directory & quickly delete the other versions. This saves storage space on my HDD.

As of this writing pkgCacheClean is available via the AUR. (More to come on pkgCacheClean.)


So Why Do I Need to Keep Two (or more) Versions of My Installed Packages in the Cache Again?

The reason I recommend keeping these two (or three if you use the Unstable repo) versions of your installation packages in your cache, is that if you upgrade your system & a package (or packages) have been installed that cause your system not to run how you want it to, you can easily downgrade to the previous version of the package(s) to get away from the bug(s) by using the following command in the Terminal (more on simplifying this later):

sudo pacman -U <path/to/package/package.name>


What if I Don't Know Which Package(s) Have the Bug(s)?

If you don't know which package(s) caused the problem, you can look in the /var/log/pacman.log where you can scroll down to the date & time of the day that you installed the "trouble" & see the packages listed.

You can then use this list to know which files to replace with their previously installed versions.

With more experience using the GNU/Linux system, you can be a bit more discerning & remove/replace only the most likely candidate(s), though the forum should be a great help in such circumstances.


On the Three Levels of System Stability:

It is worth noting that there are basically three levels of stability re. the repos in Manjaro:

The Stable repo packages - are the safest & a little older than those in the Arch Stable repos.

The Testing repo packages - are similar to what Arch is using as their Stable.

The Unstable repo packages - really should only be used by users who know what they are doing, as these are most likely to bring unexpected problems. Which means users of the Unstable repo are definitely the most likely candidates to have a need to use the information in this wiki page.

Also, if you are using the unstable repos you may be best off to save an extra earlier version in your cache - making it three instead of two versions that you can call on to use if need be in the downgrade process. (More on this later.)


For more information on the Manjaro repos, please see this Manjaro wiki page: [1]


pkgCacheClean Installation, Usage & Aliases:

If the topic of this thread means anything to you, you will want to install the pkgCacheClean package from AUR. The following command shows how to do this using yaourt:

yaourt -S pkgcacheclean

Once pkgCacheClean is installed, we can make using pkgCacheClean quicker & easier to remember by creating & adding an alias to your ~/.bashrc .

To do this, open your ~/.bashrc in your favourite text editor by entering the following command at the Terminal prompt (substituting the "<text.editor.name>" with your choice of editor e.g. gedit):

<text.editor.name> ~/.bashrc

Then add the following alias to the ~/.bashrc (I usually always add a comment to any aliases I make, in an effort to help me know what I've done in the past - when I look there in the future):

# deletes all but the last 2 versions of 
# installed packages in the pacman cache:

alias pcc="sudo pkgcacheclean 1"

While you are there add the following alias, as it will make it very quick & easy to reinitialise your Terminal after you edit your ~/.bashrc - allowing you to use any edits you have made virtually straight away. (Saves you from having to restart your Terminal process.):

# reinitialises the Terminal so as to be able
# to use any changes made to the ~/.bashrc :

alias src="source .bashrc"		

Now after saving your changes to ~/.bashrc , you should enter the following command at the Terminal prompt (in the future you'll be able to use the above alias to issue this same command, but the Terminal has to be reinitialised this one time first to activate the alias):

source .bashrc

In future, whenever you edit your ~/.bashrc , & have saved your editing, you should immediately use the src alias to activate your new changes. This src alias/command saves you having to shut down & restart the Terminal.


After the above is done, we can now use pkgCacheClean via entering its alias pcc at the Terminal prompt, which will cause pkgCacheClean to go to work deleting any & all files from the /var/cache/pacman/pkg directory, except the currently installed & the previous version.


OR If you change the 1 to a 2 (or a greater number) in the following pkgCacheClean command line - pkgCacheClean will then keep that number of package versions, as well as the currently installed packages, in your cache, deleting all those that are older versions:

# Number at the end of the command sets the number
# of the most recent versions of installation
# files to be kept (as well as those packages that
# are currently installed) in /var/cache/pacman/pkg :

alias pcc="sudo pkgcacheclean 2" 


If you use the pkgCacheClean command often, it will take next to no time at all to do its job. If there is nothing to delete, it quickly returns you to the command prompt.


Alternative to pkgCacheClean: pacman itself

You can also process this operation using pacman's own package cleaning script: paccache. Its functionnality is similar to pkgCacheClean's. For example, to delete all cached packages but the last 3 versions, enter this command in a terminal:

paccache -rk3

You can get more information about paccache's options by using the following command:

paccache -h


Downgrade a Single package at a time from either cache or A.L.A

There is package in the AUR called downgrade that runs from the terminal & makes downgrading individual packages very easy. See this wiki page for the details: [2]

Also the Arch Linux Archive - A.L.A is well worth knowing about: [3]

How to Downgrade Multiple Packages:

If there are a number of packages that need to be downgraded it is advisable to first change directory to the directory holding the packages by entering the following command in the Terminal:

cd /var/cache/pacman/pkg


After which, you basically have two choices:

You can list them one after the other, separated by a space as follows:

sudo pacman -U <package.name> <package.name> <package.name>

OR. You can go through them individually, which is quite simple & depending on the circumstances, it can be quicker.

To do this you use the following command:

pacman -U <package.name> 

on the first file; after which you can just keep on recalling the previously downgraded package's command line using the cursor control arrow key on your keyboard; then you delete the previously used name (leaving the shared extension .pkg.tar.xz ) & fill in the new name for the next package that you wish to downgrade.

To make this process somewhat more streamlined, read on?


~/bashrc Aliases to Streamline the Downgrade Process:

When it comes to having to downgrade packages from the pacman cache, I've found that setting up a couple of aliases in the ~/.bashrc file makes it a lot easier & quicker.

The following section of script is from my ~/.bashrc & includes information regarding each of the aliases.

To add the following to your ~/.bashrc you will need to open your ~/.bashrc in your favourite text editor, using your preferred method, or as previously described in this document:


#----------------------------------------------------

# pacman -U ,installs package(s) from the specified location on disk,
# usually /var/cache/pacman/pkg/ the following command will
# echo the command to the Terminal where it can be copied by
# highlighting it with your mouse, copied & pasted to the command
# line, where it will have the file name added & used:

alias pU="echo sudo pacman -U /var/cache/pacman/pkg/-FILE.NAME- .pkg.tar.xz "

# in cases where multiple file up/downgrades are required you may choose to
# first CD to the cache before running the pacman -U command multiple times -
# as adding all of the package names to the command line may be slower than
# recalling the previous pacman -U command & editing the name individually:

alias pkg="cd /var/cache/pacman/pkg"

alias pU2="echo sudo pacman -U -FILE.NAME- .pkg.tar.xz"

#----------------------------------------------------


The above aliases save both in time spent typing & remembering the command lines (which is more important to some of us than others ).


Telling pacman.conf Not to upgrade files:

If you have had to downgrade one or more packages due to bugs, it can sometimes take a while for the problem to be fixed; though generally the fixes come pretty quickly.

So there is a need to tell Pacman that we don't want to upgrade this or these files (else we'll just have to go to the trouble of downgrading them again!)

To do this we need to open the /etc/pacman.conf file in our favourite text editor. Using the Terminal we do it like so:

sudo <text.editor.name> /etc/pacman.conf

Now look down through the pacman.conf file in your editor until you find the following:

# Pacman won't upgrade packages listed in IgnorePkg and members of IgnoreGroup
#IgnorePkg   =

You add the names of any packages that need to be held back from upgrade to the IgnorePkg = line, separating them with a space. You also MUST remove the # comment from the start of the line, or else the line will be ignored.

Here is an example with the comment symbol # removed & three packages held back from being upgraded:

# Pacman won't upgrade packages listed in IgnorePkg and members of IgnoreGroup
IgnorePkg   = firefox sakura vlc

You will find more information on configuring /etc/pacman.conf here: [4]


pkgCacheClean has been added to the allservers.sh Script:

I've added pkgCacheClean to the allservers.sh script where it will automatically do its thing after each of the different types of system upgrades that are available in the allservers.sh menu.

See here for the relevant details: [5]


Support

Following is a link to this page's forum counterpart where you can post any related feedback: [6]