Friday, 17 June 2011

Build a simple RPM that packages a single file

I find this objective bizarre, although perhaps I should be used by now that some objectives just don't make sense. In this case, I just think that this is something that it is unlikely to be done by system admin. To be fair it is much cooler to give somebody an rpm with your scripts but still.

The first thing is to install the rpmdevtools package:
yum install rpmdevtools -y
You now need to create the source tree, which you can do manually (You'll need BUILD,BUILDROOT, RPMS, SPECS,SOURCE,SRPMS) or you can just use:
rpmdev-setuptree
Since the objective calls for a least one file to be packaged, let's just create a script, say greetings.sh, and copy it to its own folder in the home/user/rpmbuild/SOURCES directory:
echo 'echo "hello `whoami`";' >> greetings.sh; chmod +x greetings.sh ; mkdir /home/user/rpmbuild/SOURCES/greet-1.0; cp greetings.sh /home/user/rpmbuild/SOURCES/greet-1.0
Create a tarball out of the directory with the greetings.sh script, from SOURCES directory:
tar -czvf greet.tar.gz greet-1.0
Now, you need a spec file and you can create a sample spec file with, again from SOURCES directory:
rpmdev-newspec ../SPECS/greet.spec
You can now edit this file to this:
Name:           greet
Version:       1.0
Release:        1%{?dist}
Summary:        Greets the invoker

Group:         Greetings Group
License:        GPL
URL:          http://www.sgniteerg.com
Source0:        greet.tar.gz
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)


%description

%prep
%setup -q
%build
%install
install -m 0755 -d $RPM_BUILD_ROOT/opt/greet
install -m 0777 greetings.sh $RPM_BUILD_ROOT/opt/greet/greetings.sh

%clean
#rm -rf $RPM_BUILD_ROOT

%files
%dir /opt/greet
/opt/greet/greetings.sh

%defattr(-,root,root,-)
%doc
%changelog
You can build this file with from the SPECS directory:
rpmbuild -bb greet.spec
This will create an rpm, greet-1.0-1.el6.x86_64.rpm, in the RPMS/x86_64 directory of your buildtree, which you can then proceed to install. If you are not using the x86_64 architecture, the directory will be different. You could add BuildArch: noarch to the spec file.

You can now package all your scripts and pass them to your friends as an rpm file. How cool is that?

1 comment: