1 安装rpmbuild,并配置环境
1.1 首先,你应该检查 rpmbuild 是否已经被安装在你的系统上。你将会利用这个工具从 spec 文件或SRPM 组件创建 RPM。要检查它是否被安装了,请执行 rpmbuild --showrc 这个指令。它将会显示一大组数据,把 rpmbuild 所采用的创建环境的细节列明。假若系统输出:$ rpmbuild: command not found,便意味着 rpmbuild 未被安装。你可以用 root 身份执行下列指令,通过 yum 来安装它:
[root@host ~]# yum install rpm-build
[user@hostname ~]$ mkdir -p /usr/src/redhat/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
2 RPM相关知识点说明
rpmbuild安装抬会在/root的目录下生成一个rpmbuild的目录,共内部结构如下:
Insidethe spec file,请参考:http://wiki.mandriva.com/en/Development/Howto/RPM#Inside_the_spec_file http://blog.csdn.net/redguardtoo/article/details/1373885
3 准备必要的文件
[root@host ~]# mkdir tomcat-myweb-1.0
[root@host ~]# mv /root/tomcat /root/tomcat-myweb-1.0
[root@host ~]# vi tomcat-myweb-1.0.spec
Summary:myweb
Name:tomcat-myweb
Version:1.0
Release:0
BuildRoot:%{_tmppath}/%{name}-%{version}-root
Source0:tomcat-myweb-1.0.tar.gz
License:GPL
Group:Applications/Productivity/vbox
%description
losoel myweb RPM package
%prep
rm -rf $RPM_BUILD_DIR/%{name}-%{version}
%setup -q
%build
%install
rm -fr $BuildRoot
mkdir -p $RPM_BUILD_ROOT/usr/local/myweb
cp -rf * $RPM_BUILD_ROOT/usr/local/myweb
if [ ! -d /usr/local/myweb ]
then
{
mkdir -p /usr/local/myweb
}
fi
chmod 755 /usr/local/myweb -R
cp -rf * /usr/local/myweb
%files
%defattr(-,root,root)
/usr/local/myweb
%pre
%post
if [ -d /usr/local/myweb/ ]
then
{
bash /usr/local/myweb/preinst
}
fi
%preun
if [ -d /usr/local/myweb/ ]
then
{
bash /usr/local/myweb/prerm
}
fi
%clean
rm -fr $BuildRoot
其它辅助脚本: preinst
[root@host ~]# vi preinst
#!/bin/bash
function write_log()
{
dateoftod=`date +%Y%m%d`
logfilename="log-myweb-$dateoftod"
if [ ! -d /usr/local/myweb/myweb-logs ]
then
{
mkdir -p /usr/local/myweb/myweb-logs
}
fi
if [ ! -f /usr/local/myweb/myweb-logs/$logfilename ]
then
{
touch /usr/local/myweb/myweb-logs/$logfilename
}
fi
if [ $# -eq 2 ]
then
echo "$(date "+%Y-%m-%d %H:%M:%S") [$0:$1] $2" >>/usr/local/myweb/myweb-logs/$logfilename
elif [ $# -eq 3 ]
then
echo "$(date "+%Y-%m-%d %H:%H:%S") [$0:$1] $2 : $3">>/usr/local/myweb/myweb-logs/$logfilename
else
echo "record log error."
fi
}
echo "-------------------------------------------------------------------------"
echo " Welcome install myweb software for cloud platform security system."
echo "-------------------------------------------------------------------------"
write_log $LINENO "INFO: install process start."
if [ $UID -ne 0 ]
then
{
write_log $LINENO "FATAL: user privilege error."
echo "Error:Superuser privilege are required to run this script."
exit -1
}
fi
write_log $LINENO "INFO: user privilege correct."
#1:check mysql installed
#3:check JDK 1.6 1.7
checkjre=$(java -version 2>&1|grep "java version")
if [ "$checkjre" = "" ]
then
{
write_log $LINENO "FATAL: Please install jre first."
echo "Please install jre first."
exit -1
}
else
{
jre_version=$(java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \" | awk '{split($0, array, ".")} END{print array[1]"."array[2]}')
cmp_version="1.6"
result_cmp=$(expr $jre_version \>= $cmp_version)
if [ $result_cmp -eq 0 ]
then
{
write_log $LINENO "FATAL: Jre version too low! Please install version 1.6 or higher."
echo "Jre version too low! Please install version 1.6 or higher."
exit -1
}
fi
}
fi
其它辅助脚本:prerm
[root@host ~]# mkdir prerm
#!/bin/bash
function write_log()
{
dateoftod=`date +%Y%m%d`
logfilename="log-myweb-$dateoftod"
if [ ! -d /usr/local/myweb/myweb-logs ]
then
{
mkdir -p /usr/local/myweb/myweb-logs
}
fi
if [ ! -f /usr/local/myweb/myweb-logs/$logfilename ]
then
{
touch /usr/local/myweb/myweb-logs/$logfilename
}
fi
if [ $# -eq 2 ]
then
echo "$(date "+%Y-%m-%d %H:%M:%S") [$0:$1] $2" >>/usr/local/myweb/myweb-logs/$logfilename
else
echo "record log error."
fi
}
write_log $LINENO "INFO: start uninstall myweb process."
if [ $UID -ne 0 ]
then
{
write_log $LINENO "FATAL: user privilege error."
echo "Error:Superuser privilege are required to run this script."
echo "error">rmlog
exit -1
}
fi
#3 del /usr/local/myweb
if [ -d /usr/local/myweb ]
then
{
write_log $LINENO "delete tomcat-myweb."
rm -rf /usr/local/myweb
}
fi
[root@host ~]# mv ./preinst ./tomcat-myweb-1.0
[root@host ~]# mv ./prerm ./tomcat-myweb-1.0
[root@host ~]#tar –zcvf tomcat-myweb-1.0.tar.gz ./tomcat-myweb-1.0
[root@host ~]#mv tomcat-myweb-1.0.tar.gz ./rpmbuild/SOURCES
将tomcat-myweb-1.0.spec文件移到rpmbuild的SPECS目录下
[root@host ~]# mv ./tomcat-myweb-1.0.spec ./rpmbuild/SPECS
4 运行rpmbuild命令,完成RPM包的制作
[root@host~]#rpmbuild –ba ./rpmbuild/SPECS/tomcat-myweb-1.0.spec
5 如果成功,则rpm包会存储在/root/rpmbuild/RPMS 子目录内
注意:
1 如果想在运行安装rpm时,即(rpm-ivh xxxxxxxxxxx.rpm)与用户进行提示交互,必须要在.spec文件的 ’ %post ‘ 阶段调用shell脚本:
2 如果想在运行删除 rpm 时,即( rpm -e xxxxxxxxxxx )与用户进行提示交互 , 必须要在 .spec 文件的 ’ %preun‘ 阶段调用 shell 脚本。