yum package source making - downgoon/hello-world GitHub Wiki

内容提要:

  • AppStore 思想
  • 增加yum 源
  • 制作本地源
  • 制作内网源
  • 制作rpm软件包

AppStore 思想

在windows上,我们要装一个软件,怎么装? 我们得去 rj.baidu.com或360安全卫士下载,因为微软时代网络还不够流行,于是它没有做软件分发平台。在Mac中,我们装软件就直接去AppStore下载了,无论谁开发的Mac软件,都需要注册到AppStore,否则安装时就会报警告。

这种连接用户与软件的分发平台机制,给用户带来了非常大的方便。实际上在Liunx体系下,早就有这种分发机制与工具。

按软件包格式划分,Linux分两大类:.rpm包的类RedHat/Centos 和 .deb包的类Debian/Ubuntu。

rpm和deb包.png

我们先讲解 yum 的 rpm 包,下一篇讲解 apt-get 的 deb 包。

源配置&查看

  • 查看源
$ yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                                repo name                                                                                               status
!base/7/x86_64                                                         Packages for Enterprise Linux 7 - x86_64 - base                                                          9,007
!extras/7/x86_64                                                       Packages for Enterprise Linux 7 - x86_64 - extras                                                          317
!nginx/7/x86_64                                                        Packages for Enterprise Linux 7 - x86_64 - nginx                                                            79
  • 配置源

linux 的 yum源 都配置在 /etc/yum.repos.d 目录下:

$ ll /etc/yum.repos.d
-rw-r--r-- 1 root root 153 Mar 22 11:47 base.repo
-rw-r--r-- 1 root root 159 Mar 22 11:47 extras.repo
-rw-r--r-- 1 root root 156 Mar 22 11:47 nginx.repo
  • base源

看下base源的详细信息:

$ cat /etc/yum.repos.d/base.repo
[base]
name=Packages for Enterprise Linux $releasever - $basearch - base
baseurl=http://10.209.11.12/pub/$releasever/$basearch/base
enabled=1
gpgcheck=0

$ cat /etc/yum.repos.d/nginx.repo
[nginx]
name=Packages for Enterprise Linux $releasever - $basearch - nginx
baseurl=http://10.209.11.12/pub/$releasever/$basearch/nginx
enabled=1
gpgcheck=0

每个源可以单独一个文件,也可以把多个源写到一个文件里面。

字段 含义
[xxx] 源ID,不可重复。如上文的[base]和[nginx],完全自己给的名字
name 源名字,属于描述信息类
baseurl 源对应的下载地址,可以是 http://ftp://file://
enabled 是否启用:1表示启用;0表示禁用
gpgcheck gpg签名验证,类似AppStore下载的软件需要进行签名验证,否则会警告。但Linux是开放的,一般设置为0,表示不签名验证。如果 gpgcheck=1,需要提供证书地址,比如: gpgkey=file:/mnt/RPM-GPG-KEY-CentOS-7

读者可能已注意到 baseurl 中含有两个变量: baseurl=http://10.209.11.12/pub/$releasever/$basearch/base

  • $releasever: 表示 release ver (version),发行的版本,指的是操作系统的版本。比如 centos/7 等。
  • $basearch: 表示 base arch (architecture),基础体系结构,指的是CPU的体系结构。比如 os/x86_64os/x86_32 等。

完整的地址比如:

https://mirrors.aliyun.com/centos/7/os/x86_64/

用浏览器访问源地址:

浏览器访问源地址

  • 社区贡献源

**阿里云开源镜像站:http://mirrors.aliyun.com/ ** 搜狐开源镜像站:http://mirrors.sohu.com/ 网易开源镜像站:http://mirrors.163.com/ 北京理工大学:http://mirror.bit.edu.cn 厦门大学:http://mirrors.xmu.edu.cn/

阿里云温馨提醒

若使用阿里云服务器,将源的域名从 mirrors.aliyun.com 改为mirrors.aliyuncs.com , 不占用公网流量


本地源

有时候我们要在服务器上安装一个软件,比如 osquery,原本公网可以直接 yum install osquery 就搞定。但是由于网络管制的原因,服务器往往不许访问外网。如何建一个本地源,以便yum install osquery 就搞定 ? 当然你也可以 rpm -ivh osquery-2.3.2-1.el6.x86_64.rpm

没有外网源,安装 osquery 报告 Not Found:

$ yum search osquery
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Warning: No matches found for: osquery
No matches found

制作前,需要了解下 yum 原理:

yum 原理.png

上图表示,比如当我们 yum install osquery 时,首先查看本地缓存 /var/cache/yum/ ,如果没有,则去源看(配置在 /etc/yum.repos.d 目录)。 源提供两个信息:

  • (1)一个个的安装文件,比如rpm包;
  • (2)meta 信息,以便 yum 客户端 能快速查找。对应的目录是安装包所在目录下的子目录 repodata

所以,要搭建一个 yum 源需要做4件事:

  • 准备好 安装包: osquery-2.3.2-1.el6.x86_64.rpm
  • 生成 meta data: 借助工具 createrepo
  • 服务端:以HTTP, FTP 或 本地 File 提供下载。
  • 客户端:客户端添加一个源。

详细步骤:

  • 下载

比如下载centos7环境下的 osquery-2.3.2-1.el7.x86_64.rpm

$ wget https://osquery-packages.s3.amazonaws.com/centos7/osquery-2.3.2-1.el7.x86_64.rpm
  • 生成 meta
$ make -p /opt/local_repo   # 用 local_repo 目录存放安装包
$ cp osquery-2.3.2-1.el7.x86_64.rpm /opt/local_repo/   # 把安装包放入本地仓库目录
$ yum install createrepo    # 安装 createrepo 工具,以便生成meta
$ createrepo /opt/loca_repo/    # 生成 meta,针对 本地仓库目录
……
Spawning worker 7 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

$ ll  /opt/loca_repo/    # 多出了子目录 repodata , 里面存meta
-rw-r--r-- 1 root root 14662771 Mar 27 16:22 osquery-2.3.2-1.el6.x86_64.rpm
drwxr-xr-x 2 root root     4096 Mar 27 16:25 repodata
  • 服务端配置

如果是本地源,则直接用文件系统提供文件访问即可,无需http或ftp。

  • 客户端配置

/etc/yum.repos.d/ 目录下,增加一个配置文件 local.repo,内容如下:

$ cat /etc/yum.repos.d/local.repo
[local_repo]
name= local repo by downgoon
baseurl=file:/opt/loca_repo/
enabled=1
gpgcheck=0
  • 验证

查看谁(哪个源)能提供 osquery 的下载 ?

$ yum provides osquery
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
osquery-2.3.2-1.el6.x86_64 : osquery is an operating system instrumentation toolchain.
Repo        : local_repo  # id为local_repo的源
  • 安装
$ yum install osquery

=====================================================================================================================================================================================
 Package                                   Arch                                     Version                                       Repository                                    Size
=====================================================================================================================================================================================
Installing:
 osquery                                   x86_64                                   2.3.2-1.el6                                   local_repo                                    14 M

$ yum list installed | grep osq
osquery.x86_64                   2.3.2-1.el6                           @local_repo
  • 使用
$ osqueryi
osquery> select * from os_version;
+--------------+----------+-------+-------+-------+-------+----------+---------------+----------+
| name         | version  | major | minor | patch | build | platform | platform_like | codename |
+--------------+----------+-------+-------+-------+-------+----------+---------------+----------+
| CentOS Linux | 7 (Core) | 7     |       |       |       | centos   | rhel fedora   |          |
+--------------+----------+-------+-------+-------+-------+----------+---------------+----------+

osquery> .mode line
osquery> select * from system_info;
          hostname = VM-213072154.xxx.com
              uuid = CC24F4D9-31A8-4E61-AE49-30A583D3D48E
          cpu_type = 6
       cpu_subtype = 42
         cpu_brand = Intel Xeon E312xx (Sandy Bridge)
cpu_physical_cores = 8
 cpu_logical_cores = 8
   physical_memory = 8371900416
   hardware_vendor = Red Hat Inc.
    hardware_model = OpenStack Nova
  hardware_version = 2014.1.2-4.wo.el6
   hardware_serial = 37313930-3631-3643-5536-333358354533
     computer_name = VM-213072154.xxx.com


HTTP 源

  • 服务端配置

前面说了,服务端配置可以是 file,也可以是 http 或 ftp。 现在主流都是http,下面我们用 nginx 搭一个 http就是了。

在 nginx 上 增加一个 location,目录指向 /opt/local_repo/,开启目录浏览模块:


location /pub {
	alias /opt/local_repo/;
	autoindex on;
	autoindex_localtime on;
}

目录浏览.png

  • 客户端配置
$ cat /etc/yum.repos.d/local.repo
[local_repo]
name=local repo by downgoon
baseurl=http://10.213.72.154/pub/
enabled=1
gpgcheck=0

rpm 包制作

一般来说,包制作工具:

包格式 制作工具
.rpm rpmbuild
.deb dh_make

但是相对而言,它们太繁琐,于是有人做了个更加便捷的工具:fpm (7千多个star)

开源地址: https://github.com/jordansissel/fpm

有人说fpmFuck Package Management 的缩写,表达了传统方式下制作软件包的痛苦,并立志彻底解决这种痛苦。看看作者的宣言,我们就能感受到作者的心思:

The goal of fpm is to make it easy and quick to build packages such as rpms, debs, OSX packages, etc.

fpm, as a project, exists with the following principles in mind:

  • If fpm is not helping you make packages easily, then there is a bug in fpm.
  • If you are having a bad time with fpm, then there is a bug in fpm.
  • If the documentation is confusing, then this is a bug in fpm.

网友评论

网友评论:以前打包rpm是一个非常复杂的一件事情,自从有了fpm,打包rpm就和tar打包文件一样简单。


Tips

  • 使用包和开发者包

发布时,一般都会将软件的内容分为一般使用与开发使用 (development) 两大类。所以你才会常常看到有类似 pam-x.x.rpm 与 pam-devel-x.x.rpm 之类的档名啊!

比如安装nginx需要的依赖包:

$ yum -y install pcre-devel openssl-devel
  • 两大Linux派系:类RedHat 和 类Debian

按软件包格式划分,Linux分两大类:.rpm包的类RedHat 和 .deb包的类Debian。

rpm和deb包.png


参考资料