Linux 软件包制作指南
Linux 软件包制作指南
Section titled “Linux 软件包制作指南”制作 DEB(Debian/Ubuntu)和 RPM(RedHat/CentOS)软件包的完整模板。
- 支持 X86、ARM 两种 CPU 架构
- 安装时自动注册 systemd 服务
- 卸载时自动停止并删除服务
以一个简单的 Go HTTP 服务为例:
package main
import ( "net/http" "github.com/gin-gonic/gin")
func main() { r := gin.Default() r.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "hello world"}) }) r.Run("0.0.0.0:8888")}systemd 服务文件 deb-demo.service:
[Unit]Description=deb-demoAfter=network.target
[Service]Type=simpleUser=rootExecStart=/opt/deb-demo/bin/deb-demoRestart=on-abort
[Install]WantedBy=default.targetDEB 包制作(Debian/Ubuntu)
Section titled “DEB 包制作(Debian/Ubuntu)”debian/├── changelog # 更新日志├── compat # debhelper 兼容版本├── control # 包元信息├── postinst # 安装后脚本(755权限)├── prerm # 卸载前脚本(755权限)└── rules # 构建规则(755权限)changelog
Section titled “changelog”deb-demo (0.1.0) UNRELEASED; urgency=medium
* Initial release. (Closes: #XXXXXX)
-- root <[email protected]> Tue, 09 Jul 2024 11:18:20 +0800compat
Section titled “compat”9control
Section titled “control”Source: deb-demoSection: utilsPriority: optionalMaintainer: Your Name <[email protected]>Build-Depends: debhelper (>= 9), systemdStandards-Version: 4.5.0
Package: deb-demoDepends: ${misc:Depends}, ${shlibs:Depends}, systemdDescription: deb-demo is demo for deb package.Architecture: amd64 arm64#!/usr/bin/make -f
%: dh $@
override_dh_strip: echo "Skipping dh_strip"
override_dh_shlibdeps: echo "Skipping dh_shlibdeps"
override_dh_auto_build:ifeq ($(DEB_HOST_ARCH),amd64) GOOS=linux GOARCH=amd64 makeelse GOOS=linux GOARCH=arm64 makeendif
override_dh_auto_install: dh_auto_install mkdir -pv debian/deb-demo/opt/deb-demo/bin cp -v deb-demo debian/deb-demo/opt/deb-demo/bin/deb-demo mkdir -pv debian/deb-demo/etc/systemd/system cp -v deb-demo.service debian/deb-demo/etc/systemd/system/postinst(安装后)
Section titled “postinst(安装后)”#!/bin/shset -e
systemctl daemon-reloadsystemctl enable deb-demo.servicesystemctl start deb-demo.service
exit 0prerm(卸载前)
Section titled “prerm(卸载前)”#!/bin/shset -e
if systemctl is-active --quiet deb-demo.service; then systemctl stop deb-demo.servicefisystemctl disable deb-demo.servicesystemctl daemon-reload
exit 0# X86_64debuild -d -us -uc -b
# ARM64debuild -aarm64 -d -us -uc -b构建完成后,deb 包生成在上级目录。
RPM 包制作(RedHat/CentOS)
Section titled “RPM 包制作(RedHat/CentOS)”spec 文件
Section titled “spec 文件”创建 rpm/deb-demo.spec:
%define release 1%define __os_install_post %{nil}
Name: deb-demoPackager: rootVersion: 0.1.0Release: 1AutoReqProv: noSummary: demo for deb packageURL: http://example.comGroup: DevelopmentLicense: CommercialBuildRoot: %{_tmppath}/%{name}-%{version}-%{release}
%description
%define rootPath /opt/deb-demo
%buildcd $OLDPWD%ifarch aarch64GOOS=linux GOARCH=arm64 make%elseGOOS=linux GOARCH=amd64 make%endif
%installcd $OLDPWDrm -rfv %{buildroot}mkdir -pv %{buildroot}%{rootPath}/bincp -v deb-demo %{buildroot}%{rootPath}/bin/chmod a+x %{buildroot}%{rootPath}/bin/deb-demomkdir -pv %{buildroot}/etc/systemd/systemcp -v deb-demo.service %{buildroot}/etc/systemd/system/
%cleancd $OLDPWDmake clean
%files%{rootPath}//etc/systemd/system/deb-demo.service
%postsystemctl daemon-reloadsystemctl enable deb-demo.servicesystemctl start deb-demo.service
%preunif systemctl is-active --quiet deb-demo.service; then systemctl stop deb-demo.servicefisystemctl disable deb-demo.servicesystemctl daemon-reload# X86_64rpmbuild -ba rpm/deb-demo.spec
# ARM64rpmbuild -ba --target aarch64 rpm/deb-demo.spec| 特性 | DEB | RPM |
|---|---|---|
| 配置文件 | 多个文件 | 单个 spec 文件 |
| 复杂度 | 较高 | 较低 |
| 适用发行版 | Debian/Ubuntu | RedHat/CentOS/Fedora |
| 构建工具 | debuild | rpmbuild |