Custom Packages


WIth many production servers to look after, it is normal to have everything setup the same on some of them. Often much of the installed software is available in the extensive FreeBSD ports collection. However, my own code and other files, which I may want to duplicate on each server, is a task which I have to address myself.

Usually a FreeBSD package is built from an installed port, but it is possible to package up your own files/programs as well, using the pkg_create utility. For a single directory, for example if I simply wanted to duplicate one websites htdocs directory to a bunch of servers, this is overkill.

In addition to putting a bunch of files, or scripts, into the appropriate directories, we can also specify a pre-install script, post-install script, deinstall script, post-deinstall script, other packages as dependencies, etc. Type man pkg_create for more information.

Let's do a quick example. Suppose I require one file to be placed in /usr/local/bin/ and one file to be placed in /usr/local/lib/. On my laptop here, I'll create all the packages in /home/packages/. In there make a directory test which will contain all the files we need for our test package. All the information goes into a 'packlist' file, so edit /home/packages/test/packlist and put:


@name test-package-0.1
@cwd /usr/local
@srcdir /home/packages/test
@comment list of files
bin/test1
lib/test2

In /home/packages/test/ also create directories 'bin' and 'lib' and create our required files in them as appropriate: bin/test1 and lib/test2.

The file packlist contains the name of the package, the destination directory - in this case I've simply put /usr/local as the source files are in /bin/ and /lib/ and this will be appended to the path when installing the files. I've put the full path to where the files are, my current working directory /home/packages/test, and after that I've listed the locations of each file I've created (test1 and test2), one file per line.

To create a package .tgz file all I need to do now is:

$   cd /home/packages/test/
$   pkg_create -c -'test 1' -d -'test package 0.1' -f packlist test-package-0.1.tgz

where I've added the comment and description on the command line. To check it's there:

$   ls *.tgz
test-package-0.1.tgz

Now I can copy this over to any server and type:

$   pkg_add test-package-0.1.tgz

and it's installed. To remove the package (which will remove both of the files from the server of course) just type:

$   pkg_delete test-package-0.1

and it's done. Note that as this package is not related to any installed FreeBSD port, then when installing you may see a message like:

pkg_add: package test-package-0.1 has no origin recorded

but this can be ignored.