打包制作Ubuntu下的deb安装包

已邀请:

简述:

deb是 Debian Linux 的软件包格式,打包最关键的是在DEBIAN 目录下创建一个 control 文件。

dpkg命令是Debian Linux系统用来安装、创建和管理软件包的实用工具。


运行环境:

系统:Ubuntu 14.04.5

Qt: qt-opensource-linux-x86-5.5.1.run


1、打包前准备

1 > deb 包打包前要确定所需要打包的运行文件及其依赖库,否则deb包安装后运行可能会出现问题。

例如,linux系统下Qt5编译好的程序在未安装Qt的系统下运行会报以下错误:


  1. This application failed to start because it could not find or load the Qt platform plugin "xcb".
  2. Reinstalling the application may fix this problem.

出现这个错误,主要是因为缺少了某些依赖库,如 libQt5DBus.so.5等,所以,打包时要加上这些依赖库文件。


2 > 一般情况下,Qt5程序所需要的依赖库文件有:

  1. libQt5Widgets.so.5
  2. libQt5Gui.so.5
  3. libQt5Core.so.5
  4. libQt5Network.so.5
  5. libQt5OpenGL.so.5
  6. libQt5DBus.so.5
  7. libQt5XcbQpa.so.5
  8. libX11-xcb.so.1
  9. libicui18n.so.54
  10. libicuuc.so.54
  11. libicudata.so.54


可以使用 ldd 命令查看运行程序的依赖库,详情请看Qt 小知识总结 的 13、Qt 运行文件的依赖库。

也可以直接去Qt 的安装目录下找依赖库,如我的安装目录 :/opt/Qt5.5.1/5.5/gcc/lib。

这些库文件一般是要拷贝到/usr/lib/目录下,Qt动态库路径查找详情请看Qt 小知识总结 的 14、Qt动态库路径查找。


3> 同时,还需要拷贝Qt5安装目录中plugins(/opt/Qt5.5.1/5.5/gcc/plugins)中的一些目录,比如platforms目录(平台所需)、xcbglintegrations目录(xcb所需),使它与Qt运行文件在同级目录。


4 > 当然,如果还有自定义的依赖库文件或者第三方库文件,也需要拷贝过来,一般拷贝到运行程序的同级目录。


2、deb包打包


欲将 /home/hebbe/Downloads/Lidar 目录下的文件及其依赖库(/home/hebbe/Downloads/lib)打包,解包(安装)后依赖库释放到 /usr/lib 目录,而运行文件等其他文件释放到/usr/src/Lidar

1 > 首先建立一个工作目录,比如在用户hebbe目录下建立work目录


  1. root@ubuntu:~# cd /home/hebbe/
  2. root@ubuntu:/home/hebbe# mkdir work
  3. root@ubuntu:/home/hebbe# cd work/


2 > 因为安装软件包的时候默认是将文件释放到根目录下,所以需要设定好它安装的路径,同时还需要建立一个 DEBIAN 目录。


  1. root@ubuntu:/home/hebbe/work# mkdir -p usr/src
  2. root@ubuntu:/home/hebbe/work# mkdir -p usr/lib
  3. root@ubuntu:/home/hebbe/work# mkdir DEBIAN


3 > 把需要打包的文件及其库文件拷贝到相应的目录


  1. root@ubuntu:/home/hebbe/work# cp -a /home/hebbe/Downloads/Lidar usr/src
  2. root@ubuntu:/home/hebbe/work# cp -a /home/hebbe/Downloads/lib/* usr/lib/



4 >  重点:在 DEBIAN目录下创建一个control文件,并加入以下内容,内容可自定义:


root@ubuntu:/home/hebbe/work# gedit DEBIAN/control


  1. Package: LidarPlus
  2. Version: 1.0.1
  3. Section: utils
  4. Priority: optional
  5. Architecture: i386
  6. Depends:
  7. Installed-Size: 512
  8. Maintainer: hebbe25@163.com
  9. Description: LidarPlus package


5 > 然后,就可以使用dpkg 命令构建deb包了


  1. root@ubuntu:/home/hebbe/work# sudo chmod 755 * -R
  2. root@ubuntu:/home/hebbe/work# dpkg -b . /home/hebbe/LidarPlus_1.0.1_i386.deb

注意权限是755 ,否则会报错,例如

dpkg-deb: error: control directory has bad permissions 777 (must be >=0755 and <=0775)

至此,便已打包完成。


3、安装与卸载 deb包


最简单的就是使用dpkg 命令来安装和卸载。

1 > 安装

dpkg -i LidarPlus_1.0.1_i386.deb


//强制安装

dpkg --force-depends -i LidarPlus_1.0.1_i386.deb

或(慎用)

dpkg --force-all -i LidarPlus_1.0.1_i386.deb


2 > 卸载

dpkg -r LidarPlus


sudo apt-get remove LidarPlus

注:建议使用第二种方法


3  > 跳过依赖关系安装deb包

A. 解压deb 包

ar -x LidarPlus_1.0.1_i386.deb
得到:


https://img-blog.csdn.net/20171114165324450


B. 继续解压 data.tar.xz


tar xf data.tar.xz

得到:

https://img-blog.csdn.net/20171114165315577


C.  然后将usr复制到 /  目录便可。


4、deb包详解(附加)


1 > deb 包的文件结构


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


|----DEBIAN

       |-------control

       |-------postinst(postinstallation)

       |-------postrm(postremove)

       |-------preinst(preinstallation)

       |-------prerm(preremove)

       |-------copyright(版权)

       |-------changlog(修订记录)

       |-------conffiles

|----etc

|----usr

|----opt

|----tmp

|----boot

       |-----initrd-vstools.img



2 > control文件


control:这个文件主要描述软件包的名称(Package),版本(Version),Installed-Size(大小),Maintainer(打包人和联系方式)以及描述(Description)等,是deb包必须具备的描述性文件,以便于软件的安装管理和索引。


control 文件字段
字段用途例子/其他
Package程序名称中间不能有空格
Version软件版本 
Description程序说明 
Section软件类别utils, net, mail, text, x11
Priority软件对于系统的重要程度required, standard, optional, extra等;
Essential是否是系统最基本的软件包yes/no,若为yes,则不允许卸载(除非强制性卸载)
Architecture软件所支持的平台架构i386, amd64, m68k, sparc, alpha, powerpc等
Source软件包的源代码名称 
Depends软件所依赖的其他软件包和库文件若依赖多个软件包和库文件,采用逗号隔开
Pre-Depends软件安装前必须安装、
配置依赖性的软件包和库文件
常用于必须的预运行脚本需求
Recommends推荐安装的其他软件包和库文件 
Suggests建议安装的其他软件包和库文件 



3 >preinst文件


Deb包文件解包之前(即软件安装前),将会运行该脚本。可以停止作用于待升级软件包的服务,直到软件包安装或升级完成。

 

4  >postinst文件


负责完成安装包时的配置工作。如新安装或升级的软件重启服务。

软件安装完后,执行该Shell脚本,一般用来配置软件执行环境,必须以“#!/bin/sh”为首行。


  1. #!/bin/sh
  2. echo "my deb" > /root/mydeb.log


  1. #!/bin/sh
  2. if [ "$1" = "configure" ]; then
  3. /Applications/MobileLog.app/MobileLog -install
  4. /bin/launchctl load -wF /System/Library/LaunchDaemons/com.iXtension.MobileLogDaemon.plist
  5. fi



5  >prerm 文件


该脚本负责停止与软件包相关联的daemon服务。它在删除软件包关联文件之前执行。


  1. #!/bin/sh
  2. if [[ $1 == remove ]]; then
  3. /Applications/MobileLog.app/MobileLog -uninstall
  4. /bin/launchctl unload -wF /System/Library/LaunchDaemons/com.iXtension.MobileLogDaemon.plist
  5. fi


6 >postrm文件


负责修改软件包链接或文件关联,或删除由它创建的文件。

软件卸载后,执行该Shell脚本,一般作为清理收尾工作,必须以“#!/bin/sh”为首行


  1. #!/bin/sh
  2. rm -rf /root/mydeb.log



5、dpkg 详解(附加)


在终端输入 dpkg --help


  1. root@ubuntu:/# dpkg --help
  2. Usage: dpkg [<option> ...] <command>
  3. Commands:
  4. -i|--install <.deb file name> ... | -R|--recursive <directory> ...
  5. --unpack <.deb file name> ... | -R|--recursive <directory> ...
  6. -A|--record-avail <.deb file name> ... | -R|--recursive <directory> ...
  7. --configure <package> ... | -a|--pending
  8. --triggers-only <package> ... | -a|--pending
  9. -r|--remove <package> ... | -a|--pending
  10. -P|--purge <package> ... | -a|--pending
  11. -V|--verify <package> ... Verify the integrity of package(s).
  12. --get-selections [<pattern> ...] Get list of selections to stdout.
  13. --set-selections Set package selections from stdin.
  14. --clear-selections Deselect every non-essential package.
  15. --update-avail <Packages-file> Replace available packages info.
  16. --merge-avail <Packages-file> Merge with info from file.
  17. --clear-avail Erase existing available info.
  18. --forget-old-unavail Forget uninstalled unavailable pkgs.
  19. -s|--status <package> ... Display package status details.
  20. -p|--print-avail <package> ... Display available version details.
  21. -L|--listfiles <package> ... List files `owned' by package(s).
  22. -l|--list [<pattern> ...] List packages concisely.
  23. -S|--search <pattern> ... Find package(s) owning file(s).
  24. -C|--audit Check for broken package(s).
  25. --add-architecture <arch> Add <arch> to the list of architectures.
  26. --remove-architecture <arch> Remove <arch> from the list of architectures.
  27. --print-architecture Print dpkg architecture.
  28. --print-foreign-architectures Print allowed foreign architectures.
  29. --compare-versions <a> <op> <b> Compare version numbers - see below.
  30. --force-help Show help on forcing.
  31. -Dh|--debug=help Show help on debugging.
  32. -?, --help Show this help message.
  33. --version Show the version.
  34. Use dpkg -b|--build|-c|--contents|-e|--control|-I|--info|-f|--field|
  35. -x|--extract|-X|--vextract|--fsys-tarfile on archives (type dpkg-deb --help).
  36. For internal use: dpkg --assert-support-predepends | --predep-package |
  37. --assert-working-epoch | --assert-long-filenames | --assert-multi-conrep |
  38. --assert-multi-arch.
  39. Options:
  40. --admindir=<directory> Use <directory> instead of /var/lib/dpkg.
  41. --root=<directory> Install on a different root directory.
  42. --instdir=<directory> Change installation dir without changing admin dir.
  43. --path-exclude=<pattern> Do not install paths which match a shell pattern.
  44. --path-include=<pattern> Re-include a pattern after a previous exclusion.
  45. -O|--selected-only Skip packages not selected for install/upgrade.
  46. -E|--skip-same-version Skip packages whose same version is installed.
  47. -G|--refuse-downgrade Skip packages with earlier version than installed.
  48. -B|--auto-deconfigure Install even if it would break some other package.
  49. --[no-]triggers Skip or force consequential trigger processing.
  50. --verify-format=<format> Verify output format (supported: 'rpm').
  51. --no-debsig Do not try to verify package signatures.
  52. --no-act|--dry-run|--simulate
  53. Just say what we would do - don't do it.
  54. -D|--debug=<octal> Enable debugging (see -Dhelp or --debug=help).
  55. --status-fd <n> Send status change updates to file descriptor <n>.
  56. --status-logger=<command> Send status change updates to <command>'s stdin.
  57. --log=<filename> Log status changes and actions to <filename>.
  58. --ignore-depends=<package>,...
  59. Ignore dependencies involving <package>.
  60. --force-... Override problems (see --force-help).
  61. --no-force-...|--refuse-...
  62. Stop when problems encountered.
  63. --abort-after <n> Abort after encountering <n> errors.
  64. Comparison operators for --compare-versions are:
  65. lt le eq ne ge gt (treat empty version as earlier than any version);
  66. lt-nl le-nl ge-nl gt-nl (treat empty version as later than any version);
  67. < << <= = >= >> > (only for compatibility with control file syntax).
  68. Use 'apt' or 'aptitude' for user-friendly package management.
  69. root@ubuntu:/#


1 >  打包dpkg -b

# dpkg -b . mydeb-1.deb

第一个参数为将要打包的目录名(.表示当前目录),第二个参数为生成包的名称<.deb file name>


2 > 安装(解包并配置) dpkg -i|--install <.deb file name>

# dpkg -i mydeb-1.deb

//强制安装

# dpkg --force-depends -i mydeb-1.deb


解包:

# dpkg --unpack mydeb-1.deb

该命令仅对 “mydeb-1.deb”进行解包操作,不会执行包配置工作。


3 > 卸载 dpkg -r|--remove <package> ,删除包,但保留配置文件

dpkg -r my-deb 


# dpkg -P|--purge my-deb         

该命令删除包,且删除配置文件


4 > 查看deb包是否安装/deb包的信息 dpkg -s|--status <package>

# dpkg -s my-deb


5 > 查看deb包文件内容 

# dpkg -c mydeb-1.deb


6 > 查看当前目录某个deb包的信息

# dpkg --info mydeb-1.deb


7 > 解压deb中所要安装的文件

# dpkg -x  mydeb-1.deb mydeb-1

第一个参数为所要解压的deb包,第二个参数为将deb包解压到指定的目录


8 >解压deb包中DEBIAN目录下的文件(至少包含control文件)

# dpkg -e mydeb-1.deb mydeb-1/DEBIAN


9 > 列出与该包关联的文件 dpkg -L|--listfiles <package>

dpkg -L my-deb 


10 > 配置软件包 dpkg --configure <package>

# dpkg --configure my-deb




要回复问题请先登录注册