rpm – building hello.rpm

Reading Time: 5 minutes

Last Updated: 10/15/2024

IF YOU CAN SEE THIS TEXT THEN YOU ARE LOOKING AT A POST THAT IS STILL UNDER CONSTRUCTION. AS THIS ONE WILL TAKE SOME TIME IT IS BUILT IN SECTIONS. (this is day 5)

The purpose of this post to learn to produce a simple rpm package. hello is a s standard exemplar program and is often the first program students will work through creating. The goal of this being that by studying this exemplar it will be easier to understand how to build our own rpm packages.

This is deceptively simple looking project. Having said that it does have a number of details which should be managed with care. To better manage the process and ensure our success we are going to break this into a number of steps.

Please note that there are a number of posts which are extremely advantageous to review in advance. Put simply, to accomplish our goal it is extremely beneficial to understand a number of elements in advance. Understanding and being fluent in these will allow us to break down our goal into simple steps which can be executed with less misery mystery.

some of the initial setup and configuration of the environment.

rpmlink. // There a number of features that you should know before entering this. Chief among these are how to install, remove, list dependencies, and look at an rpm and its structure.

rpm – deconstructing rpm – hellolink // this link is instrumental to review what is published by SUSE Distribution itself. This post should review what we find in the official distribution; and walk through what is in it and what you should expect by installing this. This is instrumental if we hope to understand and replicate this.

rpmbuild – a quick reviewlink // this link should be reviewed as it is used to review installing the basic rpmbuild tools; reviews the setup of the build environment and discusses

autoconflink // While not used here is a topic worth review.

REQUIREMENTS

There are going to be a number of packages that you are going to want to have installed. First and foremost, gcc, autoconf; automake So please make sure you have these installed.

TL;DR (too long; didn’t read)

I am not saying you are like me; but I think I have an obligation to point out a few things up front that you should focus on when skimming this. Please note the above requirements; Please note there are a few surprising pieces of content that are expected to be in place and are required in order even to get this most minimum of programs in place. We will try to detail some of these. Also please note the rpmbuild process is flexible; there are multiple ways to accomplish the same task. ( i.e. the makefile install doing some tasks or relying on the .spec file to install items )

By minimum I mean that our goal here appears simple at first. At first glance we are creating a RPM package that installs a single “hello” executable ( /usr/bin/hello ). Along with that we learn that there is some additional baggage that comes along. This will traditionally include Documents; README, Copying; ChangeLog If you have already skimmed down below you (might/will) note the the version released by the distro has a number of additional files which fits their style ( ABOUT-NLS; THANKS; TODO )

BEGIN THE BEGIN

By this point you should have reviewed the “deconstructing hello.rpm” (link found above)

INITIAL BUILD ENVIRONMENT

By this point you should have installed RPMDEVTOOLS. You should have already setup your rpmbuild environment. Please see the Link above.

In short, at this time you would have already setup the rpmbuild directory which includes constructed the .rpmmacros file. If your unsure about this…. bad squirrel. (please go back and read)


In this example we are constructing the exemplar RPM by using the following steps.

  • Setup the rpmbuild structure.
  • Acquire the TAR source.
  • Acquire the SPEC file.
  • Use rpmbuild to build the project and assemble the rpm.

Put that way – this seems relatively simple – doesn’t it?

ACQUIRE THE TAR SOURCE.

The way the RPMBUILD process works is that we tuck the source TAR away in the SOURCES directory. .

cd ~/rpmbuild/SOURCES
wget wget https://gotitadmin.com/downloads/hello-rpm/hello-1.tar

I will note that he build process extracts the tar and then cleans/tidies up afterwards.

ACQUIRE THE SPEC FILE.

The spec file is the information and build guide for the RPM. The file will details the build process. For example it will detail the source tar name and can detail a number of incidentals used to detail the construction of the RPM. This will be a simple example.

cd ~/rpmbuild/SPECS
wget https://gotitadmin.com/downloads/hello-rpm/hello.spec

BUILDING THE RPM

We can build the RPM from the same SPECs directory.

cd ~/rpmbuild/SPECS
rpmbuild -bb hello.spec
[edit]
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.6mp5MU
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd hello-1
+ /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/hello-1-0.x86_64
+ exit 0

At the end, located in the RPMS directory, in the project hello-1-0.x86_64 directory we should have a file named “hello-1-0.x86_64.rpm”.

localhost:~/rpmbuild/RPMS/x86_64 # ls ~/rpmbuild/RPMS/x86_64/hello-1-0.x86_64.rpm
/root/rpmbuild/RPMS/x86_64/hello-1-0.x86_64.rpm

Mission accomplished!


Now that we have built the RPM let’s take a few minutes, step back and review items we have breezed by but are worthy of discussion.

THE LONGER DISCUSSION

There are numerous examples for the source code for hello world. This is not unique code. This post focuses on taking the exemplar hello world example and building an exemplar hello rpm.

rpmbuild works by pulling the source from the source tar and working a little magic. That magic will include calling the “configure” script; calling make and then “make install”. These really are central theme of rpmbuild. If you get nothing else out of this; this is your takeaway.

NOTE: please note that the configure script in this case was blank.
NOTE: autoconf is not called. I was expecting that this would be necessary.- but apparently not?

A NOTE ON MAKE INSTALL

When rpmbuild is called it’s going to call “make install DESTDIR=…”. This DESTDIR is handing off the job of copying the binary file over to the ~/rpmbuild/BUILDROOT directory. This directory is the equivalent of a fakeroot; or base directory used for computing what files/directories will be copied to/from the RPM.

Please note that the make install places the binary in our “root” as /usr/bin/hello.
Please note that the spec file includes “usr/bin/hello” and this is the magic that places it in the RPM. In short – if you do not include the file in your SPEC file then it won’t end up in the resulting RPM. The system may also choose to warn you if you have unexpected files in the fake root but haven’t included them in the spec.

Discussing autoconf is it’s own post. While it wasn’t directly used in this case, per se, you may note on inspecting the tar source I had a configure.ac file. That then is a whole different can of worms, as well as the use of automake. I will admit to not being as intimately familiar with these as I should be.

There will are a number of youtube and a great deal of additional posts. This was a simple case. I am actually working up to the more complicated case which includes adding service daemons. To do that we will work on implementing pre and post installation scripts. These are included in the SPEC file. The exemplar review of the production hello RPM

CLEAN BUILD:

This particular addition might be more of a reminder to me. But when testing and automating this I wrote a very basic script to automate the construction process:

wget wget wget https://gotitadmin.com/downloads/hello-rpm/clean-build.sh

As you can see it deletes the rpmbuild environment and macro; calls for a new setup; pulls down the source tar and spec file and then builds the thing. Automation is a wonderful thing. The process of writing this was more of a rinse-and-repeat type of thing so it really was a time saver.

Also please remember to ensure NTP is set on your servers. It’s always rough to learn that fi your source is set a minute in the future.

cd ~
rm -rf rpmbuild
rm .rpmmacros
rpmdev-setuptree
cd ~/rpmbuild/SOURCES
wget wget https://gotitadmin.com/downloads/hello-rpm/hello-1.tar
cd ~/rpmbuild/SPECS
wget https://gotitadmin.com/downloads/hello-rpm/hello.spec
rpmbuild -bb hello.spec

References:
https://blog.packagecloud.io/inspect-extract-contents-rpm-packages/
https://docs.pagure.org/packaging-guidelines/Packaging%3ARPMMacros.html

This entry was posted in RPM. Bookmark the permalink.