在公司内部使用国内的163、sohu等源的时候,发现在安装软件较多的时候,还是有点慢,目前公司内部的openstack平台上面跑了60多台vm实例,按照趋势还有增多的可能,确实需要搭建一套本地源。
很多搭建本地源的案例都是把发行DVD中的rpm、deb包拷贝到本地,然后通过http协议进行访问,DVD中的包都是没有更新过的,而openstack项目更新还是比较快的,尤其是ubuntu经常更新东西还是很多的,所以决定把国内的某个源拉一套到本地,然后每天定时同步更新。
1 搭建ubuntu 12.10源
在debian/ubuntu下有个神器叫apt-mirror,该工具可以让你镜像一部分或者全部发行版的apt sources到本地,apt-mirror具有以下特性:
* It uses a config similar to apts sources.list
* It's fully pool comply
* It supports multithreaded downloading
* It supports multiple architectures at the same time
* It can automatically remove unneeded files
* It works well on overloaded channel to internet
* It never produces an inconsistent mirror including while mirroring
* It works on all POSIX compliant systems with perl and wget
现在让我们把sohu的ubuntu 12.10的源mirror到本地,先安装apt-mirror工具:
apt-get install apt-mirror
/etc/cron.d/apt-mirror 设置每天什么时候同步更新内容
/etc/apt/mirror.list mirror相关信息的配置
由于源的内容比较多,而且比较重要,所有同步下来的源内容我们不放在本地磁盘,而是在共享存储上划出一块空间,挂载在宿主机上,这样在宿主机宕机或者硬盘故障的时候,可以使共享存储的内容在其他宿主机上继续发挥作用,本编中挂载在/opt/apt-source目录下。
mirror.list配置如下:
set base_path /opt/apt-source
set mirror_path $base_path/mirror
set skel_path $base_path/skel
set var_path $base_path/var
set cleanscript $var_path/clean.sh
set defaultarch <running host architecture>
set postmirror_script $var_path/postmirror.sh
set run_postmirror 0
set nthreads 40
set _tilde 0
deb-amd64 http://mirrors.sohu.com/ubuntu/ quantal main restricted universe multiverse
deb-amd64 http://mirrors.sohu.com/ubuntu/ quantal-backports main restricted universe multiverse
deb-amd64 http://mirrors.sohu.com/ubuntu/ quantal-proposed main restricted universe multiverse
deb-amd64 http://mirrors.sohu.com/ubuntu/ quantal-security main restricted universe multiverse
deb-amd64 http://mirrors.sohu.com/ubuntu/ quantal-updates main restricted universe multiverse
deb-i386 http://mirrors.sohu.com/ubuntu/ quantal main restricted universe multiverse
deb-i386 http://mirrors.sohu.com/ubuntu/ quantal-backports main restricted universe multiverse
deb-i386 http://mirrors.sohu.com/ubuntu/ quantal-proposed main restricted universe multiverse
deb-i386 http://mirrors.sohu.com/ubuntu/ quantal-security main restricted universe multiverse
deb-i386 http://mirrors.sohu.com/ubuntu/ quantal-updates main restricted universe multiverse
开始在后台同步sohu的源:
nohup apt-mirror -c /etc/apt/mirror.list &
同步完毕后,搭建web服务
apt-get install apache2
cd /var/www
ln -s /opt/apt-source/mirror/mirrors.sohu.com/ubuntu ubuntu
把/etc/apt/sources.list文件的内容替换成如下(10.1.6.6):
deb http://10.1.6.6/ubuntu quantal main multiverse restricted universe
deb http://10.1.6.6/ubuntu quantal-backports main multiverse restricted universe
deb http://10.1.6.6/ubuntu quantal-proposed main multiverse restricted universe
deb http://10.1.6.6/ubuntu quantal-updates main multiverse restricted universe
deb http://10.1.6.6/ubuntu quantal-security main multiverse restricted universe
如果在设置mirror.list时,源设置的是'deb http://',则默认是同步64位的包,但是有些包安装时需要32位的支持,所以还需要同步32位的源,就如上面配置文件设置。
更新apt数据库信息
apt-get update
2 搭建centos6源
在搞定完ubuntu之后,想按照上面的思路把centos也搞定,google一番后,没发现centos下有相似的工具,只能另换方法,决定直接从官方把整个源同步下来,到mirrors.kernel.org上一看,提供了三种方式mirror sources:HTTP、FTP、RSYNC,那就用rsync把它全部同步下来吧。
通过下面脚本进行同步
#!/bin/sh
#rsync -avzL --delete -stats rsync://mirrors.kernel.org/centos/6/ /opt/apt-source/centos6/
rsync="/usr/bin/rsync -avzL --delete"
mirror=rsync://mirrors.kernel.org/centos
ver=6
arch="x86_64 i386"
base="os updates centosplus isos fasttrack extras cr contrib"
local=/opt/apt-source/centos6
for arch in $arch
do
for base in $base
do
remote=$mirror/$ver/$base/$arch/
if [ ! -e $local/$base/$arch ]; then
mkdir -p "$local/$base/$arch"
fi
$rsync $remote $local/$base/$arch/
done
done
脚本放入后台执行即可,同步完毕后,用如下内容替换centos6的/etc/yum.repos.d/CentOS-Base.repo内容即可。
[base]
name=CentOS - Base
baseurl=http://10.1.6.6/centos/6/os/$basearch/
gpgcheck=1
gpgkey=http://10.1.6.6/centos/6/os/$basearch/RPM-GPG-KEY-CentOS-6
#released updates
[update]
name=CentOS - Updates
baseurl=http://10.1.6.6/centos/6/updates/$basearch/
gpgcheck=1
gpgkey=http://10.1.6.6/centos/6/os/$basearch/RPM-GPG-KEY-CentOS-6
#released extras
[extras]
name=CentOS - Extras
baseurl=http://10.1.6.6/centos/6/extras/$basearch/
gpgcheck=1
gpgkey=http://10.1.6.6/centos/6/os/$basearch/RPM-GPG-KEY-CentOS-6
#released CentOSPlus
[centosplus]
name=CentOS - CentOSPlus
baseurl=http://10.1.6.6/centos/6/centosplus/$basearch/
gpgcheck=1
gpgkey=http://10.1.6.6/centos/6/os/$basearch/RPM-GPG-KEY-CentOS-6
#contrib - packages by Centos Users
[contrib]
name=CentOS - Contrib
baseurl=http://10.1.6.6/centos/6/contrib/$basearch/
gpgcheck=1
gpgkey=http://10.1.6.6/centos/6/os/$basearch/RPM-GPG-KEY-CentOS-6
更新yum源缓存信息
yum clean all
yum makecache
来源:oschina
链接:https://my.oschina.net/u/123777/blog/113146