2.Linux制作deb包的方法¶
2.3. 从零开始创建自己的deb包¶
安装工具及依赖:
sudo apt-get install build-essential debhelper make autoconf automake dpkg-dev fakeroot pbuilder gnupg
首先我们创建如下目录及文件
1
2
3
4
5
6
7
8hello_deb/
├── DEBIAN
│ ├── control
│ ├── postinst
│ └── postrm
└── opt
└── hello_deb
└── hello_deb.sh
在hello_deb目录下创建DEBIAN及opt/hello_deb目录,DEBIAN目录下包含控制信息文件,
而在opt/hello_deb目录下创建hello_deb.sh文件则表示我们需要将hello_deb.sh文件安装到
linux系统的opt/hello_deb目录下。
然后分别给予postinst、postrm、hello_deb.sh文件以可执行权限。
其中control文件所包含信息如下:
hello_deb/DEBIAN/control¶
1
2
3
4
5
6
7
8
9Package: hello-deb
Version: 1.0.0
Section: free
Priority: optional
Essential: no
Architecture: armhf
Maintainer: embedfire
Provides: hell_deb
Description: deb test
若以后想升级这个deb包,可以修改该包的版本号Version,值得注意的是Architecture,
前面我们也有讲到,就是该deb包所支持的处理器架构,
因为最终要将该deb包安装到arm开发板上,而arm处理器架构为armhf,
所以我们应该在Architecture中填入armhf属性,大家可根据自己的需求做相应修改即可,
如果不知道你的处理器架可以通过dpkg -l命令来查看已安装的deb包持支的架构,
或者输入lscpu查看处理器信息,若想支持所有架构,可以填入all属性,
如果Architecture属性与当前处理器架构属性不匹配的话,deb包将无法成功安装,
且control的属性信息必须以字母或者数字开头,不然可能导致打包出错。
postinst文件包含信息如下:
hello_deb/DEBIAN/postinst¶
1
2
3
4
5#!/bin/bash
if [ "$1" = "upgrade" ] || [ "$1" = "install" ];then
echo "hello_deb installing"
fi
当安装了该deb包以后,系统会默认执行postinst脚本,
通常我们利用该脚本来搭建一些为软件执行的环境(如创建目录、修改权限等),
值得注意的是该文件需具有可执行权限。
这里写的比较简单,判断第一个参数,仅供参考。
postrm文件包含信息如下:
hello_deb/DEBIAN/postrm¶
1
2
3
4
5
6
7
8#!/bin/bash
if [ "$1" = "upgrade" ] ; then
echo "upgrade"
elif [ "$1" = "remove" ] || [ "$1" = "purge" ] ; then
echo "remove"
fi
当卸载了该deb包以后,系统会默认执行postrm脚本,通常我们利用该脚本来清理环境,
值得注意的是该文件具有可执行权限。
这里写的比较简单,判断第一个参数,仅供参考。
最后我们来看下真正的程序主体,为了简单起见,此处以一个简单的脚本为例。
hello_deb/opt/hello_deb/hello_deb.sh¶
1
2
3
4#! /bin/bash
echo Hello deb!
echo This is a test script!!!
脚本仅仅是打印两句信息,用户可自行设置需要执行的程序。
万事俱备,只欠东风,当备齐了制作deb包的基本原材料之后我们便可以开始制作属于自己的deb包了,
进入hello_deb目录下,也就是DEBIAN及home文件夹所在的目录,接着输入如下命令来构建软件包。
sudo dpkg-deb -b ../hello_deb ../hello_deb_1.0.0_armhf.deb
其中dpkg-deb是构建deb包命令,-b参数表示要构建一个deb包,
../hello_deb参数表示要构建deb包原材料的路径,
../hello_deb_1.0.0_armhf.deb参数表示将该deb包构建在当前目录的上级目录中,
一般我们构建deb包的名字都会遵循这么一个原则,
其命名方式为:软件名称+软件版本号+该软件所支持的处理器架构,
如软件名为hello_deb,版本号为1.0.0,所支持的处理器架构为armhf。
打包成功后会输出如下信息,并可在上级目录查看到deb安装包:
1
2
3
4
5qinghui@ebf-dev:~/deb/hello_deb$ sudo dpkg-deb -b ../hello_deb ../hello_deb_1.0.0_armhf.deb
dpkg-deb: 正在 '../hello_deb_1.0.0_armhf.deb' 中构建软件包 'hello-deb'。
qinghui@ebf-dev:~/deb/hello_deb$ ls ..
hello_deb hello_deb_1.0.0_armhf.deb
qinghui@ebf-dev:~/deb/hello_deb$
制作好自己的deb包后我们需要验证一下是否真的制作成功,
可以如下命令查看已制作的deb包文件内容:
1
2
3
4
5
6
7
8#命令
dpkg -c hello_deb_1.0.0_armhf.deb
#打印信息
drwxrwxr-x qinghui/qinghui 0 2021-04-15 11:16 ./
drwxrwxr-x qinghui/qinghui 0 2021-04-15 11:16 ./opt/
drwxrwxr-x qinghui/qinghui 0 2021-04-15 11:22 ./opt/hello_deb/
-rwxrwxr-x qinghui/qinghui 65 2021-04-15 11:22 ./opt/hello_deb/hello_deb.sh
也可使用如下命令查看deb包信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#命令
dpkg --info hello_deb_1.0.0_armhf.deb
#打印信息
new Debian package, version 2.0.
size 976 bytes: control archive=500 bytes.
190 字节, 9 行 control
100 字节, 7 行 * postinst #!/bin/bash
133 字节, 8 行 * postrm #!/bin/bash
Package: hello-deb
Version: 1.0.0
Section: free
Priority: optional
Essential: no
Architecture: armhf
Maintainer: embedfire
Provides: hell_deb
Description: deb test
将该deb包拷贝到linux开发板的文件系统下,
输入“sudo dpkg -i hello_deb_1.0.0_armhf.deb”命令即可安装,
其中-i 参数表示安装软件,即install,
并且在安装完软件之后可以输入“dpkg -s hello-dev”命令查看是否安装了软件。
如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16root@npi:/home/debian# sudo dpkg -i hello_deb_1.0.0_armhf.deb
Selecting previously unselected package hello-deb.
(Reading database ... 12164 files and directories currently installed.)
Preparing to unpack hello_deb_1.0.0_armhf.deb ...
Unpacking hello-deb (1.0.0) ...
Setting up hello-deb (1.0.0) ...
root@npi:/home/debian# dpkg -s hello-deb
Package: hello-deb
Status: install ok installed
Priority: optional
Section: free
Maintainer: embedfire
Architecture: armhf
Version: 1.0.0
Provides: hell_deb
Description: deb test
或者输入“dpkg -l | grep hello-deb”命令查看你的软件是否在已安装软件列表里面。
1
2root@npi:/home/debian# dpkg -l | grep hello-deb
ii hello-deb 1.0.0 armhf deb test
验证安装完成之后查看开发板/opt/hello_deb目录下是否存在hello_deb.sh文件。
1
2
3
4root@npi:/home/debian# ls /opt/
backup hello_deb scripts source
root@npi:/home/debian# ls /opt/hello_deb/
hello_deb.sh
执行看看效果
1
2
3root@npi:/home/debian# /opt/hello_deb/hello_deb.sh
Hello deb!
This is a test script!!!
到此,制作deb包的基本流程已介绍完毕。