Friday 16 December 2011

Simple RPM creation

To create a rpm the following directory structure is needed,

SRPMS : This directory contains the source code of the application in archive format like "test.tar.gz "

BUILD : The source code is extracted and built here

RPMS   : The final rpm's are placed here

SPECS  : The rpm spec files are placed here, it is the source code to build a rpm I've created a simple rpm which just places a file inside a directory, so i don't use "SRPMS " directory as my file is placed directly inside the "BUILD" directory. Below is the spec file,

[madhu@localhost rpmbuild]$ cat SPECS/madhu-test.spec
%define _topdir /home/madhu/rpmbuild     #names starting with % signs are macros,they are like variables define it  and use it anywhere inside the spec file.

%define _tmppath /home/madhu/rpmbuild/tmp  #define the tmp path this is temporary location where the files will be installed during rpmbuild

%define packager madhusudhanan     #define the packager name with email to contact in-case of support

Name: madhu-test #name of the rpm

Version: 1.0 #version number

Release: 1 #release number,basically we need to find a way to increment it automatically for every build
Summary: test file installation #summary about the package
Source: Hello-world #name of the source file
BuildArch: noarch #mention the architecture for which it is built, noarch is architecture independent, if this variable is not defined the rpm will be created for the arch on which it is built
Group: Applications #this rpm belongs to applications group
License: GPL #define the license of the software
BuildRoot: %{_tmppath}/%{name}-buildroot #BuildRoot is the temporary directory used to compile and install the files
%description
Install the test file to a custom location
%install
rm -rf $RPM_BUILD_ROOT #RPM_BUILD_ROOT is a kind of alias to BuildRoot and i am cleaning the temporary directory
install -d $RPM_BUILD_ROOT/usr/local/madhu-test #this command creates the directory if it doesn't exist
install Hello-world $RPM_BUILD_ROOT/usr/local/madhu-test #copy the file "Hello-world" to the location "/home/madhu/rpmbuild/tmp/madhu-test"
%files #mention the files which needs to be included in the rpm in this section
/usr/local/madhu-test/Hello-world #the file along with the path is picked from the temporary directory

[madhu@localhost rpmbuild]$ rpmbuild -bb SPECS/madhu-test.spec #-bb option is to build binary package

[madhu@localhost rpmbuild SPECS]$ ls ../RPMS/noarch/

madhu-test-1.0-1.noarch.rpm

One more thing i was looking for is to use environment variable in the spec file, and below does satisfy the need.

In the spec file, mention the below line

%define workspace  #workspace is the macro for which i will pass value in the command line

run the below command for building the rpm

export WORKSPACE =  /home/madhu/rpmbuild

rpmbuild --define="workspace $WORKSPACE" -bb madhu-test.spec

$WORKSPACE is the environment variable.

This way I've generic spec file and the values can be passed in the command line rather having multiple spec file for the same rpm. One use case is to have multiple Jenkins jobs for building trunk, branches.

For detailed reference see RPM-GUIDE

Monday 5 December 2011

How to setup a user to login as another user without giving password & root privileges

To login as another user we either need to have root access or need to know his password, but in my case  i don't have both, so i searched for how to set up a non root user to login as another user without knowing their password and below setup solves the purpose,

[madhu@localhost ~]$ su - user1
Password:
su: incorrect password

Ah! i entered my password but su expects user1  password  which i am not aware of, ok let me try another way,
[madhu@localhost ~]$ sudo su - user1
Password:
Sorry, user user1 is not allowed to execute '/bin/su - user1' as root on localhost.

Ah, i don't have root access

Here's is the setup, add the below entry in /etc/sudoers file, obviously you need root access to edit this file

madhu ALL=(user1) ALL

# user madhu can run any command in any terminal as user1

[madhu@localhost ~]$ sudo -u user1 -i     ##execute the command -i (login shell) as user1
Password:     #enter password of user "madhu"
-bash-3.2$ whoami
user1

That's it now you can allow access to other users to login in to your account without revealing the password.