Package source code into an rpm installation package under CentOS

Source: Internet
Author: User
Tags rpmbuild

Package source code into an rpm installation package under CentOS

Platform: centos6.5

First, build the environment:

[Cpp] view plaincopyprint?
  1. [Root @ localhostbinbin] # yuminstall-yrpm-build </span>
[Cpp] view plaincopyprint?
  1. [Root @ localhostbinbin] # yuminstallredhat-rpm-config

There will be an rpmbuild directory under the/root directory.

[Cpp] view plaincopyprint?
  1. [Root @ localhostrpmbuild] # pwd
  2. /Root/rpmbuild </span>

Go to the directory, which contains five folders: BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
It was empty at the beginning.
[Html] view plaincopyprint?
  1. [Root @ localhostrpmbuild] # ls
  2. BUILDBUILDROOTRPMSSOURCESSPECSSRPMS

If you do not have these directories, you can manually add and create


The functions of these six directories are as follows:
BUILD: rpmbuild directory for software compilation
BUILDROOT:
RPMS: rpmbuild creates the directory where binary RPM is stored
SOURCES: directory for storing source code
SPECS: directory for storing spec files
SRPMS: rpmbuild creates the directory where souce RPM is stored

First, package the folders with source code. Here my folder is named login-1.0
[Cpp] view plaincopyprint?
  1. Using root@localhostbinbin1_tarzcvflogin-1.0.tar.gz login-1.0
  2. [Root @ localhostbinbin] # ls
  3. Login-1.0.tar.gzlogin-1.0

Copy login-1.0.tar.gz to the rpmbuild/SOUCES directory.
[Html] view plaincopyprint?
  1. Export root@localhostbinbin]#cplogin-1.0.tar.gz/root/rpmbuild/SOURCES
  2. [Root @ localhostSOURCES] # ls
  3. Login-1.0.tar.gz


Go to the SPEC directory and write the SPEC file (this step is the key step to package the rpm package)
[Html] view plaincopyprint?
  1. [Root @ localhostSOURCES] # cd ../SPECS
  2. [Root @ localhostSPECS] # touchlogin. spec
  3. [Root @ localhostSPECS] # ls
  4. Login. spec
  5. [Root @ localhostSPECS] # vimlogin. spec

[Html] view plaincopyprint?
  1. # Name
  2. Name: login
  3. # Version number
  4. Version: 1.0
  5. # Compilation order
  6. Release: 1
  7. # One-sentence introduction package (up to 50 characters)
  8. Summary: istest
  9. Group: Applications/Communications
  10. License: GPL
  11. URL: www.ciis.com
  12. # Source file of the compressed package to be installed
  13. Source: login-1.0.tar.gz
  14. BuildRoot: % {_ tmppath}/% {name}-% {version}-root
  15. % Description
  16. Atest
  17. # Pre-build handling
  18. % Prep
  19. # -- Decompress: extract the source code to the system directory during building. Here, $ RPM_BUILD_DIR is used.
  20. # During setup-c decompression, a new directory with the same name as the compressed package will be generated; setup-ndirname specifies a new directory to decompress setup-q and directly decompress it without creating a new directory.
  21. % Setup-q
  22. # -- Generate: This mainly refers to the compilation and generation of binary files when building Binary packages.
  23. % Build
  24. Make
  25. # -- Installation: Install the current file to the system directory $ RPM_BUILD_ROOT/During build, and install the file in the/root directory during binary installation.
  26. % Install
  27. # Makeinstall # use makefile's install for convenience
  28. # No makefile, custom installation script
  29. Rm-rf $ RPM_BUILD_ROOT /*
  30. Mkdir-p $ RPM_BUILD_ROOT/usr/bin/
  31. Cp-r$ RPM_BUILD_DIR/% {name}-% {version} $ RPM_BUILD_ROOT/usr/bin/
  32. # Subsequent installation: processing script after Binary Package Installation
  33. % Post
  34. Chmod + x/usr/bin/% {name}-% {version}/confAgent
  35. Chmod + x/usr/bin/% {name}-% {version}/check_confAgent.sh
  36. # Subsequent processing scripts after uninstallation: processing scripts after uninstallation of Binary packages
  37. % Postun
  38. Rm-fr/usr/bin/% {name}-% {version}
  39. # Cleanup: After the build package is completed
  40. % Clean
  41. Rm-rf $ RPM_BUILD_ROOT
  42. # File list: indicates which files should be installed in the system and the installed directory;
  43. # When creating a package, the file or directory in the file list must be consistent with the file or directory in the virtual directory RPM_BUILD_ROOT; otherwise
  44. # The system prompts that the file cannot be found.
  45. % Files
  46. % Defattr (-, root, root)
  47. /Usr/bin/% {name}-% {version}
  48. % Changelog


Save and exit. Run the packaging command.
[Html] view plaincopyprint?
  1. [Root @ localhostSPECS] # rpmbuild-balogin.spec </span>

There will be a corresponding rpm package under the RPMS/i686 directory, here is the login-1.0-1.i686.rpm
[Html] view plaincopyprint?
  1. [Root @ localhostSPECS] # cd ../RPMS/i686
  2. [Root @ localhosti686] # pwd
  3. /Root/rpmbuild/RPMS/i686
  4. [Root @ localhosti686] # ls
  5. Login-1.0-1.i686.rpm


Test installation
[Html] view plaincopyprint?
  1. [Root @ localhostbinbin] # ls
  2. Login-1.0-1.i686.rpm
  3. [Root @ localhostbinbin] # rpm-ivhlogin-1.0-1.i686.rpm
  4. [Root @ localhostbinbin] #/usr/bin/login-1.0/demo
You can run the client.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.