• 幕客老师召集小伙伴
  • 运维高手36项修炼
  • python自动化运维项目实战
  • nginx从入门到实战
  • 阿里云与Centos7实战

如何通过SPEC编译RPM包(例:redis的rpm包)

 为了方便我们进行系统的软件包管理和相关的规范化部署,我们公司通常会建议大家使用rpm包作为部署。但是在我们的Yum源里没有对应的rpm包,或者我们对部署有着特定的要求该如何作呢?可以参考如下:

 

一、准备工作

要编译一个RPM包,我们需要获取到对应的源码包和SPEC文件(此文件类似于一个脚本命令文件,rpmbuild会根据SPEC的描述进行configure\make\make install)。
 

打RPM包前需要,安装编译rpm所需要的工具:

 

yum install rpm-build -y

 

准备的包情况如下:

 

redis-2.8.13.tar.gz (为最新稳定版本)
 
在系统环境下生成对应的工作区目录如下:
20:35 root@Operation.Machine#ls /usr/src/redhat/ 
BUILD RPMS SOURCES SPECS SRPMS

 

 

 

二、操作,并且生成对应的SPEC文件

 

2.1 将redis-2.8.13.tar.gz 放入源码目录SOURCES 

 

21:27 root@Operation.Machine#cp redis-2.8.13.tar.gz /usr/src/redhat/SOURCES/

 

SPECS目录为对应的spec文件存放的目录,我们通常获取SPEC文件的方法有如下三种:

方法1:通过src.rpm包获取。

#rpm -ivh redis-2.8.13-3.el7.src.rpm
#–nomd5去跳过验证该包中文件的MD5值

方法2:新建spec文件。

#需要熟悉SPEC熟悉语言
方法3:从源码包中获取已有spec文件。

#一般的源码都会随压缩包一起发布,在解压完压缩包后就可以看到。

 

三、编译构建

最后,到SPEC工作区目录执行如下命令:

 

21:11 root@Operation.Machine#rpmbuild -ba redis-2.8.13.spec21:11 root@Operation.Machine#rpmbuild -bb redis-2.8.13.spec
注:ba将产生srpmrpm. bb的参数只产生rpm包。

 

附:redis-2.8.12.spec 编译的logrotate和reids配置的性能参数我都已经调优完毕,大家可以参考这个配置。

 

%define pid_dir %{_localstatedir}/run/redis 
%define redis_dir /opt/logs/redis 
%define pid_file %{pid_dir}/redis.pid 

Summary: redis 
Name: redis 
Version:2.8.13Release: rc2 
License: BSD 
Group:Applications/Multimedia 
URL: http://code.google.com/p/redis/ Source0: redis-%{version}.tar.gz 
#Source0: redis-%{version}-%{release}.tar.gz 
Source1: redis.conf 

BuildRoot:%{_tmppath}/%{name}-%{version}-%{release}-root 
BuildRequires: gcc, make 
Requires(post):/sbin/chkconfig /usr/sbin/useradd 
Requires(preun):/sbin/chkconfig,/sbin/service 
Requires(postun):/sbin/service 
Provides: redis 

Packager:Jeson<chenjie@youku.com>%description 
Redis is a key-value database.It is similar to memcached but the dataset is 
not volatile, and values can be strings, exactly like in memcached, but also 
lists and sets with atomic operations to push/pop elements.In order to be very fast but at the same time persistent the whole dataset is 
taken in memory and from time to time and/or when a number of changes to the 
dataset are performed it is written asynchronously on disk.You may lose the 
last few queries that is acceptable in many applications but it is as fast 
as an in memory DB (beta 6 of Redis includes initial support for master-slave 
replication in order to solve this problem by redundancy).Compression and other interesting features are a work in progress.Redis is 
written in ANSI C and works in most POSIX systems like Linux,*BSD,Mac OS X, 
and so on.Redis is free software released under the very liberal BSD license.%prep 
%setup 

%{__cat}<<EOF >redis.logrotate 
%{redis_dir}/*log { 
nocompress 
copytruncate 
missingok 
daily 
dateext 
rotate 20 
create 
} 
EOF 

%{__cat} <<'EOF' >redis.sysv 
#!/bin/bash 
# 
# Init file for redis 
# 
# Written by Jason Priebe <jpriebe@cbcnewmedia.com> 
# 
# chkconfig: - 80 12 
# description: A persistent key-value database with network interface 
# processname: redis-server 
# config: /etc/redis.conf 
# pidfile: %{pidfile} 

source %{_sysconfdir}/init.d/functions 

RETVAL=0 
prog="redis-server" 

start() { 
echo -n $"Starting $prog: " 
daemon --user redis --pidfile %{pid_file} %{_sbindir}/$prog /etc/redis.conf 
RETVAL=$? 
echo 
[ $RETVAL -eq 0 ] && touch %{_localstatedir}/lock/subsys/$prog 
return $RETVAL 
} 

stop() { 
PID=`cat %{pid_file} 2>/dev/null` 
if [ -n "$PID" ]; then 
echo "Shutdown may take a while; redis needs to save the entire database"; 
echo -n $"Shutting down $prog: " 
/usr/bin/redis-cli shutdown 
if checkpid $PID 2>&1; then 
echo_failure 
RETVAL=1 
else 
rm -f /var/lib/redis/temp*rdb 
rm -f /var/lock/subsys/$prog 
echo_success 
RETVAL=0 
fi 
else 
echo -n $"$prog is not running" 
echo_failure 
RETVAL=1 
fi 

echo 
return $RETVAL 
} 

restart() { 
stop 
start 
} 

condrestart() { 
[-e /var/lock/subsys/$prog] && restart || : 
} 

case "$1" in 
start) 
start 
;; 
stop) 
stop 
;; 
status) 
status -p %{pid_file} $prog 
RETVAL=$? 
;; 
restart) 
restart 
;; 
condrestart|try-restart) 
condrestart 
;; 
*) 
echo $"Usage: $0 {start|stop|status|restart|condrestart}" 
RETVAL=1 
esac 

exit $RETVAL 
EOF 


%build 
%{__make} 

%install 
%{__rm} -rf %{buildroot} 
mkdir -p %{buildroot}%{_bindir} 

%{__install} -Dp -m 0755 src/redis-server %{buildroot}%{_sbindir}/redis-server 
%{__install} -Dp -m 0755 src/redis-benchmark %{buildroot}%{_bindir}/redis-benchmark 
%{__install} -Dp -m 0755 src/redis-cli %{buildroot}%{_bindir}/redis-cli 

%{__install} -Dp -m 0755 redis.logrotate %{buildroot}%{_sysconfdir}/logrotate.d/redis 
%{__install} -Dp -m 0755 redis.sysv %{buildroot}%{_sysconfdir}/init.d/redis 
%{__install} -Dp -m 0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/redis.conf 
%{__install} -p -d -m 0755 %{buildroot}%{_localstatedir}/lib/redis 
%{__install} -p -d -m 0755 %{buildroot}%{_localstatedir}/log/redis 
%{__install} -p -d -m 0755 %{buildroot}%{pid_dir} 

%pre 
/usr/sbin/useradd -c 'Redis' -u 499 -s /bin/false -r -d %{_localstatedir}/lib/redis redis 2> /dev/null || : 
mkdir -p %{redis_dir} 
chown redis.redis %{redis_dir} -R 

%preun 
if [ $1 = 0 ]; then 
# make sure redis service is not running before uninstalling 

# when the preun section is run, we've got stdin attached. If we 
# call stop() in the redis init script, it will pass stdin along to 
# the redis-cli script; this will cause redis-cli to read an extraneous 
# argument, and the redis-cli shutdown will fail due to the wrong number 
# of arguments. So we do this little bit of magic to reconnect stdin 
# to the terminal 
term="/dev/$(ps -p$$ --no-heading | awk '{print $2}')" 
exec < $term 

/sbin/service redis stop > /dev/null 2>&1 || : 
/sbin/chkconfig --del redis 
fi 

%post 
/sbin/chkconfig --add redis 

%clean 
%{__rm} -rf %{buildroot} 

%files 
%defattr(-, root, root, 0755) 
#%doc doc/*.html 
%{_sbindir}/redis-server 
%{_bindir}/redis-benchmark 
%{_bindir}/redis-cli 
%{_sysconfdir}/init.d/redis 
%config(noreplace) %{_sysconfdir}/redis.conf 
%{_sysconfdir}/logrotate.d/redis 
%dir %attr(0770,redis,redis) %{_localstatedir}/lib/redis 
%dir %attr(0755,redis,redis) %{_localstatedir}/log/redis 
%dir %attr(0755,redis,redis) %{_localstatedir}/run/redis 

%changelog

如何通过SPEC编译RPM包(例:redis的rpm包)

Pingbacks已打开。

引用地址

暂无评论

发表评论