Create rpm files

Here we’ll go through creating rpm files, e.g. for easily deployable code of your own, or other items.
Handy with your own yum repository, which we’ve covered here.

We’ll create rpm files using tarballs.

Start with creating a directory for your tarballs, then copy your files into a subdir with the same name the rpm is going to be.

mkdir -p /home/user/tarball/
cp the_files/* /home/user/tarball/my_code_library-1.0

go to /home/user/tarball/my_code_library-1.0
and create a spec file, which is a file containing all necessary info the rpmbuild script needs.

Name it equally my_code_library-1.0.spec, the edit it.
Below is an example of a spec file, usable with a code library.

Name:           my_code_library
Summary:      Version 1 of my code library
Version:        1.0
Release:        1
Epoch:          0
Group:          Library #type of files, e.g. Library, Applications, etc.
License:        BSD //the licence you want to publish the RPM under
Source:         %{name}-%{version}.tar.gz  //this is the tarball we'll create soon
Packager:       Your Name   //your name and your mail address you want to publish with
Vendor:         The Vendor  //e.g. your website, company site, etc.
BuildRoot:      /var/tmpbuild/ //the location where it will be built
BuildArch:      noarch  //architecture to build for, noarch, i386, x86_64 etc.
Requires:       httpd  //if your code requires any other rpm packages, name them here

%description
#a description of your package

%prep
#preparations, if needed

%setup
#setup on the build box

%build

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}/path/where/it/should/go/to
cp -Rp * %{buildroot}/path/where/it/should/go/to
rm -f %{buildroot}/path/where/it/should/go/to/%{name}-%{version}.spec

%post
#post installation instructions

%clean
#clean the buildroot after install
rm -rf %{buildroot}

%files
#file attributions and more
%attr (0644, root, root) /path/where/it/should/go/to/*
%attr (0755, root, root) %dir /path/where/it/should/go/to

%changelog
#changelog of your code in descending order.
*Mon Jul 12 2010 Name  
- Added required dependency
*Sun Jul 11 2010 Name 
- Initial spec

Install, if it’s not on the machine yet, rpm-build, we’ll need it soon.

sudo yum install rpm-build

Now, create a tarball of your library:

tar -cvzf my_code_library-1.0.tar.gz my_code_library-1.0 

This creates a gzipped tarball of your directory with the same name.

Create the rpm file now

rpmbuild -tb my_code_library-1.0.tar.gz

-b tells rpmbuild to use a spec file
-t means to look inside a tarball

Now you should see the rpm file being created, and it’ll end with
exit 0;
if no problems are encountered.
If there are any, check on the error printed to stdout and correct them, (e.g. a typo etc.)

The created rpm file can be found in
/usr/src/redhat/RPMS/

Use your new rpm file in your own Yum Repository.

If you want to create gpg signed rpm files, you’ll need to add a few steps before running rpmbuild.
If you don’t have a gpg key yet or want to use one specifically for the rpm building, create a new one

run

gpg --gen-key

Chose your key kind here

Please select what kind of key you want:
   (1) DSA and Elgamal (default)
   (2) DSA (sign only)
   (5) RSA (sign only)
Your selection? 

We’re using DSA and Elgamal
in the step, chose the size

DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)

We went for the default of 2048
In this step

Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 

we went for option 0

The next step enquires if all is correct, if so, continue with y else hit N and rerun above steps.

Now enter your name, mail addres and a comment (optional)

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"

Real name: RPM signing key
Email address: rpm@example.com
Comment:

Next step is to confirm the data and your options

You selected this USER-ID:
    "RPM Signing key <rpm@example.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?

Confirm, then enter your password.
You’ll need to enter the password every time you run the rpmbuild command, even if it’s empty.

Export your key

export --armor "RPM Signing key <rpm@example.com>" > RPM-GPG-KEY-myrepo

This key is used on the machines where you want to install the rpm files. Copy this key over to the
machine and there run

rpm --import RPM-GPG-KEY-myrepo

Following this, create in your home directory (or the user’s dir who’ll create rmps) .rpmmacros, then insert

%_signature gpg
%_gpg_name RPM Signing key %lt;rpm@example.com%gt;

Now, create rpm files simillar as above

rpmbuild --sign -tb my_code_library-1.0.tar.gz

This is just a rudimentary tutorial creating an rpm.
There are many options in the .spec file alone which can be inserted, for full info look at
TLDP RPM howto