Contents

如何创建一个简单的Debian包

1. 概述

Debian 软件包是在基于 Debian 的发行版中分发软件的最简单和最有效的方式。它负责管理依赖项并为安装/升级/卸载操作提供良好的界面。

创建包 的官方方式涉及很多步骤和过程。在本教程中,我们将了解创建这些包的更简单方法。然而,官方的包创建方式是理想的,推荐用于生产目的。

2. 准备文件

2.1. .deb包的结构

在基于 Debian 的发行版中,我们安装应用程序的方法之一是下载*.deb*包文件并使用 dpkg 命令安装它。这个“deb 包”是与软件应用程序相关的二进制文件和配置文件的存档。存档中的所有文件都保存在特定的文件夹结构中。

在安装过程中,在目标机器中,二进制文件和配置文件从根文件夹进入类似的文件夹结构

2.2. 应用程序相关文件

让我们看一个例子来更清楚地理解。首先,我们为构建创建一个工作文件夹:

itcodingman@blogdemo:~/Documents/deb/build$ mkdir test

请注意路径——当我们完成这个练习时,我们将更改文件夹。

然后,我们为我们的应用程序准备文件。为了简单起见,让我们有两个文件:

  • test.sh 可执行文件
  • test.conf配置文件

对于这些文件,我们将在本练习中保留如下内容:

itcodingman@blogdemo:~/Documents/deb/build/test$ cat test.conf 
NAME=Test
itcodingman@blogdemo:~/Documents/deb/build/test$ cat test.sh
#!/bin/bash
. test.conf
echo "Hi $NAME"

我们知道 Debian 中的可执行文件保存在 /bin文件夹中,配置文件保存在 /etc文件夹中。按照惯例,我们将test.sh文件保存在/ bin文件夹中,将test.conf文件保存在 /etc文件夹中。

因此,我们在当前目录中创建了两个文件夹bin和 etc

itcodingman@blogdemo:~/Documents/deb/build/test$ mkdir {bin,etc}
itcodingman@blogdemo:~/Documents/deb/build/test$ ls
bin  etc  test.conf  test.sh

现在,让我们将可执行文件和配置文件移动到各自的文件夹中:

itcodingman@blogdemo:~/Documents/deb/build/test$ mv test.conf etc/
itcodingman@blogdemo:~/Documents/deb/build/test$ mv test.sh bin/
itcodingman@blogdemo:~/Documents/deb/build/test$ ls
bin  etc

有了这个,我们将我们的文件放在一个文件夹结构中,一旦它安装在目标机器上,它就会是相同的

2.3. 控制文件_

最后,我们需要一个包含包元数据的控制文件 。这是一个包含字段和描述的文本文件。支持的字段有:

  • 包裹(必填):包裹名称
  • 版本(必需):包的版本
  • 维护者(必填):维护者的姓名和电子邮件
  • 架构(必需):支持的目标机器架构
  • 描述(必填):包的简短描述
  • 部分:包分类,如admindatabasekernelutils
  • 优先级:包是可选的还是必需的
  • Essential:是否始终需要包
  • Depends:列出其他依赖包,例如libc6 (>= 2.2.4-4)
  • 主页:与包关联的网站的 URL
  • Package-Type:表示类型,例如deb 或udeb,例如

如上所述,某些字段是必需的,而其他字段是可选的。

使用这些信息,让我们创建一个包含必填字段和一些推荐字段的控制文件。该文件位于名为 DEBIAN 的文件夹下

itcodingman@blogdemo:~/Documents/deb/build/test$ ls
bin  DEBIAN  etc
itcodingman@blogdemo:~/Documents/deb/build/test$ cat DEBIAN/control 
Package: test
Version: 1.0-1
Section: utils
Priority: optional
Architecture: all
Maintainer: Blogdemo <itcodingman@blogdemo>
Description: This is a test application
 for packaging

3. 构建包

有了这个,我们就可以构建我们的包了。为此,我们可以使用dpkg-deb 命令:

itcodingman@blogdemo:~/Documents/deb/build$ dpkg-deb --root-owner-group --build test
dpkg-deb: building package 'test' in 'test.deb'.
itcodingman@blogdemo:~/Documents/deb/build$ ls
test  test.deb

可以看到,一个test.deb文件就创建成功了。有了这个,我们几乎准备好了我们的包裹。

4. 验证包

4.1. 检查内容

创建包后,我们可以通过运行 dpkg命令来验证文件是否存在于其中:

itcodingman@blogdemo:~/Documents/deb/build$ dpkg -c test.deb 
drwxr-xr-x thinkpalm/thinkpalm 0 2021-12-23 12:09 ./
drwxr-xr-x thinkpalm/thinkpalm 0 2021-12-23 11:30 ./bin/
-rwxr-xr-x thinkpalm/thinkpalm 40 2021-12-23 11:15 ./bin/test.sh
drwxr-xr-x thinkpalm/thinkpalm  0 2021-12-23 11:30 ./etc/
-rw-r--r-- thinkpalm/thinkpalm 10 2021-12-23 11:15 ./etc/test.conf

从结果中,我们看到它包含我们在预期路径中添加的文件。

4.2. lint

为了进一步遵守 Debian 打包约定,我们可以对 package 进行 lint。为此,我们使用名为 lintian 的工具。 让我们看看如何使用这个工具:

itcodingman@blogdemo:~/Documents/deb/build$ lintian test.deb 
E: test: debian-changelog-file-missing
E: test: file-in-etc-not-marked-as-conffile etc/test.conf
E: test: no-copyright-file
W: test: script-with-language-extension bin/test.sh
W: test: binary-without-manpage bin/test.sh

运行命令后,我们可以看到一些标记为E的错误和标记为 W的警告。 让我们一一检查如何解决这些问题。

更改日志文件丢失

要修复此错误,我们可以添加一个changelog.Debian文件:

itcodingman@blogdemo:~/Documents/deb/build$ cat changelog.Debian 
test (1.0-2) stable; urgency=low
  [ Blogdemo ]
  * Changes to installer 
 -- Blogdemo <itcodingman@blogdemo>  Thu,  23 Dec 2021 11:30:00 +0100 
test (1.0-1) stable; urgency=low
  [ Blogdemo ]
  * Wonderful program to print the name ;)
 -- Blogdemo <itcodingman@blogdemo>  Thu,  23 Dec 2021 11:00:00 +0100 
itcodingman@blogdemo:~/Documents/deb/build$ gzip --best -n changelog.Debian 
itcodingman@blogdemo:~/Documents/deb/build$ mkdir -p test/usr/share/doc/test
itcodingman@blogdemo:~/Documents/deb/build$ cp changelog.Debian.gz test/usr/share/doc/test/

将文件标记为 Conf 文件

为了修复这个错误,我们可以在 DEBIAN文件夹中添加一个conffile

itcodingman@blogdemo:~/Documents/deb/build$ cat test/DEBIAN/conffiles 
/etc/test.conf

添加版权文件

为了解决这个问题,我们可以添加一个copyright文件:

itcodingman@blogdemo:~/Documents/deb/build$ cat test/usr/share/doc/test/copyright 
test
Copyright: 2020 Blogdemo <itcodingman@blogdemo>
2020-12-23
The entire code base may be distributed under the terms of the GNU General
Public License (GPL), which appears immediately below.  Alternatively, all
of the source code as any code derived from that code may instead be
distributed under the GNU Lesser General Public License (LGPL), at the
choice of the distributor. The complete text of the LGPL appears at the
bottom of this file.
See /usr/share/common-licenses/(GPL|LGPL)

删除二进制文件的扩展名

让我们删除脚本文件的扩展名来修复这个错误:

itcodingman@blogdemo:~/Documents/deb/build$ mv test/bin/test.sh test/bin/test 

为二进制添加人条目

让我们添加一个man文件来修复这个错误:

itcodingman@blogdemo:~/Documents/deb/build$ cat test.1
.\"                                      Hey, EMACS: -*- nroff -*-
.\" (C) Copyright 2020 Blogdemo <itcodingman@blogdemo>
.\"
.TH TEST 1 
.SH NAME
test \- test application
.SH SYNOPSIS
.B test.sh 
.SH DESCRIPTION
The 
.B test 
prints the name.
.SH SEE ALSO
.BR echo (1).
.SH AUTHORS
The
.B test 
script was written by 
Blogdemo <itcodingman@blogdemo>
.PP
This document was written by Blogdemo <itcodingman@blogdemo> for Debian.
itcodingman@blogdemo:~/Documents/deb/build$ gzip --best -n test.1
itcodingman@blogdemo:~/Documents/deb/build$ ls
build.sh  changelog.Debian.gz  changelog.gz  test  test_1.0-1.deb  test.1.gz
itcodingman@blogdemo:~/Documents/deb/build$ mkdir -p test/usr/share/man/man1
itcodingman@blogdemo:~/Documents/deb/build$ cp test.1.gz test/usr/share/man/man1/

最终的文件夹结构将是:

itcodingman@blogdemo:~/Documents/deb/build$ tree test
test
├── bin
│   └── test
├── DEBIAN
│   ├── conffiles
│   └── control
├── etc
│   └── test.conf
└── usr
    └── share
        ├── doc
        │   └── test
        │       ├── changelog.Debian.gz
        │       └── copyright
        └── man
            └── man1
                └── test.1.gz
9 directories, 7 files

让我们再次构建它:

itcodingman@blogdemo:~/Documents/deb/build$ dpkg-deb --root-owner-group --build test
dpkg-deb: building package 'test' in 'test.deb'.
itcodingman@blogdemo:~/Documents/deb/build$ mv test.deb test_1.0-2.deb
itcodingman@blogdemo:~/Documents/deb/build$ lintian test_1.0-2.deb
itcodingman@blogdemo:~/Documents/deb/build$

现在,我们可以看到所有错误和警告都已修复