初识deb包的制作

文章最后更新时间为:2020年09月10日 14:24:08

1.deb包是什么

deb是Debian软件包格式,文件扩展名为.deb。

Debian包是Unixar的标准归档,将包文件信息以及包内容,经过gzip和tar打包而来。所以deb包本质是一个压缩包文件,里面包含一些特定的目录和文件,安装过程就是dpkg程序按照指定的规则去拷贝文件和执行脚本。

2.deb包的文件结构

deb包里面的结构:DEBIAN目录 和 软件具体安装目录(模拟安装目录)(如etc, usr, opt, tmp等)。

在DEBIAN目录中至少有control文件,还可能有postinst(postinstallation)、postrm(postremove)、preinst(preinstallation)、prerm(preremove)、copyright (版权)、changlog (修订记录)和conffiles等。

  • control文件:描述软件包的名称(Package),版本(Version),描述(Description)等,是deb包必须剧本的描述性文件,以便于软件的安装管理和索引。为了能将软件包进行充分的管理,可能还具有以下字段:

    • Section:申明软件的类别,常见的有utils’, net’, mail’, text’, `x11′ 等;
    • Priority:申明软件对于系统的重要程度,如required’, standard’, optional’, extra’ 等
    • Essential:申明是否是系统最基本的软件包(选项为yes/no),如果是的话,这就表明该软件是维持系统稳定和正常运行的软件包,不允许任何形式的卸载(除非进行强制性的卸载)
    • Architecture:软件包结构,如基于i386′, ‘amd64’,m68k’, sparc’, alpha’, `powerpc’ 等;
    • Source:软件包的源代码名称;
    • Depends:软件所依赖的其他软件包和库文件。如果是依赖多个软件包和库文件,彼此之间采用逗号隔开;
    • Pre-Depends:软件安装前必须安装、配置依赖性的软件包和库文件,它常常用于必须的预运行脚本需求;
    • Recommends:这个字段表明推荐的安装的其他软件包和库文件;
    • Suggests:建议安装的其他软件包和库文件。
  • postinst文件:包含了软件在进行正常目录文件拷贝到系统后,所需要执行的配置工作。
  • prerm文件:软件卸载前需要执行的脚本。
  • postrm文件:软件卸载后需要执行的脚本。

比如查看https://github.com/nashaofu/dingtalk的deb包结构:

# 1. 解包安装内容
$ sudo dpkg -X dingtalk-2.1.7-latest-amd64.deb ./dingtalk

# 2. 解压控制内容
$ sudo dpkg -e dingtalk-2.1.7-latest-amd64.deb ./dingtalk/DEBIAN/

# 3.查看目录结构
yanq@yanq-desk:~/Downloads$ tree dingtalk -L 3
dingtalk
├── DEBIAN
│   ├── control
│   ├── md5sums
│   ├── postinst
│   └── postrm
├── opt
│   └── 钉钉
│       ├── chrome_100_percent.pak
│       ├── chrome_200_percent.pak
│       ├── chrome-sandbox
│       ├── dingtalk
│       ├── icudtl.dat
│       ├── libEGL.so
│       ├── libffmpeg.so
│       ├── libGLESv2.so
│       ├── libvk_swiftshader.so
│       ├── libvulkan.so
│       ├── LICENSE.electron.txt
│       ├── LICENSES.chromium.html
│       ├── locales
│       ├── resources
│       ├── resources.pak
│       ├── snapshot_blob.bin
│       ├── swiftshader
│       ├── v8_context_snapshot.bin
│       └── vk_swiftshader_icd.json
└── usr
    └── share
        ├── applications
        ├── doc
        └── icons


31 directories, 87 files

# 4. 查看控制文件
# 4.1 描述性文件
$ cat dingtalk/DEBIAN/control 
Package: dingtalk
Version: 2.1.7
License: MIT
Vendor: nashaofu <diaocheng@outlook.com>
Architecture: amd64
Maintainer: nashaofu <diaocheng@outlook.com>
Installed-Size: 198353
Depends: libgtk-3-0, libnotify4, libnss3, libxss1, libxtst6, xdg-utils, libatspi2.0-0, libuuid1, libappindicator3-1, libsecret-1-0
Section: default
Priority: extra
Homepage: https://github.com/nashaofu/dingtalk#readme
Description: 
  钉钉桌面版,基于electron和钉钉网页版开发,支持Windows、Linux和macOS

# 4.2 安装时执行文件
$ cat dingtalk/DEBIAN/postinst 
#!/bin/bash

# Link to the binary
ln -sf '/opt/钉钉/dingtalk' '/usr/bin/dingtalk'

# SUID chrome-sandbox for Electron 5+
chmod 4755 '/opt/钉钉/chrome-sandbox' || true

update-mime-database /usr/share/mime || true
update-desktop-database /usr/share/applications || true

# 4.3 卸载时执行的文件
$ cat dingtalk/DEBIAN/postrm 
#!/bin/bash

# Delete the link to the binary
rm -f '/usr/bin/dingtalk'

3. 制作一个简单的deb包

以安装xpramgr(自写的一个bash脚本)为例,原本的安装形式为:

sudo wget -O /usr/local/bin/xpramgr  && sudo chmod +x /usr/local/bin/xpramgr

接下来我们将安装改为deb包的形式:

3.1制作目录和文件

先新建目录和文件

$ mkdir ~/xpramgr-0.1
$ mkdir ~/xpramgr-0.1/DEBIAN
$ mkdir ~/xpramgr-0.1/opt
$ touch control文件内容
$ touch ~/xpramgr-0.1/DEBIAN/postinst
$ touch ~/xpramgr-0.1/DEBIAN/postrm
$ touch ~/xpramgr-0.1/opt/xpramgr

# 最终实现的目录树如下
xpramgr-0.1/
├── DEBIAN
│   ├── control
│   ├── postinst
│   └── postrm
└── opt
    └── xpramgr

3.2 准备安装内容

将xpramgr的shell脚本源码写到~/xpramgr-0.1/opt/xpramgr中

3.3 准备控制内容

  • control文件
$ cat ~/xpramgr-0.1/DEBIAN/control 
Package: xpramgr-0.1
Version: 0.1
Section: free
Prioritt: optional
Architecture: amd64
Maintainer: flowergu
Description: xpra manager
  • postinst文件
$ cat ~/xpramgr-0.1/DEBIAN/postinst 
#!/bin/bash

chmod +x /opt/xpramgr

ln -sf /opt/xpramgr /usr/local/bin/xpramgr
  • postrm文件
$ cat ~/xpramgr-0.1/DEBIAN/postrm 
#!/bin/bash

# Delete the link to the binary
rm -f /usr/local/bin/xpramgr
  • 赋予控制文件可执行文件
$ chmod +x ~/xpramgr-0.1/DEBIAN/post*

3.4 打包成deb文件

$ sudo dpkg -b ~/xpramgr-0.1 ~/xpramgr-0.1.deb

3.5 安装

# 安装之前
$ ls -la /opt | grep xpra
$ whereis xpramgr
xpramgr:

# 安装
$ sudo dpkg -i ~/xpramgr-0.1.deb 
Selecting previously unselected package xpramgr-0.1.
(Reading database ... 194869 files and directories currently installed.)
Preparing to unpack /home/yanq/xpramgr-0.1.deb ...
Unpacking xpramgr-0.1 (0.1) ...
Setting up xpramgr-0.1 (0.1) ...


# 安装之后
$ ls -la /opt | grep xpra
-rwxrwxr-x  1 yanq yanq 2931 Sep  3 14:18 xpramgr
$ whereis xpramgr
xpramgr: /usr/local/bin/xpramgr
$ xpramgr -h
xpramgr Usage:
    xpramgr install
    xpramgr start
    xpramgr stop
    xpramgr attach [ServerIp] [password]
    xpramgr info
    xpramgr changepwd [password]

3.6 卸载

$ sudo dpkg -r xpramgr-0.1
(Reading database ... 194870 files and directories currently installed.)
Removing xpramgr-0.1 (0.1) ...
$ ls -la /opt| grep xpra
$ whereis xpramgr
xpramgr:

参考

1 + 4 =
1 评论
    das Chrome 117 Windows 10
    2023年12月12日 回复

    不错