簡介
rpmbuild是用來打包rpm包的工具
構(gòu)建流程
- 安裝工具
#配置好yum源,使用系統(tǒng)安裝鏡像就行 yum install -y rpmdevtools - 創(chuàng)建目錄
rpmdev-setuptree #創(chuàng)建目錄~/rpmbuild和文件~/.rpmmacro tree ~/rpmbuild #查看rpmbuild下面的文件結(jié)構(gòu)目錄 用途 BUILD 構(gòu)建過程使用的目錄 RPMS 存放構(gòu)建好的rpm包 SOURCES 存放用來構(gòu)建的腳本和源代碼 SPECS 存放xxx.spec文件 SRPMS 存放構(gòu)建好的xxx.src.rpm文件,xxx.src.rpm包含 SOURCES和SPECS下的文件,可以使用這些文件自己構(gòu)建 - spec
rpmbuild是根據(jù)一個配置文件構(gòu)建rpm包的,配置文件一般放在~/rpmbuild/SPECS下面,后綴一般是.spec,這個文件的作用有點像make的配置文件Makefile,下面是一個簡單的hello.spec文件示范。#配置文件示范,rpmdev-newspec xxx.spec可以生成簡單的spec #hello.spec Name: hello Version: 1 Release: 1%{?dist} #%{?dist}是宏,rpm --eavl %{?dist} 可以查看宏的值 Summary: dsq test License: FIXME %description dsq test %prep #do nothing echo prep--------------------------- %build echo build------------------------- cat >helloworld.sh <<EOF #!/usr/bin/bash echo hello world EOF %install mkdir -p %{buildroot}/usr/bin install -m 7555 helloworld.sh %{buildroot}/usr/bin/helloworld.sh %files /usr/bin/helloworld.sh #changelog格式 :第一行*開頭記錄日期,第二行以后都是-開頭 %changelog * Mon Jan 30 2023 dsq - - 構(gòu)建階段
spec文件里面除了基礎(chǔ)的描述信息Name,Version等,然后就是各個構(gòu)建階段要執(zhí)行的命令配置。
構(gòu)建命令階段 任務(wù) %prep 準(zhǔn)備階段
rpmbuild -bp hello.spec會執(zhí)行這個階段的指令%build 構(gòu)建階段,~/rpmbuild/BUILD是默認(rèn)工作路徑;
rpmbuild -bc hello.spec,在~/rpmbuild/BUILD下面生成helloworld.sh%install 安裝階段
rpmbuild -bi hellp.spec,執(zhí)行這個命令會將helloworld.sh拷貝到 ~/rpmbuild/BUILDROOT/usr/bin/;而且這里的配置會導(dǎo)致最后安裝rpm包的時候,helloworld.sh被安裝到/usr/bin;所以配置文件中的%{buildroot}在構(gòu)建階段代表 ~/rpmbuild/BUILDROOT,在rpm包安裝階段代表系統(tǒng)的根目錄%files rpm包安裝的文件列表 rpmbuild -bp hello.spec #執(zhí)行每個構(gòu)建命令之后使用tree ~/rpmbuild查看生成的文件 rpmbuild -bc hello.spec rpmbuild -bi hello.spec rpmbuild -bl hello.spec #校驗%files rpmbuild -bb hello.spec #創(chuàng)建二進(jìn)制包 rpmbuild -bs hello.spec #創(chuàng)建src包
實踐
在我的systemd和logrotate文章里面配置了一個使用vmstat記錄系統(tǒng)性能的服務(wù),這里我將文件打包到一個rpm包里面,方便安裝到其他系統(tǒng)。
#mvstat.spec
Name: mvmstat
Version: 1
Release: 1
Summary: use vmstat to record system performance
BuildArch: noarch
License: GPL
Requires: systemd>=239-58 procps-ng logrotate
Source0: %{name}-%{version}.tar.gz
%description
use vmstat to record system performance
wirte by dsq on 2023.2.1
%prep
echo prep-------------------------
#setpup -q是將Source0指定的壓縮包解壓到BUILD目錄
%setup -q
%build
echo build-------------------------
%install
mkdir -p %{buildroot}/usr/lib/systemd/system/
mkdir -p %{buildroot}/etc/logrotate.d
mkdir -p %{buildroot}/home/monitor
install -m 0644 mvmstat.service %{buildroot}/usr/lib/systemd/system/mvmstat.service
install -m 0644 vmstat %{buildroot}/etc/logrotate.d/vmstat
#安裝軟件包前檢查是否已經(jīng)安裝過了
$pre
if [ $1 -gt 1 ] ; then
echo "do nothing; mvmstat was installed before, please check!"
fi
#安裝軟件包之后執(zhí)行下列命令
%post
systemctl stop mvmstat.service
systemctl enable mvmstat.service
systemctl start mvmstat.service
systemctl enable mvmstat.service
#卸載軟件包之前執(zhí)行下列命令
%preun
systemctl stop mvmstat.service
systemctl disable mvmstat.service
#安裝軟件包會安裝的文件和目錄
%files
/usr/lib/systemd/system/mvmstat.service
/etc/logrotate.d/vmstat
/home/monitor/
%changelog
* Mon Jan 30 2023 dsq
-
rpmbuild -bb mvmstat.spec
#在~/rpmbuild/RPMS/noarch下面生成mvmstat-1-1.noarch.rpm包
rpm -qip mvmstat-1-1.noarch.rpm #查看rpm包信息
rpm -ivh mvmstat-1-1.noarch.rpm #安裝rpm包
rpm -ql mvmstat #查看rpm包安裝了哪些文件
systemctl statu mvmstat #查看安裝的服務(wù)狀態(tài)
rpm -e mvmstat #卸載軟件包