创建自己的 Yum repository

  Fedora 下采用 Yum 管理 RPM 之间的依赖, 同时可以从 Repositories 端解析到合适的 RPM 依赖包,使得我们安装软件非常的方便。 很多 Linux 下的软件发布都会考虑发布一个 Yum 的 repo 文件, 让用户更加方便的安装。 本文先介绍客户机如何使用第三方的 repo 文件, 最后介绍下如何创建自己的 yum repository。

使用第三方的 repo 文件

 我们以 virtualbox 为例, Oracle 为 virtualbox 发布提供了 repo 文件, 地址在:

http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo

首先,我们切换到 root 用户:

[lgao@lgao ~]$ su -
Password: 

 然后, 到 /etc/yum.repos.d/ 目录下, virtualbox.repo 文件 downlod 下来:

[root@lgao ~]# cd /etc/yum.repos.d/
[root@lgao yum.repos.d]# wget http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo
--2012-06-26 15:03:13--  http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo
Resolving download.virtualbox.org... 137.254.16.69
Connecting to download.virtualbox.org|137.254.16.69|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 238 [text/plain]
Saving to: “virtualbox.repo”

100%[==============================================================>] 238         --.-K/s   in 0s      

2012-06-26 15:03:14 (17.7 MB/s) - “virtualbox.repo” saved [238/238]

 之后, 我们可以采用 yum install VirtualBox 了:

 从上面的输出我们看到, 这个 VirtualBox-4.1 的来源 Repository 是: virtualbox。 我们来细看下 virtualbox.repo 文件内容:

[root@lgao yum.repos.d]# cat virtualbox.repo 
[virtualbox]
name=Fedora $releasever - $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/fedora/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc

其中:

  • [virtualbox]   为 repository 名称, 表明接下来的数据是针对这个 repository 的, 类似 ini 文件。 一个 repo 文件里可以有多个 [XXX],且全局唯一。
  • name=Fedora $releasever - $basearch - VirtualBox  该 repository 的 summary,全局唯一。 $releasever $basearch 是 yum 的变量, 具体含义请参考 man。 (command:  man 5 yum.conf)
  • baseurl=XXX   从哪里获取 pacakges 列表和 rpm 文件。
  • enabled=1   表示此 repo 启用了。
  • gpgcheck=1  表示需要检查 gpg 签名
  • gpgkey=XXX  用来检查 gpg 签名的 key 文件

创建自己的 Yum repository

  我们已经知道了如何使用第三方的 repo, 那么怎么创建自己的 yum repository 呢。我们从上一篇博客 中知道了如何创建一个 RPM 包, 我们就用这个 RPM 来演示下如何创建自己的 yum repository。

   第一步, 选定 yum repository 的 base 地址

[lgao@lgao ~]$ mkdir -p yum_repo/Packages

   我们选定 ~/yum_repo 作为 base 地址, 然后在里面创建一个 Packages 文件夹(可选,且文件夹名称随意)。

  第二步,  copy helloworld RPM 到 Packages 目录:

[lgao@lgao ~]$ tree yum_repo/
yum_repo/
`-- Packages
    `-- helloworld-1.0.0-1.fc16.i386.rpm

   这就是我们的目录结构。当然也可以简单的将 rpm 文件放在 ~/yum_repo 目录下。

  第三步,到 ~/yum_repo 目录下, 运行 createrepo 命令创建 yum repository:

[lgao@lgao ~]$ cd yum_repo/
[lgao@lgao yum_repo]$ createrepo .
Spawning worker 0 with 1 pkgs
Workers Finished
Gathering worker results

Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
 

到这里为止, 我们的 yum repository 就创建好了。  如果你的机器里没有  createrepo 命令, 那么请使用:

[lgao@lgao yum_repo]$ sudo yum install createrepo

 来安装。

第四步, 创建 helloworld.repo 文件:

[helloworld]
name=Hello World
baseurl=file:///home/lgao/yum_repo
enabled=1
gpgcheck=0

  把 hello.repo 文件放在 /etc/yum.repos.d/ 下, 运行:

我们运行下 helloworld :

[root@lgao yum.repos.d]# helloworld 
Hello World!

接下来我们看看如何在 yum repository 里升级 helloworld。

生成 helloworld-1.0.1-1.fc16.i386.rpm, 放进 ~/yum_repo/Packages 目录下, 删除 helloworld-1.0.0-1.fc16.i386.rpm, 重新执行 createrepo 命令:

[lgao@lgao yum_repo]$ createrepo .
Spawning worker 0 with 1 pkgs
Workers Finished
Gathering worker results

Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

然后执行:

[root@lgao yum.repos.d]# yum clean all
Loaded plugins: langpacks, presto, refresh-packagekit
Cleaning repos: helloworld:virtualbox
Cleaning up Everything
No delta-package files removed by presto

 之后, 再次运行

yum install helloworld

 系统会提示有新版本 helloworld,将进行 update。

之后我们再次运行 helloworld:

[root@lgao yum.repos.d]# helloworld 
Hello World in New Version!

 我们发现 helloworld 已经更新了。

 通过发布一个 repo 文件来发布一套软件让软件安装更加方便,同时也避免了最终用户安装时找不到依赖的问题。 希望越来越多的软件发布采用更加人性化的技术。

猜你喜欢

转载自aoingl.iteye.com/blog/1568160
今日推荐