一、安裝打包工具
yum install rpmdevtools -y

二、建立工作區
rpmdev-setuptree
說明
BUILD:源碼包被解壓至此,并在該目錄的子目錄完成編譯,宏為 %_builddir
BUILDROOT:儲存 %install 階段安裝的檔案,宏為 %_buildrootdir
RPMS:生成/儲存二進制 RPM 包,宏為 %_rpmdir
SOURCES:儲存源碼包(如 .tar 包)和所有 patch 更新檔,宏為 %_sourcedir
SPECS:儲存 RPM 包配置(.spec)檔案,宏為 %_specdir
SRPMS:生成/儲存源碼 RPM 包(SRPM),宏為 %_srcrpmdir
三、建立規格檔案
cd ~/rpmbuild/SPECS/
rpmdev-newspec xx.spec
四、編輯spec檔案
1,内置宏變量
%{_sysconfdir} /etc
%{_prefix} /usr
%{_exec_prefix} %{_prefix}
%{_bindir} %{_exec_prefix}/bin
%{_lib} lib (lib64 on 64bit systems)
%{_libdir} %{_exec_prefix}/%{_lib}
%{_libexecdir} %{_exec_prefix}/libexec
%{_sbindir} %{_exec_prefix}/sbin
%{_sharedstatedir} /var/lib
%{_datadir} %{_prefix}/share
%{_includedir} %{_prefix}/include
%{_oldincludedir} /usr/include
%{_infodir} /usr/share/info
%{_mandir} /usr/share/man
%{_localstatedir} /var
%{_initddir} %{_sysconfdir}/rc.d/init.d
%{_topdir} %{getenv:HOME}/rpmbuild
%{_builddir} %{_topdir}/BUILD
%{_rpmdir} %{_topdir}/RPMS
%{_sourcedir} %{_topdir}/SOURCES
%{_specdir} %{_topdir}/SPECS
%{_srcrpmdir} %{_topdir}/SRPMS
%{_buildrootdir} %{_topdir}/BUILDROOT
%{_var} /var
%{_tmppath} %{_var}/tmp
%{_usr} /usr
%{_usrsrc} %{_usr}/src
%{_docdir} %{_datadir}/doc
%{buildroot} %{_buildrootdir}/%{name}-%{version}-%{release}.%{_arch}
$RPM_BUILD_ROOT %{buildroot}
2,參數解析
Name: 軟體包的名稱,後面可使用%{name}的方式引用
Summary: 軟體包的内容概要
Version: 軟體的實際版本号,例如:1.0.1等,後面可使用%{version}引用
Release: 釋出序列号,例如:1linuxing等,标明第幾次打包,後面可使用%{release}引用
Group: 軟體分組,建議使用标準分組
License: 軟體授權方式,通常就是GPL
Source: 源代碼包,可以帶多個用Source1、Source2等源,後面也可以用%{source1}、%{source2}引用
BuildRoot: 這個是安裝或編譯時使用的“虛拟目錄”,考慮到多使用者的環境,一般定義為:%{tmppath}/{name}-%{version}-%{release}-root或%{tmppath}/%{name}-%{version}-%{release}-buildroot-%%__id_u} -n}.該參數非常重要,因為在生成rpm的過程中,執行make install時就會把軟體安裝到上述的路徑中,在打包的時候,同樣依賴“虛拟目錄”為“根目錄”進行操作。後面可使用$RPM_BUILD_ROOT 方式引用。
URL: 軟體的首頁
Vendor: 發行商或打包組織的資訊,例如RedFlag Co,Ltd
Disstribution: 發行版辨別
Patch: 更新檔源碼,可使用Patch1、Patch2等辨別多個更新檔,使用%patch0或%{patch0}引用
Prefix: %{_prefix} 這個主要是為了解決今後安裝rpm包時,并不一定把軟體安裝到rpm中打包的目錄的情況。這樣,必須在這裡定義該辨別,并在編寫%install腳本的時候引用,才能實作rpm安裝時重新指定位置的功能
Prefix: %{sysconfdir} 這個原因和上面的一樣,但由于%{prefix}指/usr,而對于其他的檔案,例如/etc下的配置檔案,則需要用%{_sysconfdir}辨別
Build Arch: 指編譯的目标處理器架構,noarch辨別不指定,但通常都是以/usr/lib/rpm/marcros中的内容為預設值
Requires: 該rpm包所依賴的軟體包名稱,可以用>=或<=表示大于或小于某一特定版本,例如:libpng-devel >= 1.0.20 zlib ※“>=”号兩邊需用空格隔開,而不同軟體名稱也用空格分開,還有例如PreReq、Requires(pre)、Requires(post)、Requires(preun)、Requires(postun)、BuildRequires等都是針對不同階段的依賴指定
Provides: 指明本軟體一些特定的功能,以便其他rpm識别
Packager: 打包者的資訊
%description 軟體的詳細說明
%prep 預處理腳本
%setup -n %{name}-%{version}** 把源碼包解壓并放好通常是從/usr/src/asianux/SOURCES裡的包解壓到/usr/src/asianux/BUILD/%{name}-%{version}中。一般用%setup -c就可以了,但有兩種情況:一就是同時編譯多個源碼包,二就是源碼的tar包的名稱與解壓出來的目錄不一緻,此時,就需要使用-n參數指定一下了。
%setup 不加任何選項,僅将軟體包打開。
%setup -n newdir 将軟體包解壓在newdir目錄。
%setup -c 解壓縮之前先産生目錄。
%setup -b num 将第num個source檔案解壓縮。
%setup -T 不使用default的解壓縮操作。
%setup -T -b 0 将第0個源代碼檔案解壓縮。
%setup -c -n newdir 指定目錄名稱newdir,并在此目錄産生rpm套件。
%patch 打更新檔通常更新檔都會一起在源碼tar.gz包中,或放到SOURCES目錄下。一般參數為:
%patch -p1 使用前面定義的Patch更新檔進行,-p1是忽略patch的第一層目
%Patch2 -p1 -b xxx.patch 打上指定的更新檔,-b是指生成備份檔案
%patch 最簡單的更新檔方式,自動指定patch level。
%patch 0 使用第0個更新檔檔案,相當于%patch ?p 0。
%patch -s 不顯示打更新檔時的資訊。
%patch -T 将所有打更新檔時産生的輸出檔案删除。
%configure 這個不是關鍵字,而是rpm定義的标準宏指令。意思是執行源代碼的configure配置,在/usr/src/asianux/BUILD/%{name}-%{version}目錄中進行 ,使用标準寫法,會引用/usr/lib/rpm/marcros中定義的參數。另一種不标準的寫法是,可參考源碼中的參數自定義,例如:引用CFLAGS="$RPM_OPT_FLAGS" CXXFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=%{_prefix}
%build 開始建構包,在/usr/src/asianux/BUILD/%{name}-%{version}目錄中進行make的工作
%install 開始把軟體安裝到虛拟的根目錄中.在/usr/src/asianux/BUILD/%{name}-%{version}目錄中進行make install的操作。這個很重要,因為如果這裡的路徑不對的話,則下面%file中尋找檔案的時候就會失敗。 常見内容有:
%makeinstall 這不是關鍵字,而是rpm定義的标準宏指令。也可以使用非标準寫法:make DESTDIR=$RPM_BUILD_ROOT install或引用make prefix=$RPM_BUILD_ROOT install,需要說明的是,這裡的%install主要就是為了後面的%file服務的。是以,還可以使用正常的系統指令:引用install -d $RPM_BUILD_ROOT/和cp -a * $RPM_BUILD_ROOT/
%clean 清理臨時檔案
%pre rpm安裝前執行的腳本
%post rpm安裝後執行的腳本
%preun rpm解除安裝前執行的腳本
%postun rpm解除安裝後執行的腳本
%files 定義那些檔案或目錄會放入rpm中
%defattr (-,root,root)** 指定包裝檔案的屬性,分别是(mode,owner,group),-表示預設值,對文本檔案是0644,可執行檔案是0755
%changelog 變更日志
3,經典配置
1 Name: zabbix
2 Version: 3.0.3
3 Release: 1%{?dist}
4 Summary: zabbix_agentd
5
6 Group: zabbix
7 License: GPL
8 URL: www.yang.com
9 Source0: zabbix-3.0.3.tar.gz
10 Source1: zabbix_agentd
11 Source2: nginx_monitor.sh
12 Source3: userparameter_nginx.conf
13 Source4: hostmonitor.conf
14 Source5: process.discovery
15 Source6: userparameter_process.conf
16 Source7: redis_check.conf
17 Source8: redis_cluster_check.py
18 Source9: redis_multiport_check.py
19 Source10: tcp_connections.sh
20 Source11: iostat-collect.sh
21 Source12: iostat-parse.sh
22 Source13: iostat-zabbix.conf
23
24 BuildRequires: gcc, gcc-c++
25 Requires: gcc, gcc-c++, chkconfig
26
27 %description
28 Zabbix agentd 3.0.3
29
30 %pre
31 grep zabbix /etc/passwd > /dev/null
32 if [ $? != 0 ]
33 then useradd zabbix -M -s /sbin/nologin
34 fi
35 [ -d /etc/zabbix ]||rm -rf /etc/zabbix
36 [ -d /etc/zabbix ]||rm -rf /etc/zabbix-3.0.3
37
38
39 %post
40 sed -i "/^ServerActive=/c\ServerActive=172.30.17." /etc/zabbix/etc/zabbix_agentd.conf
41 sed -i "/^Server=/c\Server=172.30.17." /etc/zabbix/etc/zabbix_agentd.conf
42 sed -i "/Timeout=3/c\Timeout=30" /etc/zabbix/etc/zabbix_agentd.conf
43 sed -i "/HostMetadata=/c\HostMetadata=OPS-TMP" /etc/zabbix/etc/zabbix_agentd.conf
44 sed -i "/^Hostname=/c\Hostname=OPS-TMP" /etc/zabbix/etc/zabbix_agentd.conf
45 echo "UnsafeUserParameters=1" >>/etc/zabbix/etc/zabbix_agentd.conf
46 echo "EnableRemoteCommands=1" >>/etc/zabbix/etc/zabbix_agentd.conf
47 echo "Include=/etc/zabbix/etc/zabbix_agentd.conf.d/*.conf" >>/etc/zabbix/etc/zabbix_agentd.conf
48 chkconfig zabbix_agentd on
49
50 %preun
51 systemctl stop zabbix_agentd
52 %postun
53 userdel zabbix
54 rm -rf /etc/zabbix*
55 %prep
56 %setup -q
57
58
59 %build
60 ./configure --prefix=/etc/%{name}-%{version} --enable-agent
61 make -j16 %{?_smp_mflags}
62
63 %install
64 test -L %{buildroot}/etc/%{name} && rm -f %{buildroot}/etc/%{name}
65 install -d %{buildroot}/etc/profile.d
66 make install DESTDIR=%{buildroot}
67 install -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/init.d/zabbix_agentd
68 install -p -D %{SOURCE2} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/nginx_monitor.sh
69 install -p -D %{SOURCE3} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/userparameter_nginx.conf
70 install -p -D %{SOURCE4} %{buildroot}/etc/nginx/conf.d/hostmonitor.conf
71 install -p -D %{SOURCE5} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/process.discovery
72 install -p -D %{SOURCE6} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/userparameter_process.conf
73 install -p -D %{SOURCE7} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_check.conf
74 install -p -D %{SOURCE8} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_cluster_check.py
75 install -p -D %{SOURCE9} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_multiport_check.py
76 install -p -D %{SOURCE10} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/tcp_connections.sh
77 install -p -D %{SOURCE11} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-collect.sh
78 install -p -D %{SOURCE12} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-parse.sh
79 install -p -D %{SOURCE13} %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-zabbix.conf
80
81 echo 'export PATH=/etc/zabbix/bin:/etc/zabbix/sbin:$PATH' > %{buildroot}/etc/profile.d/%{name}.sh
82 ln -sf /etc/%{name}-%{version} %{buildroot}/etc/%{name}
83
84 %files
85 %defattr (-,root,root,0755)
86 /etc/%{name}
87 /etc/%{name}-%{version}/*
88 /etc/profile.d/%{name}.sh
89 /etc/nginx/conf.d/hostmonitor.conf
90 %attr(0755,root,root) /etc/rc.d/init.d/zabbix_agentd
91 %changelog
92 %clean
93 rm -rf %{buildroot}
4,測試配置
1 Name: typesetting
2 Version: 1.0
3 Release: 1%{?dist}
4 Summary: typesetting
5 License: guanghe
6 Source0: test.sh
7
8 %description
9
10 %prep
11
12 %build
13
14 %install
15 install -p -D -m 755 %{SOURCE0} %{buildroot}/opt/typesetting/test.sh
16 install -d %{buildroot}/etc/profile.d
17 echo '/opt/typesetting/test.sh' > %{buildroot}/etc/profile.d/%{name}.sh
18 chmod 755 %{buildroot}/etc/profile.d/%{name}.sh
19 %files
20 /opt/typesetting/test.sh
21 /etc/profile.d/%{name}.sh
22 %changelog
23
24 %post
25 /etc/profile.d/%{name}.sh
五、拷檔案、打包、安裝、解除安裝
1,将需要安裝的腳本檔案放到SOURCES下
2,編輯建構配置檔案
3,打包
rpmbuild -bb xx.spec
4,檢視打好的包
cd ~/rpmbuild/RPMS/x86_64/
5,安裝與解除安裝
rpm -ivh xx.rpm
rpm -e xx
ok
六、優化配置
1 #compile cmd: rpmbuild -bb typesetting.spec
2 Name: typesetting
3 Version: 1
4 Release: 1
5 Summary: typesetting
6
7 License: Beijing Founder Electronic Co.,LTD
8 Source0: typesetting.tar.gz
9
10 %description
11 founder typesetting
12
13 %prep
14
15 %install
16 rm -rf $RPM_BUILD_ROOT
17 mkdir -p $RPM_BUILD_ROOT/root/tmp
18 install -m 755 %{SOURCE0} $RPM_BUILD_ROOT/root/tmp/typesetting.tar.gz
19
20 %files
21 /root/tmp/typesetting.tar.gz
22
23 %post
24 cd /root/tmp
25 tar -zxf typesetting.tar.gz
26 mkdir /opt/typesetting
27 cd typesetting
28 cp * /opt/typesetting
29 rm -rf *
30 chmod 755 /opt/typesetting/*.sh
31 echo '/opt/typesetting/start.sh' >> /etc/profile
32 source /etc/profile
33
34 %preun
35 /opt/typesetting/stop.sh
36
37 %postun
38 rm -rf /opt/typesetting
39 rm -rf /root/tmp
40 sed -i '/\/opt\/typesetting\/start.sh/d' /etc/profile