Wednesday 15 February 2023

Build RPM From Scratch in RHEL 9


cat /etc/redhat-release

CentOS Stream release 9


RPM:- Package Manager and the package format used by many Linux distributions such as Fedora, RedHat, and CentOS to manage and distribute software in binary form.


rpmbuild directory is known as the root directory for the rpm build environment

tree rpmbuild

rpmbuild

├── BUILD

├── RPMS

├── SOURCES

├── SPECS

└── SRPMS


BUILD        - directory, where the source code of the program want to package, is built

RPMS         - where the rpm packages generated

SOURCES   - have compressed source code of the software inthe form of a tarball or a zip file.

SPECS   - have a .spec file with instructions to build the package

SRPMS   - same as RPMS, but for source RPMS. these special packages contain the original source code of the application.


the spec file is where all instructions and information needed to build an rpm package are defined.


 Build RPM: Example to build an RPM Create a local yum repo.

#as root

yum repolist

yum install -y rpmdevtools rpm-build

mv /etc/yum.repos.d/centos.repo /tmp

yum repolist

ls -l /etc/yum.repos.d/


#su to user account (recommended)

rpmdev-setuptree

ls -l rpmbuild

tree rpmbuild

rpmbuild

├── BUILD

├── RPMS

├── SOURCES

├── SPECS

└── SRPMS


5 directories, 0 files


cd rpmbuild/SOURCES/

ls

mkdir -p localrepo-1/etc/yum.repos.d/

ls

localrepo-1


Mount RHEL 9 ISO File or DVD

sudo mkdir /var/repo

sudo mount -o loop rhel-baseos-9.0-x86_64-dvd.iso /var/repo/

sudo mount /dev/sr0 /var/repo/

vim localrepo-1/etc/yum.repos.d/RHEL9local.repo

[Local-BaseOS]

name=Red Hat Enterprise Linux 9 - BaseOS

metadata_expire=-1

gpgcheck=1

enabled=1

baseurl=file:///var/repo/BaseOS/

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release


[Local-AppStream]

name=Red Hat Enterprise Linux 9 - AppStream

metadata_expire=-1

gpgcheck=1

enabled=1

baseurl=file:///var/repo/AppStream/

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release


 tar -cvzf localrepo-1.tar.gz localrepo-1/

localrepo-1/

localrepo-1/etc/

localrepo-1/etc/yum.repos.d/

localrepo-1/etc/yum.repos.d/RHEL9local.repo


cd ../SPECS/

ls

#create spec file

rpmdev-newspec localrepo.spec

localrepo.spec created; type minimal, rpm version >= 4.16.


 vim localrepo.spec

Name:           localrepo

Version:        1

Release:        0

Summary:        Local Repo For RHEL 9


Group:          System Environment/Base

License:        GPL

URL:            https://www.linuxmissive.com/

Source0:        localrepo-1.tar.gz

BuildArch:      noarch

BuildRoot:      %{_tmppath}/%{name}-buildroot


%description

Create a local yum repo file in /etc/yum.repos.d/ directory.


%prep

%setup -q


%install

mkdir -p "$RPM_BUILD_ROOT"

cp -R * "$RPM_BUILD_ROOT"


%clean

rm -rf $RPM_BUILD_ROOT


%files

%defattr(-,root,root,-)

/etc/yum.repos.d/RHEL9local.repo


%changelog

* Wed Feb 15 2023 Jojan Paul <jpmolekunnel@gmail.com>

-


cd 

#build now

rpmbuild -v -bb rpmbuild/SPECS/localrepo.spec

rpm -qpl rpmbuild/RPMS/noarch/localrepo-1-0.noarch.rpm 

/etc/yum.repos.d/RHEL9local.repo


#install rpm as root

su -

 rpm -ivh /home/devops/rpmbuild/RPMS/noarch/localrepo-1-0.noarch.rpm


rpm -qa | grep localrepo

localrepo-1-0.noarch


 cd /etc/yum.repos.d/

 ls

centos-addons.repo  RHEL9local.repo


#check yum repo working after installing rpm package

yum repolist

repo id                      repo name

Local-AppStream              Red Hat Enterprise Linux 9 - AppStream

Local-BaseOS                 Red Hat Enterprise Linux 9 - BaseOS

extras-common                CentOS Stream 9 - Extras packages


#test

yum install httpd


No comments:

Post a Comment