平时在开发过程中或者处理debian包时经常会遇到下载源码包的操作并重新编译的问题,此处介绍一下的debian包的处理过程,遵循下载 -- 修改 -- 编译的过程,介绍一下debian系Linux系统中用到的一些操作命令。
查询软件包
如果不清楚软件包名称,可以使用apt-cache search <pkg>
命令来查找源码包的具体名称,此命令支持模糊查询。以iproute2包为例,此处使用iprou作为关键字来查询,查询结果如下。
tencent@ubuntu:~/iproute2$ apt-cache search iprou
iproute - transitional dummy package for iproute2
iproute-doc - transitional dummy package for iproute2-doc
iproute2 - networking and traffic control tools
iproute2-doc - networking and traffic control tools - documentation
golang-github-vishvananda-netlink-dev - netlink library for go
libghc-iproute-dev - Haskell IP routing table
libghc-iproute-doc - Haskell IP routing table; documentation
libghc-iproute-prof - Haskell IP routing table; profiling libraries
上面的搜索结果中罗列出了所有匹配的软件包,使用iproute2软件包。如果向获取此软件包的详细信息,可以通过apt-cache show <pkg>
命令来获得详细信息。
tencent@ubuntu:~/iproute$ apt-cache show iproute2
Package: iproute2
Architecture: amd64
Version: 4.3.0-1ubuntu3.16.04.5
Multi-Arch: foreign
Priority: important
Section: net
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian iproute2 Maintainers <ah-iproute@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 1487
Provides: arpd
Depends: libc6 (>= 2.14), libdb5.3, libmnl0 (>= 1.0.3-4~), libselinux1 (>= 2.0.15)
Recommends: libatm1 (>= 2.4.1-17~), libxtables11
Suggests: iproute2-doc
Conflicts: arpd, iproute (<< 20130000-1)
Replaces: iproute
Filename: pool/main/i/iproute2/iproute2_4.3.0-1ubuntu3.16.04.5_amd64.deb
Size: 522552
MD5sum: b228f6d27f1485a67beea2bbae166bad
SHA1: 2526af36c168345ae65590ca72a471796ca9a261
SHA256: 9792f856e3a051286149262767881007af43148e97f6cf8e02e681ad000b3367
SHA512: c252ab6f1bcec953b54c307baa7e5f57eaf14ec60b9d827c59a3b8c3363420234c3b5801e77eadacf9cd1d2c53cc0143d9a2bab8db2c2918b9af10cbbcb66562
Homepage: http://www.linux-foundation.org/en/Net:Iproute2
Description-en: networking and traffic control tools
The iproute2 suite is a collection of utilities for networking and
traffic control.
.
These tools communicate with the Linux kernel via the (rt)netlink
interface, providing advanced features not available through the
legacy net-tools commands 'ifconfig' and 'route'.
Description-md5: d10cc46ca4a9e5489b7d2c879fcfba17
Task: minimal, ubuntu-core, ubuntu-core, ubuntu-sdk-libs-tools, ubuntu-sdk
Supported: 5y
下载二进制包
通过apt-get download <pkg>
命令可以直接下载二进制软件包,软件包的名称获取方式可以参考上一节中介绍的查询软件包方法。
tencent@ubuntu:~/iproute$ ls -l
total 0
tencent@ubuntu:~/iproute$
tencent@ubuntu:~/iproute$
tencent@ubuntu:~/iproute$ apt-get download iproute2
Get:1 https://mirrors.tencent.com/ubuntu xenial-updates/main amd64 iproute2 amd64 4.3.0-1ubuntu3.16.04.5 [523 kB]
Fetched 523 kB in 0s (523 kB/s)
tencent@ubuntu:~/iproute$
tencent@ubuntu:~/iproute$
tencent@ubuntu:~/iproute$
tencent@ubuntu:~/iproute$ ls -l
total 512
-rw-r--r-- 1 tencent tencent 522552 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5_amd64.deb
此时其实可以通过apt-get install <pkg>
命令从本地安装这个包,与平常安装稍微不同的是,需要给<pkg>
加上本地路径,表明直接从本地安装,而不是从远程仓库上拉取这个包。
下载源码包
下载软件包源码
在Debian系linux操作系统中,可以通过apt-get source <pkg>
命令来下载对应软件包的源码包。
tencent@ubuntu:~/iproute2$ apt-get source iproute2
Reading package lists... Done
NOTICE: 'iproute2' packaging is maintained in the 'Git' version control system at:
git://git.debian.org/git/collab-maint/pkg-iproute.git
Please use:
git clone git://git.debian.org/git/collab-maint/pkg-iproute.git
to retrieve the latest (possibly unreleased) updates to the package.
Need to get 563 kB of source archives.
Get:1 https://mirrors.tencent.com/ubuntu xenial-updates/main iproute2 4.3.0-1ubuntu3.16.04.5 (dsc) [2,491 B]
Get:2 https://mirrors.tencent.com/ubuntu xenial-updates/main iproute2 4.3.0-1ubuntu3.16.04.5 (tar) [525 kB]
Get:3 https://mirrors.tencent.com/ubuntu xenial-updates/main iproute2 4.3.0-1ubuntu3.16.04.5 (diff) [35.2 kB]
Fetched 563 kB in 1s (457 kB/s)
tencent@ubuntu:~/iproute2$ ls -l
total 560
drwxrwxr-x 18 tencent tencent 4096 Nov 27 12:40 iproute2-4.3.0
-rw-r--r-- 1 tencent tencent 35248 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.debian.tar.xz
-rw-r--r-- 1 tencent tencent 2491 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.dsc
-rw-r--r-- 1 tencent tencent 525364 Jan 4 2016 iproute2_4.3.0.orig.tar.xz
上述命令会完整下载iproute2的源码包,并且将源码包解压缩到当前目录中,即文件夹iproute2-4.3.0,如果不想让命令自动解压,可以加上--download-only
参数。
tencent@ubuntu:~/iproute2$ apt-get source --download-only iproute2
Reading package lists... Done
NOTICE: 'iproute2' packaging is maintained in the 'Git' version control system at:
git://git.debian.org/git/collab-maint/pkg-iproute.git
Please use:
git clone git://git.debian.org/git/collab-maint/pkg-iproute.git
to retrieve the latest (possibly unreleased) updates to the package.
Need to get 563 kB of source archives.
Get:1 https://mirrors.tencent.com/ubuntu xenial-updates/main iproute2 4.3.0-1ubuntu3.16.04.5 (dsc) [2,491 B]
Get:2 https://mirrors.tencent.com/ubuntu xenial-updates/main iproute2 4.3.0-1ubuntu3.16.04.5 (tar) [525 kB]
Get:3 https://mirrors.tencent.com/ubuntu xenial-updates/main iproute2 4.3.0-1ubuntu3.16.04.5 (diff) [35.2 kB]
Fetched 563 kB in 1s (513 kB/s)
Download complete and in download only mode
tencent@ubuntu:~/iproute2$ ls -l
total 556
-rw-r--r-- 1 tencent tencent 35248 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.debian.tar.xz
-rw-r--r-- 1 tencent tencent 2491 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.dsc
-rw-r--r-- 1 tencent tencent 525364 Jan 4 2016 iproute2_4.3.0.orig.tar.xz
如此一来,就只有源码包,简单阐述一下下载的三个文件。
- *.dec文件:这个是源码包的描述文件,其中包含包名称、文件列表和校验和信息
- *.debian.tar.xz: 这个是debian包的打包文件,里面包含编译此源码包并将其打包为debian包的控制文件
- *.orig.tar.xz: 这个是真正的源代码,在不使用
--download-only
参数时,下载的文件夹就是这个压缩包解压后的内容
如果--download-only
参数,下载完毕后可以手动解压缩源码包,此处不使用tar -xf <pkg>
命令,debian系统中提供有专用的加压缩命令dpkg-source
。
tencent@ubuntu:~/iproute2$ ls -l
total 556
-rw-r--r-- 1 tencent tencent 35248 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.debian.tar.xz
-rw-r--r-- 1 tencent tencent 2491 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.dsc
-rw-r--r-- 1 tencent tencent 525364 Jan 4 2016 iproute2_4.3.0.orig.tar.xz
tencent@ubuntu:~/iproute2$
tencent@ubuntu:~/iproute2$
tencent@ubuntu:~/iproute2$ dpkg-source -x iproute2_4.3.0-1ubuntu3.16.04.5.dsc
gpgv: Signature made Fri 12 Apr 2019 02:19:37 AM PDT using RSA key ID 8280B242
gpgv: Can't check signature: public key not found
dpkg-source: warning: failed to verify signature on ./iproute2_4.3.0-1ubuntu3.16.04.5.dsc
dpkg-source: info: extracting iproute2 in iproute2-4.3.0
dpkg-source: info: unpacking iproute2_4.3.0.orig.tar.xz
dpkg-source: info: unpacking iproute2_4.3.0-1ubuntu3.16.04.5.debian.tar.xz
dpkg-source: info: applying 0001-Add-moo-feature.patch
dpkg-source: info: applying 0002-txtdocs.patch
dpkg-source: info: applying 1000-ubuntu-poc-fan-driver.patch
dpkg-source: info: applying 1001-ubuntu-poc-fan-driver-v3.patch
dpkg-source: info: applying 1002-ubuntu-poc-fan-driver-vxlan.patch
dpkg-source: info: applying 1003-fix-variable-in-libnetlink.patch
dpkg-source: info: applying 1004-tc.8-Fix-reference-to-tc-tcindex.8.patch
dpkg-source: info: applying 99-increase-receive-buffer-size.patch
dpkg-source: info: applying 1005-ip-maddr-fix-igmp-parsing.patch
dpkg-source: info: applying 1006-ip-maddr-avoid-uninitialized-data.patch
dpkg-source: info: applying 1007-ip-maddr-fix-filtering-by-device.patch
dpkg-source: info: applying 1008-vf_trust_dddf1b44126e.patch
dpkg-source: info: applying 1009-vf_trust_b6d77d9ee312.patch
dpkg-source: info: applying 1010-vf_trust_fe9322781e63.patch
dpkg-source: info: applying 1011-tc-Add-support-for-the-matchall-traffic-classifier.patch
tencent@ubuntu:~/iproute2$
tencent@ubuntu:~/iproute2$
tencent@ubuntu:~/iproute2$
tencent@ubuntu:~/iproute2$ ls -l
total 560
drwxrwxr-x 18 tencent tencent 4096 Nov 27 13:09 iproute2-4.3.0
-rw-r--r-- 1 tencent tencent 35248 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.debian.tar.xz
-rw-r--r-- 1 tencent tencent 2491 Apr 16 2019 iproute2_4.3.0-1ubuntu3.16.04.5.dsc
-rw-r--r-- 1 tencent tencent 525364 Jan 4 2016 iproute2_4.3.0.orig.tar.xz
值得注意的一点,此命令要比apt-get source <pkg>
命令解压的更加完整,因此这条命令会将*.debian.tar.xz
压缩包也解压到源码包中,如下所示。
tencent@ubuntu:~/iproute2$ cd iproute2-4.3.0/
tencent@ubuntu:~/iproute2/iproute2-4.3.0$ ls -l
total 120
drwxrwxr-x 2 tencent tencent 4096 Nov 3 2015 bridge
-rwxrwxr-x 1 tencent tencent 6959 Nov 3 2015 configure
-rw-rw-r-- 1 tencent tencent 18011 Nov 3 2015 COPYING
drwxrwxr-x 5 tencent tencent 4096 Apr 4 2019 debian
drwxrwxr-x 3 tencent tencent 4096 Nov 27 13:09 doc
drwxrwxr-x 3 tencent tencent 4096 Nov 3 2015 etc
drwxrwxr-x 4 tencent tencent 4096 Nov 3 2015 examples
drwxrwxr-x 2 tencent tencent 4096 Nov 3 2015 genl
drwxrwxr-x 5 tencent tencent 4096 Nov 3 2015 include
drwxrwxr-x 2 tencent tencent 4096 Nov 27 13:09 ip
drwxrwxr-x 2 tencent tencent 4096 Nov 27 13:09 lib
-rw-rw-r-- 1 tencent tencent 2188 Nov 3 2015 Makefile
drwxrwxr-x 5 tencent tencent 4096 Nov 3 2015 man
drwxrwxr-x 2 tencent tencent 4096 Nov 3 2015 misc
drwxrwxr-x 2 tencent tencent 4096 Nov 3 2015 netem
-rw-rw-r-- 1 tencent tencent 1364 Nov 3 2015 README
-rw-rw-r-- 1 tencent tencent 1266 Nov 3 2015 README.decnet
-rw-rw-r-- 1 tencent tencent 624 Nov 3 2015 README.devel
-rw-rw-r-- 1 tencent tencent 4403 Nov 3 2015 README.distribution
-rw-rw-r-- 1 tencent tencent 3735 Nov 3 2015 README.iproute2+tc
-rw-rw-r-- 1 tencent tencent 1990 Nov 3 2015 README.lnstat
drwxrwxr-x 2 tencent tencent 4096 Nov 27 13:09 tc
drwxrwxr-x 6 tencent tencent 4096 Nov 3 2015 testsuite
drwxrwxr-x 2 tencent tencent 4096 Nov 3 2015 tipc
修改源码包
此时,可以到源码包中修改源代码,修改主要分为三方面
- 修改debian目录中的打包控制文件
- 修改源代码
- 修改Makefile(可能会使用automake工具)
具体的修改行为此处不多做阐述,可以根据自己的需要来修改。
重新编译源码包
完成自己的修改后,可以重新编译源码,并且将其打包为debian包,由于包一般会依赖于其他包,所以在编译之前需要提前安装好依赖包。
查询依赖关系
在Debian系统中,可以通过apt-cache depends/rdepends <pkg>
命令查询软件包的依赖关系和反向依赖关系。
tencent@ubuntu:~/iproute2/iproute2-4.3.0$ apt-cache depends iproute2
iproute2
Depends: libc6
Depends: libdb5.3
Depends: libmnl0
Depends: libselinux1
Conflicts: <arpd>
Conflicts: iproute
Recommends: libatm1
Recommends: libxtables11
Suggests: iproute2-doc
Replaces: iproute
tencent@ubuntu:~/iproute2/iproute2-4.3.0$ apt-cache rdepends iproute2
iproute2
Reverse Depends:
ubuntu-fan
iproute2:i386
virtualbox
iproute2:i386
|ltsp-server
iproute2:i386
|ltsp-client-core
iproute2:i386
|libguestfs0
iproute2:i386
|guestfsd
iproute2:i386
|criu
iproute2:i386
vlan
iproute2:i386
... ...
此外,使用dpkg-info <pkg>
命令或者使用命令apt-rdepends <pkg>
也能查看包的依赖关系,命令apt-rdepends
默认并未安装,可是通过sudo apt install apt-rdepends
来安装此软件包,此处不再多做阐述。
安装依赖包
上节中介绍了如何查询依赖包,其实并不需要逐个去安装依赖包,在Debian系的Linux系统中,可以通过命令apt-get build-dep <pkg>
安装在编译此包是需要的第三方包。
tencent@ubuntu:~/iproute2/iproute2-4.3.0$ sudo apt-get build-dep iproute2 -y
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
default-jdk-headless libasound2 libasound2-data libasyncns0 libflac8 libgif7 libgl1-mesa-glx libgtk2.0-0 libgtk2.0-common libogg0 libsndfile1 libvorbis0a libvorbisenc2 libxcb-glx0 libxxf86vm1
openjdk-8-jdk-headless
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
bison cm-super-minimal flex iptables-dev libatm1-dev libbison-dev libcupsfilters1 libcupsimage2 libdb-dev libdb5.3-dev libfl-dev libgs9 libgs9-common libharfbuzz-icu0 libijs-0.35 libjbig2dec0
libkpathsea6 libmnl-dev libosp5 libpaper-utils libpaper1 libpoppler58 libpotrace0 libptexenc1 libselinux1-dev libsepol1-dev libsynctex1 libtexlua52 libtexluajit2 libzzip-0-13 linuxdoc-tools
lynx lynx-common opensp poppler-data t1utils tex-common texlive-base texlive-binaries texlive-fonts-recommended texlive-latex-base texlive-latex-recommended xdg-utils
0 upgraded, 43 newly installed, 0 to remove and 0 not upgraded.
Need to get 757 kB/66.7 MB of archives.
After this operation, 208 MB of additional disk space will be used.
... ...
编译debian包
解决完依赖包的问题后,可以正式开始编译了,可以通过dpkg-buildimage
命令直接完成编译和打包工作。切换至源码包目录中
tencent@ubuntu:~/iproute2/iproute2-4.3.0$ dpkg-buildpackage -rfakeroot -b -us -uc
dpkg-buildpackage: source package iproute2
dpkg-buildpackage: source version 4.3.0-1ubuntu3.16.04.5
dpkg-buildpackage: source distribution xenial
dpkg-buildpackage: source changed by Stefan Bader <stefan.bader@canonical.com>
dpkg-buildpackage: host architecture amd64
dpkg-source --before-build iproute2-4.3.0
执行完毕后会在上一级目录中找到编译的debian包。