自建本地yum源

在安装xen虚拟化的时候因为自己电脑网络的问题,下载一些yum源的RPM包甚是烦恼,于是狠心搭建一个本地的yum源。速度极好。支持自动同步互联网上主流仓库。

下面说说具体步骤。

1  同步互联网上的部分仓库,搭建yum源服务端

我主要用到的是centos的6和7版本的部分仓库目录,主要是os  updates  extras 和 virt ,其中virt是专门搞虚拟化的。

搭建NGINX服务器,用作一个下载服务器,传送门  https://peloo.net/?p=444

创建本地目录,我是在树莓派上挂载一个1T的移动硬盘提供存储服务,建立的目录如下,

pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/7/{os,extras,updates,virt}
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/6/{os,extras,updates,virt}
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/7/os/x86_64/
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/7/extras/x86_64/
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/7/updates/x86_64/
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/7/virt/x86_64/
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/6/os/x86_64/
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/6/extras/x86_64/
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/6/updates/x86_64/
pi@raspberrypi:/media/pi/Seagate_disk $ mkdir -p /media/pi/Seagate_disk/repo/centos/6/virt/x86_64/

接着联网同步仓库,

/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ /media/pi/Seagate_disk/repo/centos/7/os/x86_64/
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/7/extras/x86_64/ /media/pi/Seagate_disk/repo/centos/7/extras/x86_64/
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/ /media/pi/Seagate_disk/repo/centos/7/updates/x86_64/
/usr/bin/rsync -av –exclude=debug rsync://mirrors.ustc.edu.cn/epel/7/x86_64/ /media/pi/Seagate_disk/repo/epel/7/x86_64/
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/7/virt/x86_64/ /media/pi/Seagate_disk/repo/centos/7/virt/x86_64/
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /media/pi/Seagate_disk/repo/centos/6/os/x86_64/
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /media/pi/Seagate_disk/repo/centos/6/extras/x86_64/
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /media/pi/Seagate_disk/repo/centos/6/updates/x86_64/
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/virt/x86_64/ /media/pi/Seagate_disk/repo/centos/6/virt/x86_64/
/usr/bin/rsync -av –exclude=debug rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ /media/pi/Seagate_disk/repo/epel/6/x86_64/

同步好了后,就可以通过网页登录查看目录,如图。

 

2  配置yum repo文件

在你需要新建文件三个CentOS-Base.repo  xen.repo  epel.repo

[root@CentOS7-1804-01 ~]# #touch /etc/yum.repos.d/{CentOS-Base.repo,epel.repo,xen.repo}

配置好了以后的内容如下,

[root@CentOS7-1804-01 yum.repos.d]# pwd ; ll ; cat CentOS-Base.repo ;echo "+++++++++++++" ; cat epel.repo  ; echo "+++++++++++++" ; cat xen.repo  
/etc/yum.repos.d
total 28
drwxr-xr-x.   2 root root    63 Aug  1 11:02 .
drwxr-xr-x. 146 root root 12288 Aug  1 10:42 ..
-rw-r–r–    1 root root   467 Aug  1 11:02 CentOS-Base.repo
-rw-r–r–    1 root root   146 Aug  1 11:02 epel.repo
-rw-r–r–    1 root root   131 Aug  1 11:01 xen.repo
[os]
name=Extra Packages for Enterprise Linux 7 – $basearch
baseurl=http://172.17.2.176/pi/Seagate_disk/repo/centos/7/os/x86_64
enabled=1
gpgcheck=0

[extras]
name=Extra Packages for Enterprise Linux 7 – $basearch
baseurl=http://172.17.2.176/pi/Seagate_disk/repo/centos/7/extras/x86_64
enabled=1
gpgcheck=0

[updates]
name=Extra Packages for Enterprise Linux 7 – $basearch
baseurl=http://172.17.2.176/pi/Seagate_disk/repo/centos/7/updates/x86_64
enabled=1
gpgcheck=0
+++++++++++++
[epel]
name=Extra Packages for Enterprise Linux 7 – $basearch
baseurl=http://172.17.2.176/pi/Seagate_disk/repo/epel/7/x86_64
enabled=1
gpgcheck=0
+++++++++++++
[xen4centos]
name=Xen4 for CentOS 7
baseurl=http://172.17.2.176/pi/Seagate_disk/repo/centos/7/virt/x86_64/xen
enabled=1
gpgcheck=0

3  编写定时同步仓库的脚本

因为仓库随时可能变化,自己编写了个脚本,每隔4小时同步一次仓库,同步机制是和服务器保持完全一致,服务器上删除的本地也删除。

且每次运行脚本,同步三次,因为原仓库有禁止ip并发个数的限制,多试几次成功率高一些。 脚本内容如下,

#!/bin/sh

rsync_centos() {

for((i=1; i<4; i++))
do

/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ /media/pi/Seagate_disk/repo/centos/7/os/x86_64/
/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/7/extras/x86_64/ /media/pi/Seagate_disk/repo/centos/7/extras/x86_64/
/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/ /media/pi/Seagate_disk/repo/centos/7/updates/x86_64/
/usr/bin/rsync -av –delete –exclude=debug rsync://mirrors.ustc.edu.cn/epel/7/x86_64/ /media/pi/Seagate_disk/repo/epel/7/x86_64/
/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/7/virt/x86_64/ /media/pi/Seagate_disk/repo/centos/7/virt/x86_64/

/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /media/pi/Seagate_disk/repo/centos/6/os/x86_64/
/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /media/pi/Seagate_disk/repo/centos/6/extras/x86_64/
/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /media/pi/Seagate_disk/repo/centos/6/updates/x86_64/
/usr/bin/rsync -av –delete rsync://mirrors.ustc.edu.cn/centos/6/virt/x86_64/ /media/pi/Seagate_disk/repo/centos/6/virt/x86_64/
/usr/bin/rsync -av –delete –exclude=debug rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ /media/pi/Seagate_disk/repo/epel/6/x86_64/

ping -c4 127.0.0.1 >/dev/null 2>&1

done

}

rsync_centos

exit 0

 

此时,测试一下,

[root@CentOS7-1804-01 ~]# yum remove -y ftp
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
–> Running transaction check
—> Package ftp.x86_64 0:0.17-67.el7 will be erased
–> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                              Arch                                    Version                                       Repository                            Size
=======================================================================================================================================================================
Removing:
 ftp                                  x86_64                                  0.17-67.el7                                   @os                                   96 k

Transaction Summary
=======================================================================================================================================================================
Remove  1 Package

Installed size: 96 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : ftp-0.17-67.el7.x86_64                                                                                                                              1/1
  Verifying  : ftp-0.17-67.el7.x86_64                                                                                                                              1/1

Removed:
  ftp.x86_64 0:0.17-67.el7                                                                                                                                             

Complete!
[root@CentOS7-1804-01 ~]# yum install ftp -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package ftp.x86_64 0:0.17-67.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                              Arch                                    Version                                        Repository                           Size
=======================================================================================================================================================================
Installing:
 ftp                                  x86_64                                  0.17-67.el7                                    os                                   61 k

Transaction Summary
=======================================================================================================================================================================
Install  1 Package

Total download size: 61 k
Installed size: 96 k
Downloading packages:
ftp-0.17-67.el7.x86_64.rpm                                                                                                                      |  61 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ftp-0.17-67.el7.x86_64                                                                                                                              1/1
  Verifying  : ftp-0.17-67.el7.x86_64                                                                                                                              1/1

Installed:
  ftp.x86_64 0:0.17-67.el7                                                                                                                                             

Complete!
[root@CentOS7-1804-01 ~]#
[root@CentOS7-1804-01 ~]#
[root@CentOS7-1804-01 ~]# yum remove -y lftp
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
–> Running transaction check
—> Package lftp.x86_64 0:4.4.8-8.el7_3.2 will be erased
–> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                              Arch                                   Version                                         Repository                           Size
=======================================================================================================================================================================
Removing:
 lftp                                 x86_64                                 4.4.8-8.el7_3.2                                 @os                                 2.4 M

Transaction Summary
=======================================================================================================================================================================
Remove  1 Package

Installed size: 2.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : lftp-4.4.8-8.el7_3.2.x86_64                                                                                                                         1/1
  Verifying  : lftp-4.4.8-8.el7_3.2.x86_64                                                                                                                         1/1

Removed:
  lftp.x86_64 0:4.4.8-8.el7_3.2                                                                                                                                        

Complete!
[root@CentOS7-1804-01 ~]#
[root@CentOS7-1804-01 ~]#
[root@CentOS7-1804-01 ~]# yum install -y lftp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package lftp.x86_64 0:4.4.8-8.el7_3.2 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                              Arch                                   Version                                          Repository                          Size
=======================================================================================================================================================================
Installing:
 lftp                                 x86_64                                 4.4.8-8.el7_3.2                                  os                                 751 k

Transaction Summary
=======================================================================================================================================================================
Install  1 Package

Total download size: 751 k
Installed size: 2.4 M
Downloading packages:
lftp-4.4.8-8.el7_3.2.x86_64.rpm                                                                                                                 | 751 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : lftp-4.4.8-8.el7_3.2.x86_64                                                                                                                         1/1
  Verifying  : lftp-4.4.8-8.el7_3.2.x86_64                                                                                                                         1/1

Installed:
  lftp.x86_64 0:4.4.8-8.el7_3.2                                                                                                                                        

Complete!

 

很好,没毛病。速度上来了,就是节省时间,不浪费生命。

 

发表评论

电子邮件地址不会被公开。