rpmbuild使用方法简要

2016-12-29 11:05:13
Install rpm-build Package
  • rpm-build package provides rpmbuild command which is used to build a binary RPM from source code, as configured with .spec file.
  • rpmdevtools package provides rpmdev-setuptree and rpmdev-newspec commands.
  • rpmdev-setuptree command is used to creates rpmbuild/ subdirectory, with appropriate subdirectories.
  • rpmdev-newspec command is used to creates a new package spec file in the local directory

    1
    # yum install rpm-build rpmdevtools

    rpm-build is dependent on the following package

    1
    2
    3
    4
    elfutils-libelf
    rpm
    rpm-libs
    rpm-python
Create RPM Build User

We do not want to build RPMs as the root account and maybe not even our personal account. It is common to have some form of rpm build account, maybe even, innovatively called rpmbuild. Logging as that user to build RPM files

1
2
# useradd -m build
# passwd build

RPM Build Directories

rpm-build will automatically create the following directory structures that will be used during the RPM build.

1
2
3
4
5
6
7
8
# rpmdev-setuptree
# ls -lF /root/rpmbuild/
drwxr-xr-x. 2 root root 4096 Feb 4 12:21 BUILD/
drwxr-xr-x. 2 root root 4096 Feb 4 12:21 BUILDROOT/
drwxr-xr-x. 2 root root 4096 Feb 4 12:21 RPMS/
drwxr-xr-x. 2 root root 4096 Feb 4 12:21 SOURCES/
drwxr-xr-x. 2 root root 4096 Feb 4 12:21 SPECS/
drwxr-xr-x. 2 root root 4096 Feb 4 12:21 SRPMS/

Download Source Tar File

download the source tar file for the package that you want to build and save it under SOURCES directory

1
# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.21.tar.gz
Create the SPEC File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Name:
Version:
Release: 1%{?dist}
Summary:
Group:
License:
URL:
Source0:
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

BuildRequires:
Requires:

%description

%prep
%setup -q

%build
%configure
make %{?_smp_mflags}

%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot}

%clean
rm -rf %{buildroot}

%files
%defattr(-,root,root,-)
%doc

%changelog

Name: Set the name of RPM. Whatever you give here will become the name of RPM.
Version: Set RPM version number. Default is 1.0
Release :Set release afterward, default is 1.el6 for RHEL 6
Summary: describe the contents of the package
Group: Related to the package groups listed in the XML file.
License: RHEL 6 software is released under the GPL, but you can release your software under the license you prefer
URL: Allow you to set the URL of your site.
Above section specify the information required by package. When you run yum info [package name] or rpm -qi [package name ] command, you saw this information.
BuildRoot: directory that will be used to actually build the software
Source0 : specifies the name of the gzip compressed tar archive file. If you have multiple source you can specify them in series Source0, Source1, Source2 etc. For single source you can also use Source phrase.
BuildRequires or Requires specifies the packages that must be installed before the RPM packages can be built with this spec file.
%description allows you to include a brief paragraph description of the package.
%prep commands require to prepare the source code
%setup move the SOURCE into the BUILD directory and decompress the file archive.
The –n option specifies the name of the directory where installation script should be entered after decompression.
%install section is the place where software is compiled and installed.
%files section allow you to include any additional file that you want in package. You can also specify the directory here, it will includes all files and subdirectories below it as well.
%clean section will clean up the mess created during the process.

Let look mysql5.6 program spec file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Name: mysql-rpm
Version:5.6.21
Release: renfengjun
License: GPL
URL: http://dev.mysql.com/
Group: applications/database
BuildRoot:%{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: cmake
prefix: /usr/local/mysql
Summary: mysql-5.6.21.tar.gz

%description
The MySQL(TM) software delivers a very fast,multi-threaded, multi-user,
and robust SQL (Structured Query Language)database server. MySQL Server
is intended for mission-critical, heavy-loadproduction systems as well
as for embedding into mass-deployed software.

%define MYSQL_USER mysql
%define MYSQL_GROUP mysql
%define __os_install_post %{nil}

%build
cd $OLDPWD/../
CFLAGS="-O3 -g -fno-exceptions-static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
CXX=g++
CXXFLAGS="-O3 -g -fno-exceptions -fno-rtti-static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
export CFLAGS CXX CXXFLAGS

cmake . \
-DSYSCONFDIR:PATH=%{prefix} \
-DCMAKE_INSTALL_PREFIX:PATH=%{prefix} \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DENABLE_PROFILING:BOOL=ON \
-DWITH_DEBUG:BOOL=OFF \
-DWITH_VALGRIND:BOOL=OFF \
-DENABLE_DEBUG_SYNC:BOOL=OFF \
-DWITH_EXTRA_CHARSETS:STRING=all \
-DWITH_SSL:STRING=bundled \
-DWITH_UNIT_TESTS:BOOL=OFF \
-DWITH_ZLIB:STRING=bundled \
-DWITH_PARTITION_STORAGE_ENGINE:BOOL=ON \
-DWITH_INNOBASE_STORAGE_ENGINE:BOOL=ON \
-DWITH_ARCHIVE_STORAGE_ENGINE:BOOL=ON \
-DWITH_BLACKHOLE_STORAGE_ENGINE:BOOL=ON \
-DWITH_PERFSCHEMA_STORAGE_ENGINE:BOOL=ON \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE:BOOL=ON \
-DWITH_EMBEDDED_SERVER=0 \
-DINSTALL_LAYOUT:STRING=STANDALONE \
-DCOMMUNITY_BUILD:BOOL=ON \
-DMYSQL_SERVER_SUFFIX='-r5436';

make -j `cat /proc/cpuinfo | grep processor| wc-l`

%install
cd $OLDPWD/../
make DESTDIR=$RPM_BUILD_ROOT install

%clean
rm -rf $RPM_BUILD_ROOT

%pre
mkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/run
mkdir -p /usr/local/mysql/log
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql/log
chown -R mysql:mysql /usr/local/mysql/run
echo "exportPATH=$PATH:/usr/local/mysql/bin" >> /home/mysql/.bash_profile

%post
ln -s %{prefix}/lib %{prefix}/lib64
cp /usr/local/mysql/support-files/mysql.server/etc/init.d/mysql
chkconfig mysql on

%preun
chkconfig --del mysql
rm -rf /usr/local
userdel mysql
rm -rf /var/spool/mail/mysql
rm -rf /etc/init.d/mysql
rm -rf /home/mysql

%files
%defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP})
%attr(755, %{MYSQL_USER}, %{MYSQL_GROUP})%{prefix}/*

%changelog

Now,we can start build

1
rpmbuild -ba mysql.spec

Packaging:RPMMacros
  • Macros mimicking autoconf variables

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    %{_sysconfdir}        /etc
    %{_prefix} /usr
    %{_exec_prefix} %{_prefix}
    %{_bindir} %{_exec_prefix}/bin
    %{_libdir} %{_exec_prefix}/%{_lib}
    %{_libexecdir} %{_exec_prefix}/libexec
    %{_sbindir} %{_exec_prefix}/sbin
    %{_sharedstatedir} /var/lib
    %{_datarootdir} %{_prefix}/share
    %{_datadir} %{_datarootdir}
    %{_includedir} %{_prefix}/include
    %{_infodir} /usr/share/info
    %{_mandir} /usr/share/man
    %{_localstatedir} /var
    %{_initddir} %{_sysconfdir}/rc.d/init.d
  • Other macros and variables for paths

    1
    2
    3
    4
    5
    6
    7
    8
    %{_var}               /var
    %{_tmppath} %{_var}/tmp
    %{_usr} /usr
    %{_usrsrc} %{_usr}/src
    %{_lib} lib (lib64 on 64bit multilib systems)
    %{_docdir} %{_datadir}/doc
    %{buildroot} %{_buildrootdir}/%{name}-%{version}-%{release}.%{_arch}
    $RPM_BUILD_ROOT %{buildroot}
  • Build flags macros and variables
    These macros should be used as flags for the compiler or linker. Note that the values for the macros below reflect the settings on Fedora 13 (i686) with redhat-rpm-config installed.

    1
    2
    3
    %{__global_cflags}   -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4
    %{optflags} %{__global_cflags} -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables
    $RPM_OPT_FLAGS %{optflags}
  • RPM directory macros

    1
    2
    3
    4
    5
    6
    7
    %{_topdir}            %{getenv:HOME}/rpmbuild
    %{_builddir} %{_topdir}/BUILD
    %{_rpmdir} %{_topdir}/RPMS
    %{_sourcedir} %{_topdir}/SOURCES
    %{_specdir} %{_topdir}/SPECS
    %{_srcrpmdir} %{_topdir}/SRPMS
    %{_buildrootdir} %{_topdir}/BUILDROOT

Packaging:RPMMacros
How to create an RPM package/zh-cn


您的鼓励是我写作最大的动力

俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。