deb包制作

简介

deb编包的本质是:将编译过程自动化,并生成可执行程序,使得可以通过apt-get中安装。

源码,编译器编译成指定架构版本的二进制,
不同架构的二进制组织形式不同,如大小端对齐。

DEB源码介绍

DEB 包的源码是由:程序源码+debian 目录构成,其中 debian 目录中存放着打包成 DEB 文件所需的全部文件。
通过 debian 目录中的文件可以定制软件包的行为。其中最为重要的,对于所有的软件包都必需的基本文件是:

  • control :包的一些基本信息(名字、依赖等)
    • 软件包在构建和运行时的依赖
    • 软件包其他主要信息
  • rules :构建包时的规则
    • 是用来指定如何构建软件包的,是包的编译规则文件;
    • 默认可以调用软件源码的 make 等编译程序及规则进行;
    • 也可对默认的构建编译流程进行覆写,以满足 DEB 打包的特殊需求。
  • changelog : Debian 包的修改记录
    • 记录当前 DEB 包的变更历史;
    • 同时 DEB 包的版本号也是在此文件中指定;
  • install:安装指定文件到系统目录
    • 文件拷贝列表,声明 DEB 安装时所需拷贝的文件及目标位置
  • copyright :包的版权信息
    • 包含软件包的版权和授权信息;

新建源码文件

新建文件 ~/hello-1.0/hello.bash

#!/bin/bash

echo "Hellocdq!!!"

制作压缩包

tar -zcvf ~/hello-1.0.tar.gz ~/hello-1.0/

生成debian文件夹

cd ~/hello-1.0
debian ../hello-1.0.tar.gz
#此时生成 ~/hello-1.0/debian/

修改debian文件夹

修改control

vim debian/control
Source: hello
Section: shells
Priority: optional
Maintainer: chendeqiang <chendeqiang@kylinos.cn>
Build-Depends: debhelper-compat (= 12) #添加编译依赖
Standards-Version: 4.4.1
Homepage: <insert the upstream URL, if relevant>
#Vcs-Browser: https://salsa.debian.org/debian/hello
#Vcs-Git: https://salsa.debian.org/debian/hello.git

Package: hello1
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, abc  #添加依赖包
Description: <insert up to 60 chars description>
 <insert long description, indented with spaces>

Package: hello2 #一份源码生成两个包
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: <insert up to 60 chars description>
 <insert long description, indented with spaces>

修改changelog

vim debian/changelog
hello (1.0-1) v100; urgency=medium

  * Initial release (Closes: #nnnn)  <nnnn is the bug number of your ITP>

 -- chendeqiang <chendeqiang@kylinos.cn>  Thu, 28 Jan 2021 21:05:06 +0800

修改format

vim debian/source/format
3.0 (native)

3.0 (native) 指base是自建包,3.0 (quilt) 指base是上游包。

修改rules

如果要安装二进制等文件,则需要覆写rules->dh_install

vim debian/rules
# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
    dh $@

override_dh_install:
    cp hellobash.sh /usr/bin

# dh_make generated override targets
# This is example for Cmake (See https://bugs.debian.org/641051 )
#override_dh_auto_configure:
#   dh_auto_configure -- #  -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

需要注意的是,如果覆写了二进制被安装在/usr/bin ,则debuild需要加sudo权限,且安装后的二进制文件不具备执行权限,需要手动加上可执行权限。

修改install

如果makefile没有指定一些文件的安装目录,则可以在install文件中进行指定。

vim debian/install
hello /usr/bin/

生成gpg密钥

gpg --generate-key

真实姓名:chendeqiang
电子邮箱地址:chendeqiang@kylinos.cn
o
输入两次密码

pub   rsa1234 2021-01-28 [SC] [有效至:2023-01-28]
      9B91BDA0F08C1C82F322B25B660EAA39DB4FF519
uid                      qiangdechen <chenjjjj@kk.cn>
sub   rsa1234 2021-01-28 [E] [有效至:2023-01-28]

其中,rsa1234将作为加密的密钥。

构建deb包

#如果没有 debuild 命令,则:
#sudo apt install devscripts
debuild -us -uc -nc -krsa1234

#此时将生成 ~/hello1_1.0-1_amd64.deb 和 ~/hello2_1.0-1_amd64.deb

如果报错:dpkg-checkbuilddeps: 错误: Unmet build dependencies: build-essential:native
则:sudo apt install build-essential

配置debuild所使用的默认签名

echo 'DEBSIGN_KEYID="rsa1234"' >> ~/.devscripts

#如果简写的不行,就全写:
echo 'DEBSIGN_KEYID="9B91BDA0F08C1C82F322B25B660EAA39DB4FF519"' >> ~/.devscripts

运行 debuild -sa -S (注意:V10.1 debuild -sa -S -nc)生成源码包时就会自动用选择的key来对.dsc和.changes文件进行签名了
注意:配置默认签名只对debuild命令有效,dpkg-buildpackage -S 依然是根据changelog里的署名来查找key,如果找不到就不会签名。
本地编包命令:dpkg-buildpackage -b

本地安装deb包

sudo dpkg -i eee_1.1-1_arm64.deb

# 卸载deb包
# sduo dpkg -r -i eee_1.1-1_arm64.deb

运行可执行文件

demo
#helloworld!

上传公钥到PPA

上传gpg公钥

# 查看秘钥
gpg --fingerprint

# 上传公钥到服务器
gpg --keyserver 172.0.0.0 --send-key  rsa1234

关联ppa账号和gpg秘钥

打开 https://launchpad.dev/~chendeqiang ,修改 OpenPGP keys,
之后launchpad将发送一封用GPG加密的邮件到用户的邮箱,
然后使用 gpg --decrypt 解密,并点击链接完成认证

提示:可以使用 seahorse 查看和管理gpg和ssh秘钥。
更多gpg信息参考:https://www.cnblogs.com/chendeqiang/p/14233944.html

上传ssh公钥

# 生成秘钥
ssh-keygen  –t  rsa
# 一直回车

# 复制公钥
vim ~/.ssh/id_rsa.pub

打开 https://launchpad.dev/~chendeqiang ,修改 SSH keys,
粘贴刚刚复制的秘钥

其他

添加域名/etc/hosts 和 ~/.dput.cf可参考《launchpad平台用户使用手册》

上传deb包到PPA进行编译

cd ~/
dput devppa:chendeqiang/kylinos-desktop/ppatest hello1_1.0-1_source.changes 

添加PPA的apt源

访问应用主页 https://launchpad.dev/~chendeqiang/+archive/kylinos-desktop/ppatest
添加Technical details about this PPA下的两个链接到source.list

sudo vim /etc/apt/source.list
sudo apt-get update

安装应用

重新打开一个终端

sudo apt-get install cdqtest

# 运行应用
demo

恢复源码包

下载以下三个包:

xxx.dsc 
xxx.origin.tar.gz 
xxx.debian.tar.xz

然后:

dpkg-source -x xxx.dsc 

makefile方式构建源码

hello.c

#include<stdio.h>
int main()
{
        printf("helloworld!
");
        return 0;
}

Makefile

hello : hello.o
        gcc -o hello hello.o

hello.o : hello.c
        gcc -c hello.c

#clean :
#      rm hello hello.o
#install:
#      install hello /usr/bin/

cmake方式构建源码

hello.c

#include<stdio.h>
int main()
{
        printf("helloworld!
");
        return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(hellocdq)
add_executable(hellocdq hello.cpp)

debin/control

Build-Depends: debhelper (>=9) , cmake

debian/rules

#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1


# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
	dh $@ 

# dh_make generated override targets
# This is example for Cmake (See https://bugs.debian.org/641051 )
override_dh_auto_configure:
	dh_auto_configure -- #	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

参考链接:
https://blog.csdn.net/kyle__shaw/article/details/8938787?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control
https://blog.csdn.net/kyle__shaw/article/details/8938787

其他参考资料:

原文地址:https://www.cnblogs.com/chendeqiang/p/14232064.html