以32位CentOS 6.6环境为例,获取ls命令的源代码其实并不复杂。下面一步一步走通整个流程,关键步骤会给出细节和注意事项。
1)配置源代码源
CentOS中,yum的源配置文件在 /etc/yum.repos.d/CentOS-Base.repo,修改它需要root权限。打开文件后,你会看到类似下面的内容(实际内容因发行版本和镜像而异):
# CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # # [base] name=CentOS-$releasever - Base mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os #baseurl=https://mirror.centos.org/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #released updates [updates] name=CentOS-$releasever - Updates mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates #baseurl=https://mirror.centos.org/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras #baseurl=https://mirror.centos.org/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus #baseurl=https://mirror.centos.org/centos/$releasever/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 #contrib - packages by Centos Users [contrib] name=CentOS-$releasever - Contrib mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib #baseurl=https://mirror.centos.org/centos/$releasever/contrib/$basearch/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
该文件中使用了两个变量:$releasever和$basearch,分别代表发行版本和体系结构。例如在32位CentOS 6.6上,它们的值分别是6和i386。注意releasever不会细分到次级版本(不会出现6.6),它只到主版本号。
从文件内容来看,共有5个软件源小节:base、updates、extras、centosplus和contrib。我们需要为每一个源都添加一个对应的源代码源。在文件末尾追加以下内容:
[base-src] name=CentOS-$releasever - Base src baseurl=https://vault.centos.org/6.6/os/Source/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [updates-src] name=CentOS-$releasever - Updates src-centos baseurl=https://vault.centos.org/6.6/updates/Source/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [extras-src] name=CentOS-$releasever - Extras src-centos baseurl=https://vault.centos.org/6.6/extras/Source/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [centosplus-src] name=CentOS-$releasever - Plus src-centos baseurl=https://vault.centos.org/6.6/centosplus/Source/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [contrib-src] name=CentOS-$releasever - Contrib src-centos baseurl=https://vault.centos.org/6.6/contrib/Soruce/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
不同版本的配置略有差异,但遵循几个通用原则:
- gpgkey与已有节中的gpgkey保持一致。
- 配置前可以先打开
https://vault.centos.org/看看具体目录结构,确认存在对应的Source子目录。 - baseurl中的版本号要对应实际系统版本,例如CentOS 6.6就用
6.6。
这段配置的作用是让yum及其相关工具能够找到源代码包。当之后用yumdownloader下载源代码时,实际上会执行以下动作:
- 下载
$baseurl/repodata/repomd.xml文件,如果比本地的新则更新。 - 从
$baseurl/SPackages/中下载对应的源代码rpm包。
2)找到ls命令的位置
使用which命令:
which ls
输出:/bin/ls
3)查询ls所属的rpm包
用rpm -qf命令:
rpm -qf /bin/ls
输出:coreutils-8.4-37.el6.i686
4)使用yumdownloader下载源代码rpm
yumdownloader是专门从yum源下载rpm包的工具(需要先安装yum-utils包)。执行:
yumdownloader --source coreutils-8.4-37.el6.i686
几点说明:
- 如果系统未安装
yumdownloader,先执行yum install yum-utils。 - 默认下载到当前目录,可以用参数
--destdir指定目标路径。
5)查看源代码rpm中包含的tar包
下载到的文件是coreutils-8.4-37.el6.src.rpm。用rpm -qpl列出内容并过滤出tar文件:
rpm -qpl coreutils-8.4-37.el6.src.rpm | grep tar
输出:coreutils-8.4.tar.xz
6)从src.rpm中提取tar.xz
使用rpm2cpio配合cpio命令:
rpm2cpio coreutils-8.4-37.el6.src.rpm | cpio -idv coreutils-8.4.tar.xz
输出:
coreutils-8.4.tar.xz 9561 blocks
参数说明:
-i或--extract:提取文件-d或--make-directories:必要时创建目录-v:显示处理过程
7)解压tar.xz得到源代码
最后一步,解压缩:
tar -xvf ./coreutils-8.4.tar.xz
至此,coreutils(其中包含ls命令)的完整源代码就全部准备好了。
