搭建本地 ubuntu 12.04 和 ubuntu-cloud 镜像

我们知道,ubuntu 是通过 apt-get 这套包管理软件来安装软件的,比如你要安装 screen, apt-get install -y screen 就可以了,apt-get 就会从网络上的一个软件源里面把 screen 相关的包下载下来,然后安装。apt-get 是从网络端的软件源来安装的,所以在我们装好操作系统后,经常会有这么一步,选择最近的软件源,来替代 /etc/apt/sources.list 配置的默认的软件源,一般我们会用 ustc, 163, souhu 等国内比较快的源。

如果是个人电脑的话,这种方式没有问题,可是如果是公司有很多服务器的情况下,这种方式就不合适了。因为每台服务器升级安装软件都要单独从网络下载一份软件包,一方面由于公司网络带宽的限制,速度不会太快,另一方面,重复下载,浪费带宽。所以考虑在公司内部搭建一个 ubuntu 镜像,作为缓存或者代理,定时和网络上的镜像源同步,把公司里的电脑的源配置成本地源,这样就能既提升速度,又节省带宽。

下面是本人在给公司部署 OpenStack 时候搭建镜像源的过程:

镜像服务器
这里我们用到 apt-mirror
[code lang="bash"]
sudo apt-get install apt-mirror
[/code]

配置文件在 /etc/apt/mirror.list, 默认是下面的内容
[code lang="text"]
############# config ##################
#
# set base_path /var/spool/apt-mirror
#
# 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 20
set _tilde 0
#
############# end config ##############

deb http://archive.ubuntu.com/ubuntu precise main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu precise main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse

clean http://archive.ubuntu.com/ubuntu
[/code]
改成这样
[code lang="text"]
############# config ##################
#
# set base_path /var/spool/apt-mirror
#
# 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
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads 20
set _tilde 0
#
############# end config ##############

# Precise 64Bit Mirror
deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise main restricted universe multiverse
deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse

# Precise 32Bit Mirror
deb-i386 http://cn.archive.ubuntu.com/ubuntu precise main restricted universe multiverse
deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse

deb-src http://cn.archive.ubuntu.com/ubuntu precise main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse
clean http://cn.archive.ubuntu.com/ubuntu

deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main
deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main
deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main
deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main
clean http://ubuntu-cloud.archive.canonical.com/ubuntu
[/code]

如果不需要源文件的话,可以把 deb-src 给去掉, deb-amd64 是针对 64 位的包,deb-i386 是针对 32 位的包,根据需要可以删减。
[code lang="text"]
deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main
deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main
deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main
deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main
[/code]

这一段是针对 OpenStack G 版的, 可以根据需要换成相应的版本,ubuntu 官方现在提供 folsom, grizzly, havana 三个版本。

配置完成后,开一个 screen 窗口
[code lang="bash"]
sudo apt-mirror /etc/apt/mirror.list
[/code]
然后就可以去干别的事了(喝咖啡调代码什么的)
按我上面的配置同步完成后,一共有 144G 大小。

上面的是手动同步更新,可以设置 cron 任务自动同步更新
apt-mirror 在安装完成后会自动创建一个 cron 任务 /etc/cron.d/apt-mirror,
[code lang="text"]
#
# Regular cron jobs for the apt-mirror package
#
0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log
[/code]
把最后一行的注释去掉,表示每天早晨 4 点进行自动同步

安装 apache
[code lang="bash"]
sudo apt-get install apache2
sudo ln -s /var/spool/apt-mirror/mirror/cn.archive.ubuntu.com/ubuntu /var/www/ubuntu
sudo ln -s /var/spool/apt-mirror/mirror/ubuntu-cloud.archive.canonical.com/ubuntu /var/www/ubuntu-cloud
[/code]

其它服务器配置
其它 ubuntu 服务器就可以把 /etc/apt/sources.list 配置成这台镜像服务器啦!
mirrors.hengtiansoft.com 是我们的刚装好的镜像服务器
[code lang="text"]
#

# deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120817.3)]/ dists/precise/main/binary-i386/
# deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120817.3)]/ dists/precise/restricted/binary-i386/
# deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120817.3)]/ precise main restricted

#deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120817.3)]/ dists/precise/main/binary-i386/
#deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120817.3)]/ dists/precise/restricted/binary-i386/
#deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120817.3)]/ precise main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://mirrors.hengtiansoft.com/ubuntu/ precise main restricted
deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://mirrors.hengtiansoft.com/ubuntu/ precise-updates main restricted
deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://mirrors.hengtiansoft.com/ubuntu/ precise universe
deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise universe
deb http://mirrors.hengtiansoft.com/ubuntu/ precise-updates universe
deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://mirrors.hengtiansoft.com/ubuntu/ precise multiverse
deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise multiverse
deb http://mirrors.hengtiansoft.com/ubuntu/ precise-updates multiverse
deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://mirrors.hengtiansoft.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-backports main restricted universe multiverse

deb http://mirrors.hengtiansoft.com/ubuntu precise-security main restricted
deb-src http://mirrors.hengtiansoft.com/ubuntu precise-security main restricted
deb http://mirrors.hengtiansoft.com/ubuntu precise-security universe
deb-src http://mirrors.hengtiansoft.com/ubuntu precise-security universe
deb http://mirrors.hengtiansoft.com/ubuntu precise-security multiverse
deb-src http://mirrors.hengtiansoft.com/ubuntu precise-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner

## Uncomment the following two lines to add software from Ubuntu's
## 'extras' repository.
## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
# deb http://extras.ubuntu.com/ubuntu precise main
# deb-src http://extras.ubuntu.com/ubuntu precise main

deb http://mirrors.hengtiansoft.com/ubuntu-cloud precise-proposed/grizzly main
deb http://mirrors.hengtiansoft.com/ubuntu-cloud precise-updates/grizzly main
[/code]

apt-get update,更新下缓存
之后就可以 apt-get install -y xxxxx 啦,体验飞一般的速度~~~

参考:
create an apt-mirror with ubuntu 12.04 lt (要爬墙)
apt-mirror

7 thoughts on “搭建本地 ubuntu 12.04 和 ubuntu-cloud 镜像

  1. ubuntu

    请教我的镜像总是报错“Proceed indexes: [Ssh: 1: cannot open cn.archive.ubuntu.com/ubuntu//dists/precise-security/main/source/Sources.gz: No such file
    apt-mirror: can’t open index in proceed_index_gz at /usr/bin/apt-mirror line 445.”,不知是何原因。

    Reply

Leave a Reply to hj.ma Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>