In this blogpost I’ll write my notes on creating a custom RPM. In one of my other posts I needed to modify the source code of a specific piece of Zabbix software; the Zabbix Java Gateway.
I like to have my software in RPMS for easy deployment, here are the steps I took to create a zabbix-java-gateway RPM with the modified source code.
You can find the resulting RPM here.
My system is a clean CentOS 6.5 x86_64 machine. Building RPMS is done as a non privileged user, so I assume you have a user present in your environment. All steps are performed by the user “dkanbier” unless specified otherwise.
1. Setup the build environment
Get the needed software:
$ sudo yum -y update $ sudo yum -y groupinstall "Development Tools" $ sudo yum -y install rpmdevtools
Create the required directory structure:
This creates a directory structure and .rpmmacros file:
ls -al |grep rpm drwxrwxr-x. 7 dkanbier dkanbier 4096 Jan 22 13:23 rpmbuild -rw-rw-r--. 1 dkanbier dkanbier 135 Jan 22 13:23 .rpmmacros
2. Get the SRPM (Source RPM) from the software we need to modify
In this case we will need the zabbix SRPM file, which can be found here: zabbix-2.2.1-1.el6.src.rpm
$ wget http://repo.zabbix.com/zabbix/2.2/rhel/6/SRPMS/zabbix-2.2.1-1.el6.src.rpm
Install it in the rpmbuild structure:
$ rpm -ivh zabbix-2.2.1-1.el6.src.rpm
Be sure to check out what gets installed. Particularly interesting is the SPEC file in rpmbuild/SPECS/zabbix.spec.
But we came here to modify the source code, so let’s look for that.
3. Modify the source code
In ~/rpmbuild/SOURCES you’ll find a tar.gz archive named zabbix-2.2.1.tar.gz. This contains the entire zabbix source files and we need to modify a file there.
Let’s unpack, modify the file and pack it again.
$ cd ~/rpmbuild/SOURCES $ tar -xzf zabbix-2.2.1.tar.gz
The file I’m interested in is zabbix-2.2.1/src/zabbix_java/src/com/zabbix/gateway/JMXItemChecker.java.
Open it and change:
All done with the source code, let’s zip it again so we can rebuild the RPMS:
$ rm -rf zabbix-2.2.1.tar.gz $ tar -czf zabbix-2.2.1.tar.gz zabbix-2.2.1/
So we’re back to where we started, but with the modified source code in the tar.gz file.
4. Build the new RPMS
Now our system needs to have all the development packages installed which are required for the zabbix software. If we try to build now, we’d run into errors like:
$ rpmbuild -ba rpmbuild/SPECS/zabbix.spec error: Failed build dependencies: postgresql-devel is needed by zabbix-2.2.1-1.el6.x86_64 net-snmp-devel is needed by zabbix-2.2.1-1.el6.x86_64 openldap-devel is needed by zabbix-2.2.1-1.el6.x86_64 gnutls-devel is needed by zabbix-2.2.1-1.el6.x86_64 iksemel-devel is needed by zabbix-2.2.1-1.el6.x86_64 sqlite-devel is needed by zabbix-2.2.1-1.el6.x86_64 unixODBC-devel is needed by zabbix-2.2.1-1.el6.x86_64 OpenIPMI-devel >= 2 is needed by zabbix-2.2.1-1.el6.x86_64 libssh2-devel >= 1 is needed by zabbix-2.2.1-1.el6.x86_64 java-devel >= 1.6.0 is needed by zabbix-2.2.1-1.el6.x86_64
So let’s install these dependencies first. You can install all but 1 from the default CentOS repositories:
$ sudo yum -y install postgresql-devel net-snmp-devel openldap-devel gnutls-devel sqlite-devel unixODBC-devel OpenIPMI-devel libssh2-devel java-devel
iksemel-devel is not available in the default CentOS repositories. This is found in the EPEL repository however, I’ll add it really quick:
$ wget http://nl.mirror.eurid.eu/epel/6/i386/epel-release-6-8.noarch.rpm $ sudo rpm -Uvh epel-release-6-8.noarch.rpm
Install the iksemel-devel package:
$ sudo yum -y install iksemel-devel
Now build the RPM packages using the rpmbuild command. This will compile all the sources, so it will take a while and produce a lot of output. Just wait till it finishes and tells us everything went fine. With that much output we’ll just have to take it’s word for it.
When it’s done, take a look at the last lines of the output:
Wrote: /home/dkanbier/rpmbuild/SRPMS/zabbix-2.2.1-1.el6.src.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-agent-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-get-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-sender-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-server-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-server-mysql-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-server-pgsql-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-proxy-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-proxy-mysql-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-proxy-pgsql-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-proxy-sqlite3-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-java-gateway-2.2.1-1.el6.x86_64.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/noarch/zabbix-web-2.2.1-1.el6.noarch.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/noarch/zabbix-web-mysql-2.2.1-1.el6.noarch.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/noarch/zabbix-web-pgsql-2.2.1-1.el6.noarch.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/noarch/zabbix-web-japanese-2.2.1-1.el6.noarch.rpm Wrote: /home/dkanbier/rpmbuild/RPMS/x86_64/zabbix-debuginfo-2.2.1-1.el6.x86_64.rpm Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.FEZtdI + umask 022 + cd /home/dkanbier/rpmbuild/BUILD + cd zabbix-2.2.1 + rm -rf /home/dkanbier/rpmbuild/BUILDROOT/zabbix-2.2.1-1.el6.x86_64 + exit 0
Great, exit status 0 and it tells me where it wrote the RPMS. In my case I’m only interested in the zabbix-java-gateway RPM file, because that contains the modification I made.
On my Zabbix server I can now just remove the existing RPM and replace it with this one, add the JBoss EAP 6 remoting-jmx libraries to /usr/sbin/zabbix_java/lib and start the zabbix-java-gateway.
There is actually a lot more to creating RPM’s. For example, it would have been nice to update the version number of the RPM and tell people who made it using the .spec file.
The only thing that let’s people know somebody else than Zabbix made this RPM is bij looking at the Build Host when you query the RPM:
$ rpm -qip rpmbuild/RPMS/x86_64/zabbix-java-gateway-2.2.1-1.el6.x86_64.rpm Name : zabbix-java-gateway Relocations: (not relocatable) Version : 2.2.1 Vendor: (none) Release : 1.el6 Build Date: Wed 22 Jan 2014 02:04:59 PM CET Install Date: (not installed) Build Host: zabbix-dev.kanbier.lan Group : Applications/Internet Source RPM: zabbix-2.2.1-1.el6.src.rpm Size : 622630 License: GPLv2+ Signature : (none) URL : http://www.zabbix.com/ Summary : Zabbix java gateway Description : The Zabbix java gateway
And of course the RPM wouldn’t be signed by Zabbix’s GPG key.