OVERVIEW
Not too long ago, I had to learn how to build RPM files. It turns out, there is excellent documentation out there on building RPMs that include compiling source code as part of the build process. If you need something simpler, for example, an RPM that does nothing but installs some static documents (or really any files, for that matter), it's not quite as well documented. In this post, I'll show you how to build an RPM that does nothing more than install files (i.e., where no ./configure;make... is required). Useful for documentation, or where an installer is provided, rather than an .rpm file.
Environment: RHEL/CentOS/Scientific 6, rpmbuild 4.8.0.
Assumptions: I assume you have a good understanding of what RPMs are, how they are used, and can navigate around Linux with some level of comfort (or at the very least, your hunger for learning outweighs your pain threshold).
CREATE THE BUILD DIRECTORY LAYOUT
No difference here; this is the same as if you're doing any other RPM file.
# mkdir ~/rpmbuild
# for DIR in BUILD BUILDROOT RPMS SOURCES SPECS SRPMS; \
do mkdir ~/rpmbuild/${DIR};done
BUILD THE SOURCES DIRECTORY
Create a directory structure in SOURCES that mimics the actual directory structure where
files will get dropped. For example, if placing documentation for package foo-1.2.3 in
/usr/share/doc, create the directories:
# mkdir -p ~/rpmbuild/SOURCES/usr/share/doc/foo-1.2.3
Drop any files (or tree) into this directory, exactly as it will appear in the actual
file system. Next create a tarball for the source in the SOURCES/ directory:
# tar -czvf foo-1.2.3.tar.gz usr/
The file name above (foo-1.2.3.tar.gz) will be used in the SPEC file below for the Source0 value. Assuming the directory structure looks like:
SOURCES/
usr/
share/
doc/
foo-1.2.3/
dir1/
file1
file2
dir2/
dir2.1/
file4
file3
BUILDING THE SPEC FILE
Once your directory structure is built, the files are in place, and you've created your source file tarball, you can start building a SPEC file. This really is the magic sauce in the project. Here is a sample that uses the example directory structure above. I've added a few comments that explain what happens along the way.
Name: <package name w/o version #s. Ex: unixODBC-doc>
Version: <full version (maj.min.patch)
Release: <release # - 0 if none; required>
Source0: <filename of source file in rpmbuild/SOURCES/>
Packager: Firstname Lastname <me@my-address.com>
Summary: <short pkg description>
License: <license type>
Group: <group under which this pkg falls - see rpm.org for groups>
BuildArch: <architecture: noarch, i386, i686, x86_64, ...>
%description
<long, multiline description>
%prep
# creating the package directory name
%setup -c -n %{name}-%{version}
%build
# Leave %build empty.
%install
# Drop SOURCES files into the BUILDROOT directory to prepare for packaging
test -d ${RPM_BUILD_ROOT} && rm -rf ${RPM_BUILD_ROOT}
/bin/mkdir -p ${RPM_BUILD_ROOT}
cp -rp ${RPM_BUILD_DIR}/%{name}-%{version}/* ${RPM_BUILD_ROOT}/
%clean
%post
# Place any commands to be run after the files are installed here. These
# are just shell commands - no #!/bin/sh is required.
%preun
%files
# Tell rpmbuild which files to install (or uninstall)
# defattr are default attributes/ownerships. "-" = same as source file.
# %attr entries override defattr.
%defattr(-,root,root)
%attr(0644, root, root) /usr/share/doc/foo-1.2.3/dir1/file1
%attr(0644, root, root) /usr/share/doc/foo-1.2.3/dir1/file2
%attr(0644, root, root) /usr/share/doc/foo-1.2.3/dir2/file3
%attr(0644, root, root) /usr/share/doc/foo-1.2.3/dir2/dir2.1/file4
# Tell rpmbuild which directories are owned by this package
%dir /usr/share/doc/foo-1.2.3/dir1
%dir /usr/share/doc/foo-1.2.3/dir2
%dir /usr/share/doc/foo-1.2.3/dir2.1
# ChangeLog. Most recent entry at the top. The format is rather strict.
# An example appears below.
%changelog
* <3-char-Day> <3-char-Month> <Dayofmonth> <year> <firstname> <lastname> <me@my-address.com>
- Description of first change/update/modification
- Description of second change/update/modification
* Thu Apr 23 2016 Richard Lohman rlohman@example.com
- Added man pages.
BUILDING THE RPM
Beyond that, the remainder of the process is identical to any other RPM build, so no need to replicate the wealth of documentation out there.
This comment has been removed by the author.
ReplyDeleteNo mention of %doc? Maybe pedantic for an RPM maned $thing-doc but still. There's a spec to be reSPECted.
ReplyDeleteHa. I crack me up.