How to run your own yum repository

773

Author: David Murphy

Yum is a powerful tool that greatly improves package handling on RPM-based Linux distributions. This tutorial explains how to create a local yum repository, configure your machine to use this repository, and customise a yum RPM to automatically use this repository.

Package management has always been a sore point for Linux, and particularly for RPM-based distros. “Dependency hell” is the term commonly used for the pain involved in installing a piece of software with a package management utility. Debian made things easier with its Advanced Packaging Tool (APT), but that’s no comfort to Red Hat and Fedora users. Connectiva Linux created APT-RPM, which brings most of the benefits of APT to RPM-based distros, but early versions bent some rules to get results.

Yum (Yellow Dog Updater, Modified) was created to address both the perceived deficiences in APT-RPM at the time, and restrictions of the Red Hat up2date package management tool. Yum handles dependencies gracefully and supports multiple repositories, as does APT-RPM. It also supports groups — tell a machine to process an application group and it will install all of those applications. This greatly simplifies managing multiple machines.

Why use yum instead of APT-RPM? Well, apart from the groups support, yum is distributed with Fedora Core, and APT-RPM is an add-on.

Creating the base repository

To create local yum repository, you need a Web server. I’ll be using the default Apache configuration you get with Fedora Core 2.

The DocumentRoot for Apache on Fedora Core is set to /var/www/html. Create a yum folder below this:


$ mkdir /var/www/html/yum

The base install RPMs for FC2 come to approximately 1.9GB in total, so ensure you have enough room. If you don’t, create the yum folder somewhere else, and create a softlink:


$ mkdir /home/yum
$ ln -s /home/yum /var/www/html/yum

You need to ensure that Apache is configured to follow links.

Now you can create a structure to store the files in. Yum supports multiple repositories, so you can serve files for both Fedora Core 1 and 2 from the same server. We’re going to store our files in the following structure:

yum
|-Fedora
  |-Core
    |-2
      |-base

Creating this is simple:


$ mkdir -p /var/www/html/yum/Fedora/Core/2/base

To get the source files for the repository, run the following commands for each of the FC2 binary CDs:


$ mount /mnt/cdrom
$ cp /mnt/cdrom/Fedora/RPMS/*.rpm /var/www/html/yum/Fedora/Core/2/base/
$ umount /mnt/cdrom

Yum works by parsing the RPM headers. To save us from downloading each RPM to read its header, yum’s developers provide a tool called yum-arch to extract the headers into their own (much smaller) files. To create your headers, run the commands:


$ cd /var/www/html/yum/Fedora/Core/2/base/
$ yum-arch .

This creates a headers folder, and turns your collection of files into a repository.

Creating the updates repository

Having your own base repository is all well and good for installing software, but you can derive far more benefit from having your own Updates repository. Creating one is as straightforward as creating the base was. Conceptually, the Updates repository fits into our structure thusly:

yum
|-Fedora
  |-Core
    |-2
      |-base
      | |-headers
      |-updates

Creating it is simple:


$ mkdir -p /var/www/html/yum/Fedora/Core/2/updates

Next, get a copy of the updates, which can be found on the Fedora Download Server or your friendly neighbourhood mirror. Simply grab them with your favourite download tool and place them in the updates folder, then run yum-arch again.

Make sure you keep this repository up-to-date, or you’ll lose the benefit of having it!

Configuring your machine

Now that you’ve got your repositories, you need to set your machine up to talk to them. This is controlled by the file /etc/yum.conf.

Here’s the default contents of the file:

[main]
cachedir=/var/cache/yum
debuglevel=2
logfile=/var/log/yum.log
pkgpolicy=newest
distroverpkg=redhat-release
tolerant=1
exactarch=1
retries=20

[base]
name=Fedora Core $releasever - $basearch - Base
baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/$releasever/$basearch/os/>

[updates-released]
name=Fedora Core $releasever - $basearch - Released Updates
baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/updates/$releasever/$basearch/

#[updates-testing]
#name=Fedora Core $releasever - $basearch - Unreleased Updates
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/updates/testing/$releasever/$basearch/

#[development]
#name=Fedora Core $releasever - Development Tree
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/development/$basearch/

…and here are the changes we’ll make:

[base]
name=Fedora Core $releasever - $basearch - Base
baseurl=http://servername/Fedora/Core/$releasever/$basearch/base/

[updates-released]
name=Fedora Core $releasever - $basearch - Released Updates
baseurl=http://servername/Fedora/Core/$releasever/$basearch/updates/

$releasever and $basearch will get expanded by yum to point to the current folder structure.

Now it’s a simple matter of running yum update to see what needs updating.

Lather, rinse, repeat

Suppose you have 100 machines you want to keep updated. Editing the yum.conf file for each machine is possible, but I’m sure you’ve got better things to do. There’s got to be a better way, right?

There is. You can alter the stock Fedora yum RPM to automatically point your machines at the repository.

Get the source RPM (SRPM) from either your CD or the Fedora Download Server (or your friendly neighbourhood mirror, of course). The package you want is yum-2.0.7-1.1.src.rpm. As root, install this package with the command rpm -i yum-2.0.7-1.1.src.rpm.

RPM sources are kept in the /usr/src/redhat folder, with the source files in SOURCES and the spec files in SPECS. You need to edit both the default yum.conf file and the yum.spec file.

First, edit /usr/src/redhat/SOURCES/yum.conf.fedora as in the “Configuring your machine” section above. Next, edit /usr/src/redhat/SPECS/yum.spec, changing the line:

Release: 1.1

to:

Release: 1.2

This change gives our customised RPM precedence over the stock one. Now you can build the RPM with the command rpmbuild -ba /usr/src/redhat/SPECS/yum.spec.

The resultant RPM is stored in /usr/src/redhat/RPMS/noarch, and can be installed with the command rpm -U /usr/src/redhat/RPMS/noarch/yum-2.0.7-1.2.noarch.rpm.

Note: if you’ve already edited your /etc/yum.conf file, you can create the customised one as /etc/yum.conf.rpmnew.

There you have it — a local yum repository complete with a customised yum package to take the strain out of maintaining your machines.