Pacman/Tips and tricks

Revision as of 08:16, 17 July 2013 by Ftravers(talk | contribs)(→‎Read-write cache: modified the read/write section as sharing the pacman DB seems like an error and cannot think when you would EVER want to do that!)

Identify files not owned by any package

Periodic checks for files outside of pacman database are recommended. These files are often some 3rd party applications installed using the usual procedure (e.g. ./configure && make && make install). Search the file-system for these files (or symlinks) using this simple script:

Note that one should not delete all files listed in non-db.txt without confirming each entry. There could be various configuration files, logs, etc., so use this list responsibly and only proceed after extensively searching for cross-references using grep.

Removing orphaned packages

For recursively removing orphans:

# pacman -Rs $(pacman -Qtdq)

The following alias is easily inserted into ~/.bashrc and removes orphans if found:

The dcron cron line seems to cause problems, that is why cut -d ' ' -f 1 is needed - to keep just the package name.

Listing official installed packages only

pacman -Qqn

This list packages that are found in the sync database(s). If the user has unofficial repositories configured, it will list packages from such repositories too.

Getting the dependencies list of several packages

Dependencies are alphabetically sorted and doubles are removed.
Note that you can use pacman -Qi to improve response time a little. But
you won't be able to query as many packages. Unfound packages are simply skipped
(hence the 2>/dev/null).
You can get dependencies of AUR packages as well if you use yaourt -Si,
but it will slow down the queries.

You should replace "$@" with packages, or put this line in a shell function.

Listing changed configuration files

If you want to backup your system configuration files you could copy all files in /etc/, but usually you're only interested in the files that you have changed. In this case you want to list those changed configuration files, we can do this with the following command:

# pacman -Qii | awk '/^MODIFIED/ {print $2}'

The following script does the same. You need to run it as root or with sudo.

Listing all packages that nothing else depends on

If you want to generate a list of all installed packages that nothing else depends on, you can use the following script. This is very helpful if you are trying to free hard drive space and have installed a lot of packages that you may not remember. You can browse through the output to find packages which you no longer need.

clean

#!/bin/bash
# This script is designed to help you clean your computer from unneeded
# packages. The script will find all packages that no other installed package
# depends on. It will output this list of packages excluding any you have
# placed in the ignore list. You may browse through the script's output and
# remove any packages you do not need.
# Enter groups and packages here which you know you wish to keep. They will
# not be included in the list of unrequired packages later.
ignoregrp="base base-devel"
ignorepkg=""
# Temporary file locations
tmpdir=/tmp
ignored=$tmpdir/ignored
installed=$tmpdir/installed
# Generate list of installed packages and packages you wish to keep.
echo $(pacman -Sg $ignoregrp | awk '{print $2}') $ignorepkg | tr ' ' '\n' | sort | uniq > $ignored
pacman -Qq | sort > $installed
# Do not loop packages you are keeping
loop=$(comm -13 $ignored $installed)
# Check each remaining package. If package is not required by anything and
# is not on your ignore list, print the package name to the screen.
for line in $loop; do
check=$(pacman -Qi $line | awk '/Required By/ {print $4}')
if [ "$check" == 'None' ]; then echo $line; fi
done
# Clean up $tmpdir
rm $ignored $installed

If you install expac you can run expac "%n %N" -Q $(expac "%n %G" | grep -v ' base') | awk '$2 == "" {print $1}' which should give the same results but much faster.

The following script has the option to exclude files like above, but uses expac:

2. Edit pacman.conf and add this repository before the other ones (e.g. extra, core, etc.). This is important. Don't just uncomment the one on the bottom. This way it ensures that the files from the CD/DVD/USB take precedence over those in the standard repositories:

Note: If there is a package that needs to be removed from the repository, read up on repo-remove.

Once the local repository has been made, add the repository to pacman.conf. The name of the db.tar.gz file is the repository name. Reference it directly using a file:// url, or access it via FTP using ftp://localhost/path/to/directory.

Network shared pacman cache

Read-only cache

If you're looking for a quick and dirty solution, you can simply run a standalone webserver which other computers can use as a first mirror: darkhttpd /var/cache/pacman/pkg. Just add this server at the top of your mirror list. Be aware that you might get a lot of 404 errors, due to cache misses, depending on what you do, but pacman will try the next (real) mirrors when that happens.

Read-write cache

Tip: See pacserve for an alternative (and probably simpler) solution than what follows.

In order to share packages between multiple computers, simply share /var/cache/pacman/ using any network-based mount protocol. This section shows how to use shfs or sshfs to share a package cache plus the related library-directories between multiple computers on the same local network. Keep in mind that a network shared cache can be slow depending on the file-system choice, among other factors.

Then, to share the actual packages, mount /var/cache/pacman/pkg from the server to /var/cache/pacman/pkg on every client machine.

To have shared package databases, mount /var/lib/pacman/sync/{core,extra,testing,community} in the same way. Proceed to place the appropriate lines in /etc/fstab. when would you EVER do this??? This seems to be a very bad idea to suggest to anyone.

Preventing unwanted cache purges

By default, pacman -Sc removes package tarballs from the cache that correspond to packages that are not installed on the machine the command was issued on. Because pacman cannot predict what packages are installed on all machines that share the cache, it will end up deleting files that should not be.

To clean up the cache so that only outdated tarballs are deleted, add this entry in the [options] section of /etc/pacman.conf:

CleanMethod = KeepCurrent

Backing up and retrieving a list of installed packages

It is good practice to keep periodic backups of all pacman-installed packages. In the event of a system crash which is unrecoverable by other means, pacman can then easily reinstall the very same packages onto a new installation.

First, backup the current list of non-local packages:

$ pacman -Qqen > pkglist.txt

Store the pkglist.txt on a USB key or other convenient medium or gist.github.com or Evernote, Dropbox, etc.

Copy the pkglist.txt file to the new installation, and navigate to the directory containing it.

Issue the following command to install from the backup list:

# pacman -S $(< pkglist.txt)

In the case you have a list which was not generated like mentioned above, there may be foreign packages in it (i.e. packages not belonging to any repos you have configured, or packages from the AUR).

In such a case, you may still want to install all available packages from that list:

Most likely, pacman's database of installed software, /var/lib/pacman/local, has been corrupted or deleted. While this is a serious problem, it can be restored by following the instructions below.

Firstly, make sure pacman's log file is present:

$ ls /var/log/pacman.log

If it does not exist, it is not possible to continue with this method. You may be able to use Xyne's package detection script to recreate the database. If not, then the likely solution is to re-install the entire system.

This way two files will be created: files.list with package files, still present on machine and pkglist.orig, packages from which should be downloaded. Later operation may result in mismatch between files of older versions of package, still present on machine, and files, found in new version. Such mismatches will have to be fixed manually.

Here is a way to automatically restrict second list to packages available in a repository:

$ { cat pkglist.orig; pacman -Slq; } | sort | uniq -d > pkglist

Check if some important base package are missing, and add them to the list:

$ comm -23 <(pacman -Sgq base) pkglist.orig >> pkglist

Proceed once the contents of both lists are satisfactory, since they will be used to restore pacman's installed package database; /var/lib/pacman/local/.

Performing the recovery

--log /dev/null allows to avoid needless pollution of pacman log, --needed will save some time by skipping packages, already present in database, --nodeps will allow installation of cached packages, even if packages being installed depend on newer versions. Rest of options will allow pacman to operate without reading/writing filesystem.

Update the local database so that packages that are not required by any other package are marked as explicitly installed and the other as dependences. You will need be extra careful in the future when removing packages, but with the original database lost is the best we can do.

Recovering a USB key from existing install

If you have Arch installed on a USB key and manage to mess it up (e.g. removing it while it is still being written to), then it is possible to re-install all the packages and hopefully get it back up and working again (assuming USB key is mounted in /newarch)