您的位置:首页 > 其它

21.文件系统――yum的工作原理以及如何建立yum仓库

2014-08-29 21:52 288 查看
一、yum的工作原理
yum是一个RPM包的前端管理工具,在rpm包的依赖关系已经被建成数据库的前提下,它能够实现自动查找相互依赖的rpm包,并从repository中下载互相依赖的rpm包到本地。




二、如何建立yum的仓库repository
如果yum和repository不在同一台主机上,那么能够为yum提供repository仓库服务的远程服务器协议通常有两类:(1)ftp://(2)http://如果yum和repository在同一台主机上,那么yum就可以直接到某个目录中提取rpm包,而为了和远程服务器形成统一的风格,也会使用一个传输协议:file:///,因此如果仓库建在根目录/下,那么就可以使用file:///yum/这个路径来提取文件。
yum仓库的建立,需要分成服务端和客户端两部分:A.服务端 将所有需要用到的rpm包存放于某个目录之下,该目录可以是远程的(采用ftp和http协议),也可以是本地的(采用file协议)。目录建立好了之后,使用createrpo命令来提取目录下所有rpm包的元数据和依赖关系,生成一些xml格式的文件,这些xml文件会存放在repodata目录下,至此,服务器端的repository就建立好了。注意repodata所在的位置,就是yum仓库的位置(主要针对红帽6,红帽5的repository会分成4个目录,每个目录下都有一个repodata)。
B. 客户端 客户端的配置非常简单,只需要使用yum及其子命令install、reinstall或remove即可。不过使用yum及其子命令之前,需要编辑配置文件,以告知yum命令仓库repository在什么地方。这个配置文件为:/etc/yum.conf。此外/etc/yum.repos.d/*.repo都被视为/etc/yum/conf的组成部分。下面来查看一下/etc/yum.conf: [root@localhost ~]# vim/etc/yum
yum/         yum.conf     yum.repos.d/
观察yum.conf文件,其中中括号是用来做仓库定义的,中括号里的内容,即为仓库的名称,但是main除外,main是为其他仓库提供默认配置的。仓库可以有多个,但是main(全局配置)只有一个 [root@localhost ~]# vim/etc/yum.conf
[main]
# 全局配置,为其他仓库定义默认值
cachedir=/var/cache/yum/$basearch/$releasever
# 缓存目录为/var/cache/yum/$basearch/$releasever
keepcache=0
# rpm包是否需要在本地长久保存
debuglevel=2
# 调试级别,运行过程中的信息是否需要显示
logfile=/var/log/yum.log
# 日志文件,记录yum命令安装过的文件,执行过的操作等等,供以后检索使用
exactarch=1
# 下载的软件包是否需要和本地平台完全匹配(1表示是,0表示否)
obsoletes=1
gpgcheck=1
# 下载rpm包之前是否需要自动进行来源(签名)合法性检测,1表示要检查。
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

# This is the default, if you make this bigger yum won't see if the
# metadata is newer on the remote and soyou'll "gain" the bandwidth of not# having to download the new metadata and"pay" for it by yum not having
# correct information.
# It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep oldpackages around. If you don't like this
# checking interupting your command lineusage, it's much better to have
# something manually check the metadataonce an hour (yum-updatesd will
# do this). metadata_expire=90m
# PUT YOUR REPOS HERE OR INseparate files named file.repo
# in /etc/yum.repos.d

如果要定义yum的仓库,可以在/etc/yum.repos.d下新建一个以repo结尾的文件:

[root@localhost ~]# cd/etc/yum.repos.d/
[root@localhost yum.repos.d]#ls
CentOS-Base.repo  CentOS-Debuginfo.repo
CentOS-Media.repo  CentOS-Vault.repo
# CentOS-Base.repo是最终会生效的配置文件,建议先备份,然后对其进行修改

配置文件的主要内容有:
[Repository_ID] ――仓库的ID,可以取任意名字,只要不和其他的ID冲突即可name=... ――仓库的描述信息,长短不限,可以有空格,但是必不可少baseurl=url ――告诉yum客户端,通过哪种路径可以访问到enabled={1|0} ――是否启用这个仓库,0表示不启用,1表示启用,默认是启用的gpgcheck=1{1|0} ――是否进行签名合法性检测,0表示不启用,1表示启用,默认启用。 如果选择启用gpg检查,则需要告知其key是什么。gpgkey=url ――如果启用gpg检测,则需要指定gpgkey的路径,即使导入过gpgkey, 这里仍然需要手动为其指定路径,这个路径可以是远程服务器上的, 也可以是本地的,只要让本地客户端访问到即可。cost=1000 ――访问的代价,定义了cost的,且数字较小的,会被优先访问。
如果两个仓库里的RPM包是一样的,一个在远程服务器上,另一个在本地光盘上,那么本地光盘的访问速度通常会快于远程服务器上。在配置文件中,我们可以定义这样的两个仓库,为其中一个设定优先级 下面来配置一个仓库。为了避免其他的仓库扰乱当前要定义的仓库,在配置之前,先编辑一下原有的CentOS-Base.repo文件:[root@localhost yum.repos.d]#vim CentOS-Base.repo
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
enabled=0
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
enabled=0
# 将这个仓库禁用即可

[root@localhost yum.repos.d]#vim base.repo
[base]
name=CentOS 6.4 x86_64
baseurl=http://mirror.centos.org/centos/6/os/x86_64/
enabled=1
gpgcheck=0

下面来检测已经配置好的repository是否可以正常使用:

[root@localhost yum.repos.d]#yum list all | less
# list命令可以用来查看仓库里的程序包,后文会详解,这里只做检测用
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror, refresh-packagekit, security
Repository base is listed more than once in theconfiguration
Loading mirror speeds from cached hostfile
Installed Packages
ConsoleKit.x86_64   0.4.1-3.el6 @anaconda-CentOS-201311272149.x86_64/6.5
# 软件包名称.平台版本  版本号-发行号 @anaconda表示系统已经安装了,来自于那个yum仓库
ConsoleKit-libs.x86_64 0.4.1-3.el6 @anaconda-CentOS-201311272149.x86_64/6.5
ConsoleKit-x11.x86_64  0.4.1-3.el6 @anaconda-CentOS-201311272149.x86_64/6.5
apr.i686                 1.3.9-5.el6_2                    base
# base表示该程序在base仓库中,还没有安装
apr-devel.i686           1.3.9-5.el6_2                    base
apr-devel.x86_64         1.3.9-5.el6_2                    base
apr-util.i686            1.3.9-3.el6_0.1                  base

可以使用yum命令查看repository里的包,说明仓库已经建好了。

三、使用安装光盘来建立yum repository (1). 挂载光盘来建立repository红帽5和红帽6的安装光盘本身就是一个yum repository,挂载到本地就可以直接使用 [root@localhost ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sdb3 on /mydata type ext4 (rw,noatime)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sr0 on /media/cdrom type iso9660 (ro)
# 安装光盘已经挂载了,如果使用的是虚拟机,可以将安装光盘导入到虚拟机中;如果是物理设备,则直接将光盘插入光驱即可。
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)

[root@localhost ~]# cd /media/cdrom[root@localhost cdrom]# ls
CentOS_BuildTag  isolinux                  RPM-GPG-KEY-CentOS-Debug-6
EFI              Packages                  RPM-GPG-KEY-CentOS-Security-6
EULA             RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Testing-6
GPL              repodata                  TRANS.TBL
images           RPM-GPG-KEY-CentOS-6

[root@localhost cdrom]# yum clean all
# 清空yum repository的缓存
Loaded plugins: fastestmirror, refresh-packagekit, security
Cleaning repos: base
Cleaning up Everything
Cleaning up list of fastest mirrors

[root@localhost cdrom]# cd /etc/yum.repos.d/[root@localhost yum.repos.d]# ls
base.repo  CentOs-Base.repo.bk  CentOS-Debuginfo.repo  CentOS-Media.repo  CentOS-Vault.repo

[root@localhost yum.repos.d]# vim base.repo # 修改yum仓库的配置文件,将repository的路径指向光盘
[base]
#name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
#gpgcheck=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

name=CentOS 6.4 x86_64
# baseurl=http://mirror.centos.org/centos/6/os/x86_64/
baseurl=file:///media/cdrom/
# 注意,如果是file,需要使用3条反斜线
enabled=1
gpgcheck=0

[root@localhost yum.repos.d]# yum makecache
# 建立yum缓存
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
base                                                                 | 4.0 kB     00:00 ...
base/group_gz                                                        | 220 kB     00:00 ...
base/filelists_db                                                    | 5.8 MB     00:00 ...
base/primary_db                                                      | 4.4 MB     00:00 ...
base/other_db                                                        | 2.7 MB     00:00 ...
Metadata Cache Created

[root@localhost yum.repos.d]# yum repolist
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
repo id                               repo name                                       status
base                                  CentOS 6.4 x86_64                               6,367
# 可以看到base repository依然存在
repolist: 6,367

(2). 指定repository的优先级 [root@localhost yum.repos.d]# vim myrepository.repo
# 新建一个repo文件,来创建第二个repository

[cdrom-centos6]
name=CentOS 6.4 x86_64
baseurl=file:///media/cdrom/
enabled=1
gpgcheck=0

[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, refresh-packagekit, security
Repository 'base' is missing name in configuration, using id
Cleaning repos: base centos6 cdrom-centos6
Cleaning up Everything
Cleaning up list of fastest mirrors

[root@localhost yum.repos.d]# yum repolist
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
base                                                                            | 4.0 kB     00:00 ...
base/primary_db                                                                 | 4.4 MB     00:00 ...
cdrom-centos6                                                                   | 4.0 kB     00:00 ...
cdrom-centos6/primary_db                                                        | 4.4 MB     00:00 ...
repo id                                       repo name                                          status
base                                          CentOS 6.4 x86_64                                  6,367
cdrom-centos6                                 CentOS 6.4 x86_64                                  6,367
repolist: 12,734
# 可以看到已经创建了两个仓库:base和 cdrom-centos6
repolist: 0
[root@localhost yum.repos.d]# yum clean all
# 清除缓存
Loaded plugins: fastestmirror, refresh-packagekit, security
Cleaning repos: base cdrom-centos6
Cleaning up Everything
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
base                                                                            | 4.0 kB     00:00 ...
base/group_gz                                                                   | 220 kB     00:00 ...
base/filelists_db                                                               | 5.8 MB     00:00 ...
base/primary_db                                                                 | 4.4 MB     00:00 ...
base/other_db                                                                   | 2.7 MB     00:00 ...
cdrom-centos6                                                                   | 4.0 kB     00:00 ...
cdrom-centos6/group_gz                                                          | 220 kB     00:00 ...
cdrom-centos6/filelists_db                                                      | 5.8 MB     00:00 ...
cdrom-centos6/primary_db                                                        | 4.4 MB     00:00 ...
cdrom-centos6/other_db                                                          | 2.7 MB     00:00 ...
Metadata Cache Created

[root@localhost yum.repos.d]# yum install bind
# 安装bind包
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package bind.x86_64 32:9.8.2-0.17.rc1.el6_4.6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================
Package           Arch                Version                                 Repository         Size
==============================================================================
Installing:
bind              x86_64              32:9.8.2-0.17.rc1.el6_4.6               base              4.0 M
# 这里显示是从base这个仓库中安装的
Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 4.0 M
Installed size: 7.3 M
Is this ok [y/N]: N

如果要改变安装的仓库,可以通过cost来改变优先级 [root@localhost yum.repos.d]# vim base.repo
[base]
name=CentOS 6.4 x86_64
baseurl=http://mirror.centos.org/centos/6/os/x86_64/
baseurl=file:///media/cdrom/
#enabled=1
#gpgcheck=0
cost=2000
# base仓库的cost值为2000

[root@localhost yum.repos.d]# vim myrepository.repo
[cdrom-centos6]
name=CentOS 6.4 x86_64
baseurl=file:///media/cdrom/
enabled=1
gpgcheck=0
cost=1000
# dcrom-centos6的cost值设为1000,其优先级将高于base仓库

[root@localhost yum.repos.d]# yum install bind
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
cdrom-centos6                                                                   | 4.0 kB     00:00 ...
base                                                                            | 4.0 kB     00:00 ...
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package bind.x86_64 32:9.8.2-0.17.rc1.el6_4.6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================
Package         Arch              Version                              Repository                Size
=================================================================================
Installing:
bind            x86_64            32:9.8.2-0.17.rc1.el6_4.6            cdrom-centos6            4.0 M
# 再次安装bind包,会发现repository已经变成了cdrom-centos6
Transaction Summary
=================================================================================
Install       1 Package(s)

Total download size: 4.0 M
Installed size: 7.3 M
Is this ok [y/N]: N

四、使用内置变量来建立yum仓库以http://mirrors.sohu.com/centos/为例,如果出现了centos6.6,则centos6.5镜像的目录将会清空,因此不建议写成:baseurl=http://mirrors.sohu.com/centos/6.5/os/x86_64/

该目录是空的

rpm包只存在于最高版本中,如果一定要写上发行号,则建议写成6,而不是6.5, 因为6最终指向的是最高发行版本。 事实上,建立yum仓库时可以使用内置变量,官方给出的例子在/etc/yum.repos.d/CentOs-Base.repo.bk文件中 [root@localhost ~]# cd /etc/yum.repos.d[root@localhost yum.repos.d]# ls
base.repo            CentOS-Debuginfo.repo  CentOS-Vault.repo
CentOs-Base.repo.bk  CentOS-Media.repo      myrepository.repo

[root@localhost yum.repos.d]# vim CentOs-Base.repo.bk
# 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=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
# 这里的$releasever 和 $basearch都属于内置变量,$releasever为当前主发行分支,$baseaxrch为本机的版本,即x86或x86_64
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://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=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
#baseurl=http://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=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://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=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

yum的内置变量主要有:$releaseverThis is replaced with the package's version, as listed in distroverpkg. This defaults to the version of the redhat-release package. ―― 用distroverpkg中列出的版本号来替换,即主发行号,不包括子发行号 $archThis is replaced with your system's architecture, as listed by os.uname() in Python.――平台版本,即x86_64 或 i386 使用uname -a可以查看当前系统的版本:[root@localhost yum.repos.d]# uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

使用uname -m可以显示主机平台[root@localhost yum.repos.d]# uname -m
x86_64

$basearchThis is replaced with your base architecture. For example, if $arch=i686 then $basearch=i386. ―― 取得平台的基准版本,64位系统的basearch是x86_64,而是32位系统可以是i386,i586和i686,但它们共同属于i386系列,故basearch为i386 $YUM0-9This is replaced with the value of the shell environment variable of the same name. If the shell environment variable does not exist, then the configuration file variable will not be replaced. ――yum内置的,用来进行系统调用的变量,很少用到。 故如果要写一个baseurl,可以这样操作: baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch

本文出自 “重剑无锋 大巧不工” 博客,请务必保留此出处http://wuyelan.blog.51cto.com/6118147/1546674
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐