How to create RPM packages:
To create rpm packages we need to set up our system first. we need some developmental tools and user account to create rpm packages.
#yum groupinstall "Development Tools"
#yum install rpmdevtools { in fedora}
in centos
#yum install redhat-rpm-config
#useradd rpdev
# su - rpdev
After installing the rpmbuild package, we need to create the files and direcotries under the home directory of the user to be used for building rpm packages. To avoid possible system libraries and other files damage, you should never build an rpm with the root user. we should always use a non-privileged user account to do this.
To create rpm's we need a directory structure and .rpmmacros file under the home directory overriding the default location of the RPM building tree.
$mkdir-p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
and then
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
Building rpm's is like building and compiling the software from either the source or source rpms, to do that we need tools to compile and build source packages such as make and gcc
# yum install make
# yum install gcc
To create an RPM package we need to create a ".spec" file that describes the information about the software being packaged. we then run the "rpmbuild" command on the spec file, which will carry out the steps specified in the spec file to create the described packages.
Usually we'll place the sources (tar.gz) files into "~/rpmbuild/SOURCES", spec file in "~/rpmbuild/SPECS/" and the name of the spec file should be the base name of the package. To create all packages(both binary and source packages), we have to run the following command from the SPECS directory:
$ rpmbuild -ba 'name'.spec
There are different statges while rpmbuild reads, writes and executes the instructions in the spec file such as %_specdir{~/rpmbuild/SPECS}, %_sourcedir{~/rpmbuild/SOURCES}, %_builddir {~/rpmbuild/BUILD source files are unpacked and compiled in a subdirectory underneath this}, %_buildrootdir {~/rpmbuild/BUILDROOT}, %_rpmdir {~/rpmbuild/RPMS binary rpms are created and stored here } and %_srcrpmdir {~/rpmbuild/SRPMS Source rpm directory}.
To package a program, its probably best if we try a dry run going through the build and installation procedure with rpm.
Creating a blank spec file
$ cd ~/rpmbuild/SPECS
$ vi program.spec
The following is an example of what the template may be like:
Name:
Version:
Release: 1%{?dist}
Summary:
Group:
License:
URL:
Source0:
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires:
Requires:
%description
%prep
%setup -q
%build
%configure
make %{?_smp_mflags}
%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot}
%clean
rm -rf %{buildroot}
%files
%defattr(-,root,root,-)
%doc
%changelog
Using source rpms:
----------------------
We can also build software from source rpms,
$rpm -ivh sourcepackage-name*.src.rpm
This places the package's .spec file into ~/rpmbuild/SPECS and other source and patch files in ~/rpmbuild/SOURCES.
We can also unpack the .src.rpm in a directory using rpm2cpio:
$ mkdir PROGRAMNAME_src_rpm
$ cd PROGRAMNAME_src_rpm
$ rpm2cpio ../PROGRAMNAME-*.src.rpm | cpio -i
Creating RPMs from the spec file
-------------------------------------
$ rpmbuild -ba program.spec
if this works then binary rpm files are created under ~/rpmbuild/RPMS/ and the source rpm will be in ~/rpmbuild/SPRMS.
As an example we'll create wget rpm's:
By creating an rpm we'll automate the process of untaring the sources, running ./configure command, make command and make install commands.
we can specify the directory to be installed to ./configure with --prefix=/fullpathe. To automate the process we'll place the code in the sources directory and write a confiugration to dictate where to find the source to be compiled and how to build and install the code. The configuration (spec) file is the input the utility called rpmbuild. Therefore copy the source file into the ~/rpmbuild/SOURCES directory. Then create a spec file as follows:
Troubleshooting:
"error: File /usr/src/redhat/SOURCES/nano-1.2.0.tar.gz: No such file or directory" when trying to rpmbuild -ba *.spec. Solution: rpmbuild can not see your /etc/rpm/macros or $HOME/.rpmmacros.
Can not write to /var/tmp/nano-1.2-1-root. Solution: change the buildroot in spec file to Buildroot: %{_buildroot}
What "Group:" entry should I use? Solution: To see all valid groups less /usr/share/doc/rpm-*/GROUPS
RPM build errors: Installed (but unpackaged) files found:..... Add the name of the unpackaged files to the %files section of the spec file to resolve the error
Further advanced usage details will be followed shortly.
References:
1) http://www.ibm.com/developerworks/library/l-rpm1/
2) http://wiki.centos.org/HowTos/SetupRpmBuildEnvironment
3) http://fedoraproject.org/wiki/How_to_create_an_RPM_package
4) http://www.ibm.com/developerworks/linux/library/l-rpm2/index.html
5) http://www.ibm.com/developerworks/linux/library/l-rpm3/index.html
No comments:
Post a Comment