Build your own RPMS

From Spry Wiki

Jump to: navigation, search

Sometimes the standard RPMs on your Redhat based Linux distribution are not compiled with the options you need and you'd rather have an easily managed RPM than an a bunch of unpackaged files strewn across your filesystem. The first step is to go to the download site for your distribution and find the SRPMS directory. For example, you can find SRPMs for Centos 4 at http://mirrors.usc.edu/pub/linux/distributions/centos/4/os/SRPMS/.

SRPMs are usually packaged as a single unit, unlike regular RPMS which are often split up into parts. The MySQL source for example is packaged in one SRPM which when built creates mysql-server, mysql-client, mysql-devel and so on.

By default, when you run the rpmbuild command, it looks for sources in /usr/src/redhat which means you need to build your RPMs as root. To fix this, go to your home directory and do:

$ mkdir -p rpm/BUILD
$ mkdir rpm/RPMS
$ mkdir rpm/SOURCES
$ mkdir rpm/SRPMS

Then create a file in your home directory called .rpmmacros and put into it:

%_topdir /home/username/rpm

Download your SRPM and put it wherever you want, say your home directory. Then change into rpm/SOURCES and do:

$ rpm2cpio /home/username/mutt-1.4.1-12.el4.src.rpm | cpio -id
3906 blocks

If you forget to pipe it into cpio, your screen will fill with junk.

The RPM build is controlled by a file ending in .spec. It has different sections such as %description, %prep, %build, %files, and others. Configuring the compilation goes into the %build section. For autoconf based builds, you can see what configure options can be added or removed by doing:

$ rpmbuild -bp mutt.spec

The -bp stands for "build prep", which will extract the source tarball into rpm/BUILD and apply any patches that are included with the SRPM. Change into this directory and run "./configure --help | less".

$ cd ../BUILD/mutt-1.4.1/
$ ./configure --help |less

...Output omitted...

  --disable-warnings         Turn off compiler warnings (not recommended)
  --enable-nfs-fix           Work around an NFS with broken attributes caching 
  --enable-buffy-size        Use file size attribute instead of access time 
  --enable-mailtool          Enable Sun mailtool attachments support 
  --enable-locales-fix       The result of isprint() is unreliable 
  --with-exec-shell=SHELL    Specify alternate shell (ONLY if /bin/sh is broken)

I'll add the --enable-mailtool option to the build. Change back into the SOURCES directory and edit mutt.spec. The current configure options are in the %build section like so:

%build
export -n LINGUAS
CFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=%{_prefix} \
        --with-sharedir=/etc --sysconfdir=/etc \
        --with-docdir=%{_docdir}/mutt-%{version} \
        --with-mandir=%{_mandir} \
        --with-infodir=%{_infodir} \
        --enable-pop --enable-imap \
        --with-sasl \
%{!?nossl:--with-ssl} \
%{!?nokerberos:--with-gss} \
        --disable-warnings --with-ncursesw --disable-domain \
        --disable-flock --enable-fcntl

I'm just going to add --enable-mailtool right after the --enable-fcntl. Now to create the RPM:

$ rpmbuild -ba mutt.spec

...Output scrolls by...

Processing files: mutt-debuginfo-1.4.1-12.el4
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Checking for unpackaged file(s): /usr/lib/rpm/check-files /var/tmp/mutt-1.4.1-12.el4-root
Wrote: /home/username/rpm/SRPMS/mutt-1.4.1-12.el4.src.rpm
Wrote: /home/username/rpm/RPMS/i386/mutt-1.4.1-12.el4.i386.rpm
Wrote: /home/jusername/rpm/RPMS/i386/mutt-debuginfo-1.4.1-12.el4.i386.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.70632
+ umask 022
+ cd /home/username/rpm/BUILD
+ cd mutt-1.4.1
+ rm -rf /var/tmp/mutt-1.4.1-12.el4-root
+ exit 0

As you can see, it created a new SRPM with your modifications and put it into rpm/SRPMS, and the compiled RPMs went into rpm/RPMS. Check to see:

$ ls ../RPMS/i386/
mutt-1.4.1-12.el4.i386.rpm  mutt-debuginfo-1.4.1-12.el4.i386.rpm

Now you just install them as you would any RPM.

Things to Watch For

  • If your modifications to the spec file cause new files to be added or removed from the preexisting config, you need to add or remove them from the %files section of the spec file.
  • If there is no existing SRPM for the package you want, start with a spec file that already exists and modify it rather than trying to create one from scratch.
  • The spec file contains lots of macros to help make the RPM portable. These may be %{_bindir}, %{_libdir}, %{_localstatedir}, etc. Check /usr/lib/rpm/macros to see what these represent.

Recent Changes | RSS Feed RSS