search by tags

for the user

adventures into the land of the command line

making your own RPMs

this one is a little bit complicated, but then after you do it, you’ll think it’s not that complicated

redhat comes with a utility called rpmbuild, which you can use to build an RPM. but you need to set up a bunch of things first for it all to work

the first thing you need to do is create an environment and specify the build area so that rpmbuild knows where to do its thing
its best to do this as a non-root user

$ mkdir -p ~/rpmbuild/{RPMS,SRPMS,BUILD,SOURCES,SPECS}
$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

the directory structure should look like

├── .rpmmacros
├── rpmbuild
    ├── BUILD
    ├── SPECS
    ├── SOURCES
    ├── RPMS
    └── SRPMS

SOURCES is where you put your source files
SPECS is where you put your spec file. these are your instructions for how you want rpmbuild to build the rpm
BUILD is the temporary space where rpmbuild combines your source files into an RPM
RPMS is where rpmbuild spits out the final RPM
SRPMS are source RPMs that you can use instead of SOURCES (i think)

the .rpmmacros file contains global macros, which are essentially variables you can pass to your rpmbuild. there are built-in macros like

%_prefix /usr
%_sysconfdir /etc
%_localstatedir /var
%_infodir /usr/share/info
%_mandir /usr/share/man
%_initrddir %{_sysconfdir}/rc.d/init.d
%_defaultdocdir %{_usr}/share/doc

and you can define your own like

#%define macro_name value
%define major 2

# Accessing macro

the second thing to do is to put your source file(s) into SOURCES

the third thing to do is to create a spec file

$ vim ~/rpmbuild/SPECS/package.spec

%define        __spec_install_post %{nil}
%define          debug_package %{nil}
%define        __os_install_post %{_dbpath}/brp-compress

Summary: A very simple rpm package
Name: package
Version: 1.0
Release: 1
License: MIT
Group: Development/Tools
SOURCE0 : %{name}-%{version}.zip
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root


unzip %{SOURCE0}

# empty section.

# should reflect how directories will actually look, but must be done within the buildroot
rm -rf %{buildroot}
mkdir -p  %{buildroot}
cp -a * %{buildroot}

rm -rf %{buildroot}

# set permissions on the files/directories created
# %defattr(<file mode>, <user>, <group>, <dir mode>)
# %attr(<mode>, <user>, <group>) file
# next indicates that the file in the package should be installed with extension .rpmnew if there is already a modified file with the same name on the installed machine
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
# this is the standard place to put binaries, relative to the build root

* Aug 27 2015 First Build


as you can see, getting the spec file right is the most important step as it is basically the template for where you want files to be put when you install the rpm and what permissions and behaviour should follow

once you are ready, the last step is to actually run rpmbuild

$ rpmbuild -ba ~/rpmbuild/SPECS/package.spec

# you can provide arguments too
$ rpmbuild -ba --define "macro_name value" ~/rpmbuild/SPECS/package.spec

if all goes well, you’ll have a shiny new rpm in the RPMS directory that you can take and install with

$ rpmbuild -Uvh mygroovyrpm.rpm