您好,登錄后才能下訂單哦!
使用fpm來制作rpm包
2017/2/22
一、配置fpm環境 1、安裝ruby和fpm 使用rvm來管理ruby curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - curl -L https://get.rvm.io | bash -s stable --ruby 重新打開一個shell # ruby -v ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux] 安裝fpm依賴包 yum install rpm-build -y 安裝fpm [root@tvm-rpm ~]# gem install fpm -V 2、fpm的參數 參考:https://github.com/jordansissel/fpm/wiki % fpm -s <source type> -t <target type> [options] -s 源格式 -t 目標格式 -n 包名 -v version值,實際版本號 --iteration release值,發布序列號 --epoch epoch值 --vendor 廠商 --maintainer 維護者 --description 描述 --url 軟件主頁 --workdir fpm工作目錄 -d 依賴的軟件包 --directories 遞歸指定的目錄標記為屬于這個包 -C 切換到指定的目錄 -p 輸出到指定的路徑 --force 強制覆蓋文件 --after-install FILE 包安裝后執行的腳本 --before-install FILE 包安裝前執行的腳本 --after-remove FILE 包移除后執行的腳本 --before-remove FILE 包移除前執行的腳本 --after-upgrade FILE 包升級后執行的腳本 --before-upgrade FILE 包升級前執行的腳本 -e building前編輯spec文件 二、示例 【實例1:將python3的源碼打包成rpm來安裝】 1、配置編譯python環境所需的包 [root@tvm-rpm ~]# mkdir /data/{download,rpms} 使用局域網的epel源: [root@tvm-rpm ~]# mv /etc/yum.repos.d/*.repo /tmp/ \ && wget http://mirrors.office.test/local-office.repo -O /etc/yum.repos.d/local-office.repo \ && yum clean all \ && yum makecache 注:如果要使用公網的epel源,可以這樣操作: # rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm [root@tvm-rpm ~]# yum -y groupinstall "Development tools" [root@tvm-rpm ~]# yum -y install openssl-devel readline-devel bzip2-devel sqlite-devel zlib-devel ncurses-devel db4-devel expat-devel 下載最新的python源碼包: [root@tvm-rpm ~]# wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz -O /data/download/Python-3.4.3.tgz [root@tvm-rpm ~]# cd /data/download/ [root@tvm-rpm download]# tar xf Python-3.4.3.tgz [root@tvm-rpm download]# cd Python-3.4.3 [root@tvm-rpm Python-3.4.3]# [root@tvm-rpm Python-3.4.3]# export DIR_PY_SRC_INSTALL=/data/rpms/Python-3.4.3 [root@tvm-rpm Python-3.4.3]# export DIR_PY_RPM_INSTALL=/usr/local [root@tvm-rpm Python-3.4.3]# export LDFLAGS="-Wl,-rpath=${DIR_PY_RPM_INSTALL}/lib ${LDFLAGS}" [root@tvm-rpm Python-3.4.3]# ./configure --prefix=${DIR_PY_RPM_INSTALL} [root@tvm-rpm Python-3.4.3]# make [root@tvm-rpm Python-3.4.3]# make install DESTDIR=${DIR_PY_SRC_INSTALL} [root@tvm-rpm Python-3.4.3]# cd .. 我們來看一下,生成了那些目錄和文件: [root@tvm-rpm Python-3.4.3]# cd /data/rpms [root@tvm-rpm Python-3.4.3]# ls Python-3.4.3/usr/local/ bin include lib share 2、開始打包 [root@tvm-rpm Python-3.4.3]# fpm -s dir -t rpm \ -n python3 \ -v '3.4.3' \ --iteration '1.el6' \ --epoch '0' \ --vendor ' Python Software Foundation' \ --maintainer 'PC' \ --description 'use fpm to make a pkg for python-3.4.3' \ --url 'https://www.python.org/downloads/release/python-343/' \ --workdir /data/rpms \ -p /data/rpms/pkgs/ \ -C ${DIR_PY_SRC_INSTALL} \ --directories=${DIR_PY_RPM_INSTALL}/lib \ --directories=${DIR_PY_RPM_INSTALL}/include \ -d 'openssl' \ -d 'bzip2' \ -d 'zlib' \ -d 'expat' \ -d 'db4' \ -d 'sqlite' \ -d 'ncurses' \ -d 'readline' Created package {:path=>"/data/rpms/pkgs/python3-3.4.3-1.el6.x86_64.rpm"} 我們來看看包的內容: [root@tvm-rpm rpms]# rpm -qpi pkgs/python3-3.4.3-1.el6.x86_64.rpm Name : python3 Relocations: / Version : 3.4.3 Vendor: Python Software Foundation Release : 1.el6 Build Date: Wed 15 Jul 2015 04:55:17 PM CST Install Date: (not installed) Build Host: tvm-rpm Group : default Source RPM: python3-3.4.3-1.el6.src.rpm Size : 126285890 License: unknown Signature : (none) Packager : PC URL : https://www.python.org/downloads/release/python-343/ Summary : use fpm to make a pkg for python-3.4.3 Description : use fpm to make a pkg for python-3.4.3 3、安裝測試 1)安裝前: [root@tvm-rpm rpms]# ls /usr/local/{bin,include,lib,share/man} /usr/local/bin: /usr/local/include: /usr/local/lib: /usr/local/share/man/man1: 2)開始安裝: [root@tvm-rpm rpms]# rpm -ivh pkgs/python3-3.4.3-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:python3 ########################################### [100%] [root@tvm-rpm rpms]# rpm -qa |grep python3 python3-3.4.3-1.el6.x86_64 3)安裝后: [root@tvm-rpm rpms]# ls /usr/local/{bin,include,lib,share/man/man1} /usr/local/bin: 2to3 2to3-3.4 easy_install-3.4 idle3 idle3.4 pip3 pip3.4 pydoc3 pydoc3.4 python3 python3.4 python3.4-config python3.4m python3.4m-config python3-config pyvenv pyvenv-3.4 /usr/local/include: python3.4m /usr/local/lib: libpython3.4m.a pkgconfig python3.4 /usr/local/share/man/man1: python3.1 python3.4.1 執行python命令,查看版本: [root@tvm-rpm rpms]# python3 -c "import sys; print(sys.version)" 3.4.3 (default, Jul 15 2015, 14:40:59) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] 4)卸載: [root@tvm-rpm rpms]# rpm -e python3-3.4.3-1.el6.x86_64 [root@tvm-rpm rpms]# ls /usr/local/{bin,include,lib,share/man/man1} /usr/local/bin: /usr/local/include: /usr/local/lib: /usr/local/share/man/man1: 符合預期。 【實例2:打包yum源的配置文件為rpm包】 [root@tvm-rpm pkgs]# cd /data/rpms/pkgs/ [root@tvm-rpm pkgs]# fpm -s dir -t rpm \ -a 'x86_64' \ -n 'office-repo-latest' \ -v '6' \ --iteration '1.el6' \ --epoch '0' \ --vendor 'pc@office' \ --maintainer 'PC' \ --description 'provide file: [local-office.repo] for local users. include: centos-base, eple, user-define rpms' \ /etc/yum.repos.d/local-office.repo Created package {:path=>"office-repo-latest-6-1.el6.noarch.rpm"} [root@tvm-rpm pkgs]# rpm -qpl office-repo-latest-6-1.el6.x86_64.rpm /etc/yum.repos.d/local-office.repo [root@tvm-rpm pkgs]# rpm -qpi office-repo-latest-6-1.el6.x86_64.rpm Name : office-repo-latest Relocations: / Version : 6 Vendor: pc@office Release : 1.el6 Build Date: Thu 06 Aug 2015 04:53:26 PM CST Install Date: (not installed) Build Host: tvm-rpm Group : default Source RPM: office-repo-latest-6-1.el6.src.rpm Size : 2392 License: unknown Signature : (none) Packager : PC URL : http://example.com/no-uri-given Summary : provide file: [local-office.repo] for local users. include: centos-base, eple, user-define rpms Description : provide file: [local-office.repo] for local users. include: centos-base, eple, user-define rpms 測試1:將打包的文件拷貝到其他主機上 [root@tvm-test ~]# mv /etc/yum.repos.d/local-office.repo /tmp/ 安裝: [root@tvm-test ~]# rpm -ivh office-repo-latest-6-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:office-repo-latest ########################################### [100%] [root@tvm-test ~]# diff /etc/yum.repos.d/local-office.repo /tmp/local-office.repo 測試2:將打包的文件拷貝到本地的yum源來提供安裝。 [root@tvm-yum ~]# mv office-repo-latest-6-1.el6.x86_64.rpm /data/yum/repo/ 先移除之前測試1安裝的包: [root@tvm-test ~]# rpm -qa |grep office office-repo-latest-6-1.el6.x86_64 [root@tvm-test ~]# rpm -e office-repo-latest-6-1.el6.x86_64 安裝: [root@tvm-test ~]# rpm -ivh http://mirrors.office.test/office-repo-latest-6-1.el6.x86_64.rpm Retrieving http://mirrors.office.test/office-repo-latest-6-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:office-repo-latest ########################################### [100%] [root@tvm-test ~]# diff /etc/yum.repos.d/local-office.repo /tmp/local-office.repo 【實例3:打包最新版的monit】 1)從官網下載最新的binary [root@tvm-rpm rpms]# mkdir monit && cd monit [root@tvm-rpm monit]# wget https://mmonit.com/monit/dist/binary/5.14/monit-5.14-linux-x64.tar.gz [root@tvm-rpm monit]# tar zxvf monit-5.14-linux-x64.tar.gz monit-5.14/ monit-5.14/bin/ monit-5.14/bin/monit monit-5.14/COPYING monit-5.14/conf/ monit-5.14/conf/monitrc monit-5.14/man/ monit-5.14/man/man1/ monit-5.14/man/man1/monit.1 [root@tvm-rpm monit]# cd monit-5.14 [root@tvm-rpm monit-5.14]# ll bin/ conf/ man/man1/ bin/: total 2688 -rwxr-xr-x 1 root root 2752045 Jun 9 18:18 monit conf/: total 12 -rw------- 1 root root 11220 Jun 9 18:18 monitrc man/man1/: total 132 -rw-r--r-- 1 root root 131280 Jun 9 18:18 monit.1 拷貝到合適的位置: [root@tvm-rpm monit-5.14]# cp -a bin/monit /usr/bin/ [root@tvm-rpm monit-5.14]# cp -a conf/monitrc /etc/ [root@tvm-rpm monit-5.14]# cp -a man/man1/monit.1 /usr/share/man/man1/ 增加一個控制腳本: [root@tvm-rpm monit-5.14]# ll init.d/ total 4 -rwxr-xr-x 1 root root 1272 Jun 5 2014 monit [root@tvm-rpm monit-5.14]# cp -a init.d/monit /etc/init.d/ 創建目錄: [root@tvm-rpm monit-5.14]# mkdir /etc/monit.d 調整配置文件: [root@tvm-rpm monit-5.14]# grep ^[^#] /etc/monitrc set daemon 120 with start delay 240 set logfile /var/log/monit include /etc/monit.d/* 2)打包 [root@tvm-rpm monit-5.14]# cd /data/rpms/pkgs/ [root@tvm-rpm pkgs]# fpm -s dir -t rpm \ -a 'x86_64' \ -n 'monit' \ -v '5.14' \ --iteration '1.el6' \ --epoch '0' \ --vendor 'pc@office' \ --maintainer 'PC' \ --description 'monit-5.14 for local users' \ /usr/bin/monit \ /etc/monitrc \ /etc/monit.d \ /usr/share/man/man1/monit.1 \ /etc/init.d/monit Created package {:path=>"monit-5.14-1.el6.x86_64.rpm"} [root@tvm-rpm pkgs]# rpm -qpl monit-5.14-1.el6.x86_64.rpm /etc/init.d/monit /etc/monit.d /etc/monitrc /usr/bin/monit /usr/share/man/man1/monit.1 [root@tvm-rpm pkgs]# rpm -qpi monit-5.14-1.el6.x86_64.rpm Name : monit Relocations: / Version : 5.14 Vendor: pc@office Release : 1.el6 Build Date: Fri 28 Aug 2015 06:00:57 PM CST Install Date: (not installed) Build Host: tvm-rpm Group : default Source RPM: monit-5.14-1.el6.src.rpm Size : 2895915 License: unknown Signature : (none) Packager : PC URL : http://example.com/no-uri-given Summary : monit-5.14 for local users Description : monit-5.14 for local users 測試1:將打包的文件拷貝到其他主機上 [root@tvm-test ~]# rpm -Uvh monit-5.14-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:monit ########################################### [100%] [root@tvm-test ~]# service monit start Starting monit: Starting Monit 5.14 daemon Monit start delay set -- pause for 240s [ OK ] [root@tvm-test ~]# chkconfig monit on 卸載: [root@tvm-test ~]# rpm -e monit-5.14-1.el6.x86_64 [root@tvm-test ~]# ls /usr/bin/monit /etc/monitrc ls: cannot access /usr/bin/monit: No such file or directory ls: cannot access /etc/monitrc: No such file or directory 可以發現,已經被刪除。 測試2:更新到本地的yum源后安裝 [root@tvm-yum x86_64]# pwd /data/yum/repo/office/6/x86_64 上傳rpm包,重建repo: [root@tvm-yum x86_64]# createrepo . Spawning worker 0 with 30 pkgs Workers Finished Gathering worker results Saving Primary metadata Saving file lists metadata Saving other metadata Generating sqlite DBs Sqlite DBs complete 安裝: [root@tvm-test ~]# yum makecache [root@tvm-test ~]# yum install monit Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package monit.x86_64 0:5.14-1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================================== Package Arch Version Repository Size =============================================================================================================================== Installing: monit x86_64 5.14-1.el6 office 1.2 M Transaction Summary =============================================================================================================================== Install 1 Package(s) Total download size: 1.2 M Installed size: 2.8 M Is this ok [y/N]: y Downloading Packages: monit-5.14-1.el6.x86_64.rpm | 1.2 MB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : monit-5.14-1.el6.x86_64 1/1 Verifying : monit-5.14-1.el6.x86_64 1/1 Installed: monit.x86_64 0:5.14-1.el6 Complete! 【實例4:打包tengine】 1、準備 mkdir /opt/fpm/{src,rpms,install} -p export DIR_SRC_MAKE_INSTALL=/opt/fpm/install export DIR_RPM_INSTALL=/opt/tengine export DIR_RPMS=/opt/fpm/rpms export DIR_FPM_SCRIPTS=/opt/fpm/scripts 2、源碼編譯 cd /opt/fpm/src wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz yum install pcre pcre-devel -y tar zxvf tengine-2.1.2.tar.gz && cd tengine-2.1.2 ./configure --prefix=${DIR_RPM_INSTALL} ===================================================注意以下輸出,確認是否符合預期。 Configuration summary + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library + jemalloc library is disabled nginx path prefix: "/opt/tengine" nginx binary file: "/opt/tengine/sbin/nginx" nginx configuration prefix: "/opt/tengine/conf" nginx configuration file: "/opt/tengine/conf/nginx.conf" nginx pid file: "/opt/tengine/logs/nginx.pid" nginx error log file: "/opt/tengine/logs/error.log" nginx http access log file: "/opt/tengine/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx dso module path: "/opt/tengine/modules/" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" =================================================== make && make install DESTDIR=${DIR_SRC_MAKE_INSTALL} 3、自定義配置文件和腳本 [root@tvm-rpm tengine-2.1.2]# cd /opt/fpm 1)編輯腳本,用于rpm包安裝和卸載時執行指令: [root@tvm-rpm fpm]# mkdir scripts [root@tvm-rpm fpm]# cat scripts/after-install.sh #!/bin/bash # # 2017/2/22 # user echo -e '\n\033[1;34mCreate user nginx:\033[0m\n' id nginx >/dev/null 2>&1 && echo 'User [nginx] exist.' || (useradd -s /sbin/nologin -d /var/cache/nginx -c "nginx user" nginx;id nginx) echo -e '\n\033[1;34mConfiguration summary:\033[0m\n' cat <<'_EOF' =================================================== + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library + jemalloc library is disabled nginx path prefix: "/opt/tengine" nginx binary file: "/opt/tengine/sbin/nginx" nginx configuration prefix: "/opt/tengine/conf" nginx configuration file: "/opt/tengine/conf/nginx.conf" nginx pid file: "/opt/tengine/logs/nginx.pid" nginx error log file: "/opt/tengine/logs/error.log" nginx http access log file: "/opt/tengine/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx dso module path: "/opt/tengine/modules/" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" startup script: /etc/init.d/tengine =================================================== _EOF echo -e '\n\033[1;33mTengine has been successfully installed.\033[0m\n' [root@tvm-rpm fpm]# cat scripts/before-remove.sh #!/bin/bash # # 2017/2/22 echo -e '\n\033[1;33mIn order to backup you data, please follow the guide, control it by hand.\033[0m\n' # service echo -e '\n\033[1;34mService stopped.\033[0m\n' service tengine stop # user echo -e '\n\033[1;34m[Guide] Try to remove user nginx?\033[0m\n' echo 'userdel -fr nginx' # dir echo -e '\n\033[1;34mDirectory backuped: [/opt/tengine] -> [/opt/tengine.old]\033[0m\n' mv -fv /opt/tengine /opt/tengine.old 2)控制腳本 [root@tvm-rpm fpm]# mkdir install/etc/init.d [root@tvm-rpm fpm]# vim install/etc/init.d/tengine (略) [root@tvm-rpm fpm]# chmod +x install/etc/init.d/tengine 4、開始打包 [root@tvm-rpm fpm]# fpm -s dir -t rpm \ -n Tengine \ -v '2.1.2' \ --iteration '1.el6' \ --epoch '0' \ --vendor 'Tengine@taobao' \ --maintainer 'PC' \ --description 'Tengine is based on Nginx which stands for Engine-X.' \ --url 'tengine.taobao.org' \ -C ${DIR_SRC_MAKE_INSTALL} \ -p ${DIR_RPMS} \ -d 'pcre' \ --after-install ${DIR_FPM_SCRIPTS}/after-install.sh \ --before-remove ${DIR_FPM_SCRIPTS}/before-remove.sh \ -f Created package {:path=>"/opt/fpm/rpms/Tengine-2.1.2-1.el6.x86_64.rpm"} 5、查看rpm包內的文件: [root@tvm-rpm fpm]# rpm -qpl /opt/fpm/rpms/Tengine-2.1.2-1.el6.x86_64.rpm [root@tvm-rpm fpm]# tree -L 2 . ├── install -> fpm打包時,將chroot到這個目錄,對應安裝后的文件系統中以下路徑:/opt, /etc │ ├── etc -> 在nginx的安裝包中拷貝/etc/init.d/nginx,改一下配置文件中的相關路徑為tengine的即可,然后將文件拷貝到這里(/etc/init.d/tengine)即可。 │ └── opt -> 源碼安裝后的目錄 ├── rpms │ └── Tengine-2.1.2-1.el6.x86_64.rpm -> rpm包保存在這里 ├── scripts │ ├── after-install.sh -> 安裝rpm包后執行這個腳本 │ └── before-remove.sh -> 卸載rpm包前執行這個腳本 └── src ├── tengine-2.1.2 -> 源碼編譯的工作目錄 └── tengine-2.1.2.tar.gz -> 源碼 7 directories, 4 files 6、拷貝rpm包到一臺新的機器上測試: 符合預期。 ZYXW、參考 1、fpm/wiki https://github.com/jordansissel/fpm/wiki 2、使用 FPM 創建 Python 的 RPM 包 http://theo.im/blog/2014/05/16/use-fpm-to-create-python-rpm-packages/ 3、使用FPM快速生成RPM包 https://linux.cn/article-3184-1.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。