In this post I will build a very simple RPM, this RPM will contain a very useful program/shell script.
With this information you can build complex RPMs later on.
Set up your build environment
In this case I am using a RHEL 6.5 64bit system
1 2 |
[root@rpmbuild ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.5 (Santiago) |
Install the tools:
rpm-build: is what you need to build RPMs
rpmdevtools: is not required but it is very helpful because it helps you create the directory tree and base SPEC file
1 |
[root@rpmbuild ~]# yum install rpm-build rpmdevtools |
Create a non-privileged user to build the RPMs
1 2 3 |
[root@rpmbuild ~]# useradd rpmbuilder [root@rpmbuild ~]# su - rpmbuilder [rpmbuilder@rpmbuild ~]$ |
Create the directory tree using rpmdev-setuptree
1 2 3 4 5 6 7 8 |
[rpmbuilder@rpmbuild ~]$ rpmdev-setuptree [rpmbuilder@rpmbuild ~]$ tree rpmbuild/ rpmbuild/ ├── BUILD ├── RPMS ├── SOURCES ├── SPECS └── SRPMS |
Package Application
Work on packaging your application/program (e.g. very_useful_script.sh)
The folder and the archive naming is important for later when they get unarchived, the rpm tools will by default use name-version (e.g. name=very-useful-script, version=1.0, that is why the folder/archive was named very-useful-script-1.0/ ).
That is the default and can be easily changed in the SPEC file.
1 2 3 4 5 6 7 |
[rpmbuilde@rpmbuild ~]$ ls rpmbuild very_useful_script.sh [rpmbuilde@rpmbuild ~]$ mkdir <b>very-useful-script-1.0</b> [rpmbuilde@rpmbuild ~]$ mv very_useful_script.sh very-useful-script-1.0/ [rpmbuilde@rpmbuild ~]$ tar cvzf very-useful-script-1.0.tgz very-useful-script-1.0/ [rpmbuilde@rpmbuild ~]$ ls rpmbuild very-useful-script-1.0 very_useful_script.sh very-useful-script-1.0.tgz |
Move your packaged application to the SOURCES directory under rpmbuild/
1 |
[rpmbuilde@rpmbuild ~]$ mv very-useful-script-1.0.tgz ~/rpmbuild/SOURCES/ |
Now it is time to create the SPEC
Create a skeleton spec file
1 2 |
[rpmbuilde@rpmbuild ~]$ rpmdev-newspec Skeleton specfile (minimal) has been created to "newpackage.spec". |
Move it to your directory tree
1 |
[rpmbuilde@rpmbuild ~]$ mv newpackage.spec ~/rpmbuild/SPECS/very-useful-script.spec |
This is how your directory tree should look like
1 2 3 4 5 6 7 8 9 10 11 |
[rpmbuilde@rpmbuild ~]$ tree rpmbuild/ rpmbuild/ ├── BUILD ├── RPMS ├── SOURCES │ └── very-useful-script.tgz ├── SPECS │ └── very-useful-script.spec └── SRPMS 5 directories, 2 files |
SPEC file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Name: very-useful-script Version: 1.0 Release: 1%{?dist} Summary: This is a very useful script Group: Applications/System License: MIT URL: http://example.com Source: very-useful-script-1.0.tgz %description This is a very useful script %prep %setup -q %install rm -rf $RPM_BUILD_ROOT install -d $RPM_BUILD_ROOT/usr/local/bin/ install -m 755 very_useful_script.sh $RPM_BUILD_ROOT/usr/local/bin/very_useful_script.sh %clean rm -rf $RPM_BUILD_ROOT %files %dir /usr/local/bin %defattr(-,root,root,-) %doc /usr/local/bin/very_useful_script.sh %changelog |
Dissecting the SPEC file
The below is header information and just descriptive data
1 2 3 4 5 6 7 8 9 10 11 12 |
Name: very-useful-script Version: 1.0 Release: 1%{?dist} Summary: This is a very useful script Group: Applications/System License: MIT URL: http://example.com Source: very-useful-script-1.0.tgz %description This is a very useful script |
The below is where we prepare our sources to be packaged into RPM
%prep is a section where we can execute commands or use macros.
%setup is a macro that unarchives the original sources.
Earlier I was discussing the importance of naming the folder and archive as name-version, this is because the %setup macro expects that by default, but you can overwrite the default by specifying the folder name (e.g. %setup -q -n very-useful-script-1.0-john-x86)
1 2 3 |
%prep %setup -q |
OR
1 2 3 |
%prep %setup -q -n very-useful-script-1.0-john-x86 |
The below removes previous remains of the files in the buildroot
Then creates a folder /usr/local/bin/ in the buildroot
Then puts our very_useful_script.sh in /usr/loca/bin with mode 755
1 2 3 4 5 |
%install rm -rf $RPM_BUILD_ROOT install -d $RPM_BUILD_ROOT/usr/local/bin/ install -m 755 very_useful_script.sh $RPM_BUILD_ROOT/usr/local/bin/very_useful_script.sh |
The below just cleans the buildroot
1 2 3 |
%clean rm -rf $RPM_BUILD_ROOT |
The below specifies all the files that will be installed by the RPM
You need to list them all, or use wildcards
1 2 3 4 5 6 7 8 9 |
%files %dir /usr/local/bin %defattr(-,root,root,-) %doc /usr/local/bin/very_useful_script.sh |
Build the RPM using the SPEC file
1 |
[rpmbuilde@rpmbuild rpmbuild]$ rpmbuild -ba ~/rpmbuild/SPECS/very-useful-script.spec |
After the RPM has been successfully been built, you can find it under:
1 |
[root@rpmbuild ~]# ls /home/rpmbuilde/rpmbuild/RPMS/x86_64/ |
Install it (need to be root)
1 |
[root@rpmbuild ~]# rpm -ivh /home/rpmbuilde/rpmbuild/RPMS/x86_64/very-useful-script-1.0-1.el6.x86_64.rpm |
Hopefully this guide will help you when building RPMs.
Comments
Leave a comment Trackback