RHEL制作RPM的几个关键地方

在RHEL,CentOS,Fedora人人都会yum install或者rpm -ivh来进行软件的安装,但是老是安装拥有别人署名,Licence以及其他资料的RPM总感觉不太过瘾,比如rpm -qi kernel看到的肯定就是操作系统厂商的信息了,如何自己制作一个拥有自己署名的独立软件呢,下面就会给你答案,因为最开始一窍不通的时候,也是参考了网上的各种说法,所以自己的做法很多可能不是太专业,仅仅个人习惯

1:准备source code

主要针对C程序编译,假如是脚本语言安装,以及web比如copy一些前端后端脚本文件,也可以自行写一个Makefile或者shell脚本

mkdir -p hello-5.2.3/main

mv hello.c Makefile main

hello.c : 功能就是打印hello world!

Makefile: 负责编译hello.c生成可执行程序hello,使得运行./hello就会打印hello world!

 

2:准备源代码.tar.gz包,个人爱好增添一个重写的Makefile

cd hello-5.2.3/

vim Makefile

第一个关键地方,Makefile的install部分(类似格式)需添加红色rpm目录(注意格式要用tab键):

prefix = /usr/local

all:
cd main; $(MAKE) $(AM_MAKEFLAGS) $@

.PHONY:install

install:
mkdir -p $(RPM_BUILD_ROOT)$(prefix)/bin
install -m 755 main/a.out $(RPM_BUILD_ROOT)$(prefix)/bin

clean:
cd main; $(MAKE) $(AM_MAKEFLAGS) $@

显然这个Makefile的make会执行main目录下的Makefile,make install会将main下可执行程序copy到/usr/local/bin,红色部分非常关键,是制作rpm的中间路径,最终copy到系统目录会在这个路径下寻找

tar zcvf hello-5.2.3.tar.gz hello-5.2.3

cp hello-5.2.3.tar.gz /root/rpmbuild/SOURCES

 

3:准备spec文件,这是另外一个关键,配置rpm制作的方法和路径等

mkdir -p /usr/src/redhat/SPECS/

vim /usr/src/redhat/SPECS/hello.spec

需要做如下配置,只写几个关键地方,其它的含义可以上网搜搜,到处都有介绍:

Name: hello

Version:5.2.3

Source0: hello-5.2.3.tar.gz

%build
make

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{binpath}
make install RPM_INSTALL_ROOT=%{buildroot}

%files
/usr/local/bin

%changelog
* Fri Apr 23 2014 lihui <www.lihuia.com>  5.2.3

红色重点的地方应该一看就明白啥意思,最后一个可是你的版权哦,辛苦了大半天忘了写的话可别埋怨别人哦^_^

修改完保存

 

4:准备制作

假如你的机器还没有安装rpmbuild,没关系,yum install rpm-build

在这里申明下,一般来说,制作rpm尽量non-root,原因可以google一下方法以及优点,但是本人由于一直都是root操作(这也是自己很少几个不用sudo的地方)

rpm -bb /usr/src/redhat/SPECS/hello.spec

假如没有报错,说明制作成功,恭喜你,取rpm吧,/root/rpmbuild/RPMS/x86_64/hello-5.2.3-1.el6.x86_64.rpm

这样,拥有自主知识产权的rpm就制作成功了

 

5:安装以及检查自己的产权

# cp /root/rpmbuild/RPMS/x86_64/hello-5.2.3-1.el6.x86_64.rpm ./

# rpm -ivh hello-5.2.3-1.el6.x86_64.rpm

Preparing… ########################################### [100%]
1:hello ########################################### [100%]

# which a.out
/usr/local/bin/a.out

# /usr/local/bin/a.out
hello world!

可见rpm安装完全正确,生成的可执行程序也运行正确

最后就是咱们的产权:

# rpm -qi hello
Name : hello      Relocations: (not relocatable)
Version : 5.2.3 Vendor: (none)
Release : 1.el6   Build Date: Fri 23 May 2014 09:19:55 PM CST
Install Date: Fri 23 May 2014 09:20:12 PM CST Build Host: 2014
Group : lihuia.com Source RPM: hello-5.2.3-1.el6.src.rpm
Size : 4224 License: GPL
Signature : (none)
URL : http://lihuia.com
Summary : print hello world!
Description : hello world

大概自己的信息该有尽有,假如还不满足,自己查看spec文件一一添加

由于上面这个例子是随手敲的,所以有些Makefile以及文件都不太规范,不过能看明白就好,下面附几个文件内容,格式看懂就行:

 

[lihui@2014 ~]$ ll hello-5.2.3
total 8
drwxrwxr-x. 2 lihui lihui 4096 May 23 21:09 main
-rw-rw-r–. 1 lihui lihui 267 May 23 21:06 Makefile
[lihui@2014 ~]$ ll hello-5.2.3/main/
total 8
-rw-rw-r–. 1 lihui lihui 72 May 23 19:42 hello.c
-rw-rw-r–. 1 lihui lihui 57 May 23 21:09 Makefile

 

[lihui@2014 ~]$ cat hello-5.2.3/main/hello.c
#include <stdio.h>

int main(){
printf(“hello world!\n”);
return 0;
}

 

[lihui@2014 ~]$ cat hello-5.2.3/main/Makefile
static: hello.o
cc hello.c
clean:
rm -rf a.out hello.o
[lihui@2014 ~]$ cat hello-5.2.3/Makefile
prefix = /usr/local
ROOT_DIR := ../..
all: static
static:
cd main; $(MAKE) $(AM_MAKEFLAGS) $@

.PHONY:install

install:
mkdir -p $(RPM_BUILD_ROOT)$(prefix)/bin
install -m 755 main/a.out $(RPM_BUILD_ROOT)$(prefix)/bin

 

[lihui@2014 ~]$ cat /usr/src/redhat/SPECS/hello.spec
Name: hello
Version: 5.2.3
Release: 1%{?dist}
Summary: print hello world!

Group: lihuia.com
License: GPL
URL: http://lihuia.com
Source0: hello-5.2.3.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

Requires: gcc

%define binpath /usr/local/bin

%description
hello world

%prep
%setup -q
%build
make
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}/usr/local/bin
make install RPM_INSTALL_ROOT=%{buildroot}
%files
%doc
%{binpath}

%changelog
* Fri Apr 23 2014 lihui <www.lihuia.com> 5.2.3
– build the program

%define __debug_install_post %{_rpmconfigdir}/find-debuginfo.sh %{?_find_debuginfo_opts} “%{_builddir}/%{?buildsubdir}” %{nil}

clean:
cd main; $(MAKE) $(AM_MAKEFLAGS) $@

发表回复