RCHE Red Hat Certified Engineer Linux Study Guide[c] Exam (Rh302)

 < Day Day Up > 


The RPMs that I've described so far are already in binary format. In other words, they're compiled and ready to install just like 'tarballs' or Microsoft-style installation packages. But Linux goes back to the source code. Courtesy of the GPL, the source code is readily available.

In fact, if you're studying for the Red Hat exams using Red Hat Linux 9, the source code may be all you have available for the RHEL packages that you may need for this book. The source code includes the programs and configuration files that you need to set up and install the binary RPMs described earlier.

On The Job 

You can also build RPM sources from a tar archive. However, as of this writing, there are no requirements in the RHCE and RHCT related course outlines related to building source RPMs from a tar archive. And the obsolete version of the RHCE exam prep guide suggests that you only need to be 'familiar with the basic elements of' source RPMs.

Creating and Using Custom RPMs

A source RPM is, as the name indicates, a package of source code used to build architecture-specific packages. Properly labeled source RPMs include the 'src' identifier as part of the filename, such as:

polarbear-2.07-2.src.rpm

Binary RPMs are built from source RPMs. The source RPM contains the source code and specifications necessary to create the binary RPM.

Before you continue with this section, install the rpm-build RPM package. For RHEL 3, if you're installing from a network source on the /mnt/inst directory, you'd run the following command:

# rpm -Uvh /mnt/inst/RedHat/RPMS/rpm-build-4.2.1-4.2.i386.rpm

If you're running Red Hat Linux 9, install the rpm-build package associated with that distribution. You can then use the rpmbuild command as described in the following sections.

On The Job 

In RHEL 3, the rpmbuild command has superseded the older rpm commands associated with building RPM packages from source code.

Installing Source RPMs

Like normal RPMs, a source RPM (SRPM) is installed with the -i option. It does not install the package itself. This installs the contents of the SRPM within the /usr/src/redhat directory structure, which you can turn into a binary RPM. For the purpose of this section, I've installed the source for the Package Management utility, redhat-config-packages, from the freely available RHEL 3 source RPMs. I describe their availability later in this chapter.

The /usr/src/redhat/ Directory Structure

There are five subdirectories under the /usr/src/redhat directory, which I describe in Table 4-12.

Table 4-12: Build Directories from RPM Sources

Directory

Purpose

/usr/src/redhat/SOURCES

Contains the original program source code

/usr/src/redhat/SPECS

Contains spec files, which control the RPM build process

/usr/src/redhat/BUILD

Source code is unpacked and built here

/usr/src/redhat/RPMS

Contains the output binary RPM

/usr/src/redhat/SRPMS

Contains the SRPM created by the build process

When you build an SRPM, you will build it within this structure. If you install an SRPM, it is extracted into this structure. The redhat-config-packages source RPM that you installed in the previous section should now have a file in the /usr/src/redhat/SPECS directory, in the redhat-config-packages.spec file.

Changing Compile Options for a Source RPM

While most precompiled RPMs will serve your needs, at times you will want to modify the source code or compile options in the corresponding SRPMs. You can do so in the spec file that you get when you installed the source RPM.

The Spec File

To change the compile options in an SRPM, you must understand spec files. The spec file is stored in /usr/src/redhat/SPECS/packagename.spec. The spec file controls the way a package is built, and what actions are performed when it is installed or removed from a system. A spec file has ten different sections, as described in Table 4-13. Several of the sections include commands that can be run as individual shell scripts. You won't see all of these sections in every spec file.

Table 4-13: Build Directories for Source RPM Files

Section

Description

Preamble

Includes information shown with an rpm -qi command. This normally includes a summary, version, and group. It also includes a list of dependent packages.

Description

A basic package description.

Prep

Includes any preparatory commands required before building the source code, such as unpacking.

Build

Commands to actually compile the spec file and build sources.

Install

Commands to install the software on a system.

Install and uninstall scripts

This section contains scripts that will be run on the end user's system to install or remove the software. RPM can execute a script before the package is installed, after the package is installed, before the package is removed, and after the package is removed.

Verify

Although RPM takes care of most verification tasks, a script can be inserted here for any desired extra checks.

Clean

A script can be specified here to perform any necessary cleanup tasks.

Files

A list of files in the package.

Changelog

A list of revisions.

You can change the compile-time options for a package in the build section of the spec file. Here's a sample build section from a different spec file (this is not from the redhat-config-packages source RPM):

%build rm -rf $RPM_BUILD_ROOT mkdir -p $RPM_BUILD_ROOT/usr/bin $RPM_BUILD_ROOT/etc ./configure --prefix=/usr --exec-prefix=/ make CFLAGS="$RPM_OPT_FLAGS" LDFLAGS=-s

This section, a shell script, begins with some housekeeping, removing any files that may be left over from a previous build. A directory structure is created for the source files. Then the package is configured and compiled with a 'make.'

For a different package, you might modify the make command line to compile other components after LDFLAGS. The compile options from $RPM_OPT_FLAGS are defaults, set by RPM. Alternatively, you could use this variable to set other compile time options such as a different CPU.

On The Job 

Perhaps the essential reference guide to the RPM system is a book called Maximum RPM. It's available online from Red Hat at www.redhat.com/docs/books/max-rpm.

Building Custom Source and Binary RPMs

By now, you should understand where you should modify an SRPM spec file to change compile time options in the Build section. However, there's much more to building customized RPMs. Once you have modified the spec file, you need to tell RPM to build a new RPM and SRPM.

Starting a Build

You can build an RPM using the rpmbuild command, with the build switch, -b. By itself, rpmbuild -b calls the scripts specified in the Prep, Build, and Install parts of the spec file. Normally, you'll modify the -b with a, which makes RPM go through the build process, step by step. The RPM build operation is directed at a spec file. For example, the command

# rpmbuild -ba redhat-config-packages.spec

directs RPM to create binary and source RPMs from this spec file. Alternatively, if you just want the binary RPM, the following command will do the job:

# rpmbuild -bb redhat-config-packages.spec

Building Red Hat Enterprise RPMs

The SRPMs for RHEL 3 are freely available online from the Red Hat FTP site at ftp.redhat.com or mirror sites specified at www.redhat.com/download/mirror.html. As of this writing, the RHEL 3 SRPMs are located on the /pub/redhat/linux/enterprise/3/en/os/i386/SRPMS directory.

If you're studying for the Red Hat exams using Red Hat Linux 9, you don't have to buy RHEL 3. You can build Red Hat Enterprise RPMs from the source code. For example, we can learn more about the Red Hat Package Management utility from the redhat-config-packages source code. To do so, take the following steps:

Navigate to ftp.redhat.com (or a mirror site) and navigate to the directory with RHEL 3 SRPMs. Then you can:

  1. Download the RHEL 3 version of the package that contains the Package Management utility, redhat-config-packages-1.2.5-1.src.rpm.

  2. Install the source RPM package. The rpmbuild commands described in the previous section do not install the package on your computer; it does set it up in your /usr/src/redhat tree.


 < Day Day Up > 

Категории