January 2nd, 2005, 08:50
I'm sure no one will need to do this, but in case you do, these are some tactics to rebuild you package database.

Starting From Scratch

If for some reason you lose your entire /var/db/pkg folder, there are a few techniques you can use to revive you package listing. With the entire DB gone, you can see that pkg_info returns nothing. So now, you have to rebuild your list. Even with the package registration gone, you can still install packages just fine. When ports are installed, it looks for a key file from a port instead of the actual package registration. Unfortunatly, with all of this gone, portupgrading is pretty much pointless since it has no idea what version each package is, and if it's even installed. It's good to know it's still there and working, but this does pose a problem with rebuilding your database. With all the dependencies being satisfied, you are only able to install one package at a time. When you reinstall one, the deps will not come with it since they are already there.

So how do we do this? Well, there are a few ways. First, you can reinstall everything you remember installing, and checking the deps from the info of the port. When looking for a port, make use of the search function in the ports tree. Let's say we are reinstalling php4, first we search to find out where it is:

> cd /usr/ports/
> make search key="php4" | more

<-- snipp -->
Port: php4-4.3.10
Path: /usr/ports/lang/php4
Info: PHP Scripting Language (Apache Module and CLI)
B-deps: apache-1.3.33_1 bison-1.75_2 expat-1.95.8 gettext-0.14.1 libiconv-1.9.2_1 m4-1.4.1 perl-5.8.5
R-deps: apache-1.3.33_1 expat-1.95.8 perl-5.8.5
<-- snipp-->

From that command we search through the entire Index for "php4". Of course the results are huge, but they spaced well enough to read though the titles fairly easy. Here we see the Port name, Path, Info, Maintainer, and Dependencies. If you don't know where the port is for a certain app,this is the best way to find it. The Dependencies are what you want to use as a reference to what to install. This way once you reinstall the port, you can then do the deps so that you don't forget something that is installed and not in the database. Another way to reference dependencies is to run "make install clean" when reinstalling you ports. When it runs the clean, you will see all the dpendencies for that port.

The other method is to use the contents of /usr/ports/distfiles as your reference. Just run:

ls -lR /usr/ports/distfiles | more

In another term go down the list of files in distfiles, and start going into each port and running "make reinstall" or "make install clean". This is the best way, as long as you haven't cleaned out your distfiles directory (which you really shouldn't unless /usr is extrememly full. Lot's of source packages are used more than once). Instead of using the "make search" function, you can always use the FreshPorts Website ( for your port information.

Recovering from an Old or Botched Database

Sometime you might have a database that was overwritten with an old database, or a database that doesn't even match the version. What you want to do from here is install the port /usr/ports/sysutils/pkg_cutleaves. This port allows you to go through each port and tell it if you want to keep delete or erase the port. Run this:

ls -R /usr/ports/distfiles > portfiles.list

Open up your new distfiles text file in one term with 'vi portfiles.list', and run pkg_cutleaves in another. As the utility lists the packages, check to see if the port exists in the lists file, and put a mark next to the ones that are suppose to be installed, or erase that line entirely. If one doesn't exist, simply tell pkg_cutleaves to delete the package. When you get through them all, you will have all the packages that are installed, and in the portfiles.list folder you will have the installed files marked, and the remaining left so that you know what needs to be installed. After you install them all, run 'portupgrade -ra' to rematch the versions.

Yes, it's not an easy process, but rebuilding anything like this isn't going to be easy. I hope that this doesn't happen to anybody, but it's here if you need it.

January 2nd, 2005, 18:57
Just note, this won't work if you install with make install distclean, as it will delete things in the /usr/ports/distfiles folder.