wiki:BinaryRPM

How to build a binary RPM

By binary RPM I mean an rpm for a package for which you don't have the source. The easiest way to show this is to make an example. I'm going to make an rpm called fatlab to distribute fatworks software.

  • The first step is to install the software on a machine and figure out where all the files are supposed to exist. There are a few ways to do this, something that might take a long time but is guaranteed to work is the following:
[root@build #]$ touch /tmp/now
[root@build #]$ install fatlab
[root@build #]$ find /etc /usr /bin /lib -newer /tmp/now >/tmp/files
[root@build #]$ tar cjf fatlab-%{version}.tar.bz2 `cat /tmp/files`

If you know where the files are, then just make the tar file yourself...substitute %{version} with the version number of fatlab.

  • Now start a new empty spec file for fatlab
Name:      fatlab
Version:   1.0
Release:   1{?dist}
Summary:   fatlab fatematical package INTERNAL USE ONLY
Group:     Applications/Scientific
License:    Commercial
URL:       www.fatworks.com/fatlab
Source0:   http://www.fatworks.com/fatlab/fatlab-1.0.tar.bz2
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
AutoReq:   no

%description
--- Internal Use only ---
fatworks fatlab
-------------------------

The post important part of this spec file so far is AutoReq?: no. This tells rpm to not try and figure out the requirements of the package. They will invariably be wrong if you let rpm do it itself.

prep and install

  • Next add the %prep and %install stages to the rpm, you will skip the %build section, there shouldn't be a build section if this is really a binary rpm.
%prep
%setup -q -n %{version}

%install
# copy files to installation dir
mkdir -p %{buildroot}/usr/fatlab/%{version}
for dir in etc bin doc sys lib
do
     cp -a $dir %{buildroot}/usr/fatlab/%{version}
done

# symlink the binaries to /usr/bin
mkdir -p %{buildroot}/%{_bindir}
ln -s /usr/fatlab/%{version}/bin/fatlab %{buildroot}/%{_bindir}
ln -s /usr/fatlab/%{version}/bin/fatlab-x11 %{buildroot}/%{_bindir}

# make a menu button 
mkdir -p %{buildroot}/%{_datadir}/applications
cat <<EOF>%{buildroot}/%{_datadir}/applications/%{name}-%{version}.desktop
[Desktop Entry]
Version=%{version}
Name=fatlab-%{version}
Comment=A great fatematical package
Exec=fatlab %U
Icon=/usr/fatlab/%{version}/fatlab_icon.xpm
Terminal=false
Type=Application
Categories=Application;Fatematics
MimeType=application/x-fatlab
EOF

post and postun

  • Optionally you can do things in the postinstall script. In the case of fatlab, you'll need to change the selinux context of a lot of libraries. To ensure that the libraries always get labelled with the proper context, change the default with semanage as well. We include a postuninstall script to remove the contexts we setup in post
%post
for dir in bin lib
do
     semanage fcontext -f -- -a -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so 2 >/dev/null || :
     semanage fcontext -f -- -a -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so\.[0-9] 2 >/dev/null || :
     chcon -t textrel_shlib_t /usr/fatlab/%{version}/$dir/*.so 2>/dev/null || :
     chcon -t textrel_shlib_t /usr/fatlab/%{version}/$dir/*.so.[0-9] 2>/dev/null || :
done

%postun
for dir in bin lib
do
     semanage fcontext -f -- -d -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so 2 >/dev/null || :
     semanage fcontext -f -- -d -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so\.[0-9] 2 >/dev/null || :
done

files

  • Now for the files section, nothing special here, just remember to set %config on anything you want to be able to change after install
%files
%defattr(-,root,root,-)
%doc license.txt trademarks.txt
%{_bindir}/%{name}
%{_bindir}/%{name}-x11
%{_datadir}/applications/%{name}-%{version}.desktop
/usr/fatlab/%{version}/bin
/usr/fatlab/%{version}/doc
%config /usr/fatlab/%{version}/etc
/usr/fatlab/%{version}/lib
/usr/fatlab/%{version}/sys

changelog

  • Finally and probably most importantly, add a changelog so you can trac your changes to the rpm.
%changelog
* Wed Mar 9 2001 Thomas Uphill <myemailaddress@example.com> - 1.0-1
- initial build of fatlab.
  • Now build your rpm. You should have to add some Requires: lines to the header lines, but that is an iterative process, you figure out what requirements are missing on your target system and start adding the requirements to the spec file. Building is just with the standard rpmbuild command found in the rpm-build rpm.

Complete Spec file

So the final spec file should look like this:

Name:      fatlab
Version:   1.0
Release:   1{?dist}
Summary:   fatlab fatematical package INTERNAL USE ONLY
Group:     Applications/Scientific
License:    Commercial
URL:       www.fatworks.com/fatlab
Source0:   http://www.fatworks.com/fatlab/fatlab-1.0.tar.bz2
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
AutoReq:   no

%description
--- Internal Use only ---
fatworks fatlab
-------------------------

%prep
%setup -q -n %{version}

%install
# copy files to installation dir
mkdir -p %{buildroot}/usr/fatlab/%{version}
for dir in etc bin doc sys lib
do
     cp -a $dir %{buildroot}/usr/fatlab/%{version}
done

# symlink the binaries to /usr/bin
mkdir -p %{buildroot}/%{_bindir}
ln -s /usr/fatlab/%{version}/bin/fatlab %{buildroot}/%{_bindir}
ln -s /usr/fatlab/%{version}/bin/fatlab-x11 %{buildroot}/%{_bindir}

# make a menu button 
mkdir -p %{buildroot}/%{_datadir}/applications
cat <<EOF>%{buildroot}/%{_datadir}/applications/%{name}-%{version}.desktop
[Desktop Entry]
Version=%{version}
Name=fatlab-%{version}
Comment=A great fatematical package
Exec=fatlab %U
Icon=/usr/fatlab/%{version}/fatlab_icon.xpm
Terminal=false
Type=Application
Categories=Application;Fatematics
MimeType=application/x-fatlab
EOF
%post
for dir in bin lib
do
     semanage fcontext -f -- -a -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so 2 >/dev/null || :
     semanage fcontext -f -- -a -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so\.[0-9] 2 >/dev/null || :
     chcon -t textrel_shlib_t /usr/fatlab/%{version}/$dir/*.so 2>/dev/null || :
     chcon -t textrel_shlib_t /usr/fatlab/%{version}/$dir/*.so.[0-9] 2>/dev/null || :
done

%postun
for dir in bin lib
do
     semanage fcontext -f -- -d -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so 2 >/dev/null || :
     semanage fcontext -f -- -d -t textrel_shlib_t /usr/fatlab/%{version}/$dir/.\*\.so\.[0-9] 2 >/dev/null || :
done
%files
%defattr(-,root,root,-)
%doc license.txt trademarks.txt
%{_bindir}/%{name}
%{_bindir}/%{name}-x11
%{_datadir}/applications/%{name}-%{version}.desktop
/usr/fatlab/%{version}/bin
/usr/fatlab/%{version}/doc
%config /usr/fatlab/%{version}/etc
/usr/fatlab/%{version}/lib
/usr/fatlab/%{version}/sys
%changelog
* Wed Mar 9 2001 Thomas Uphill <myemailaddress@example.com> - 1.0-1
- initial build of fatlab.
Last modified 7 years ago Last modified on Mar 9, 2011 4:48:55 PM