How to build a local Yumrepo server on CentOS 6

Posted by paul on 2015.03.29

How to build a local Yumrepo server on CentOS 6.x

2015.03.29 Sun

Overview

In this tutorial I describe how to set up a CentOS 6.6 Yum Repository. All you need to do is copy/paste 2 shell scripts and execute them. Once that's done, you copy a .repo file to your Yum Clients. That's all. I work with only x86_64 version only.

If you need a more thorough explanation on how to set up a local Yum Repo, please follow my 4-part series on setting up a local yumrepo .

Video demo

One minute video demo to show how easily you can create a yumrepo from scratch using my scripts. Obviously the whole process takes days to complete due to 10GB of data to download from a CentOS mirror server. By using my 2 scripts, the actual time you need to spend typing in front of your screen will be less than a few minutes. Watch in 720p if possible.

Terms

Some terms to know. I just came up with these for my own use.

  1. yumrepo: Usually known as Yum Repository. It's a place that contains rpms. A yumrepo can be accessed via http or ftp. I use http.
  2. Yumserver: It's a CentOS server that hosts rpms for yumclients to download when yum command is issued on the yumclient.
  3. Yumclient: Any CentOS server that downloads rpms from a yumrepo is a yumclient. Even a yumserver itself is a yumclient as it downloads rpms from a yumrepo.

Setting up Yumrepo for CentOS 6.6

Have a CentOS 6.6 server ready with about 100GB of storage space. A CentOS 6.6 Yum Repository with minimal RPMs (no i386 or ISO files) needs only about 14GB of storage. However you need about 12GB of extra space to shuffle files around. And having extra storage space allows adding additional Yum Repositories later with minimal disruption so I recommend starting out with about 100GB of storage space.

Assuming the new yumserver's hostname is yumserver01. If you use IP and not DNS for internal servers, replace update URL with correct IP.

  1. From your PC, ssh into your new yumserver.
  2. On your new yumserver, create 2 directories as root
  3. [[email protected] ~]# mkdir isos bin
    
  4. Download CentOS 6.6 x86_64 ISO files (both DVD1 and DVD2) to /root/isos/. You will need about 6GB of space.
  5. [[email protected] ~]# ls -lh /root/isos/
    total 5.6G
    -rw-r--r-- 1 root root 4.4G Feb 26 13:24 CentOS-6.6-x86_64-bin-DVD1.iso
    -rw-r--r-- 1 root root 1.3G Mar 18 10:02 CentOS-6.6-x86_64-bin-DVD2.iso
    
  6. Download yum-scripts.zip and unzip the content (2 scripts) to /root/bin/. You should see the 2 scripts in /root/bin/.
  7. [[email protected] ~]# ls /root/bin/ | sort
    yum-create-server-centos6.6.sh
    yum-rsync-minimal-centos6.6.sh
    
  8. Ensure root owns the ISO files.
  9. [[email protected] ~]# chown -R root:root /root/isos
    
  10. Ensure root owns the scripts and they are executable.
  11. [[email protected] ~]# chown -R root:root /root/bin
    [[email protected] ~]# chmod 750 -R /root/bin
    
  12. Execute yum-create-server-centos6.6.sh as root. It will take about 10-30min to complete.
  13. Following folder will be created and RPMs from the 2 ISO files will be copied in.
  14. [[email protected] ~]# du -hs /data/www/yumrpms/centos6.6/6.6/os/x86_64
    5.8G	/data/www/yumrpms/centos6.6/6.6/os/x86_64
    
  15. A link, 6, is created. It points to 6.6
  16. [[email protected] ~]# ls -lh /data/www/yumrpms/centos6.6/
    total 8.0K
    lrwxrwxrwx  1 apache apache    3 Mar 23 08:04 6 -> 6.6
    drwxrwxr-x 11 apache apache 4.0K Oct 19 21:36 6.6
    
  17. Use a web browser and test browsing http://yumserver01/yumrpms/centos6.6/6/os/x86_64/. Replace yumserver01 with your own FQDN or IP address of yumserver. You should see a webpage showing list of files and fodlers.
  18. You should also see this file on yumserver01: /root/bin/66.repo. The file should have following.
  19. # 66.repo
    # Change hostname to IP if you do not have DNS for local machines.
    [base]
    name=CentOS-$releasever - Base
    baseurl=http://yumserver01/yumrpms/centos6.6/$releasever/os/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=&arch=&repo=os
    #baseurl=http://mirror.centos.org/centos//os//
    gpgcheck=1
    gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
    
    #released updates
    [updates]
    name=CentOS-$releasever - Updates
    baseurl=http://yumserver01/yumrpms/centos6.6/$releasever/updates/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=&arch=&repo=updates
    #baseurl=http://mirror.centos.org/centos//updates//
    gpgcheck=1
    gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    baseurl=http://yumserver01/yumrpms/centos6.6/$releasever/extras/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=&arch=&repo=extras
    #baseurl=http://mirror.centos.org/centos//extras//
    gpgcheck=1
    gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus
    baseurl=http://yumserver01/yumrpms/centos6.6/$releasever/centosplus/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=&arch=&repo=centosplus
    #baseurl=http://mirror.centos.org/centos//centosplus//
    gpgcheck=1
    enabled=0
    gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
    #END of file
    
  20. If the yumserver01 is to be accessed via IP (ex: 192.168.1.20), in 66.repo replace the 4 instances of yumserver01 with the IP.
  21. ...
    baseurl=http://192.168.1.20/yumrpms/centos6.6/$releasever/os/$basearch/
    ...
    
  22. Now you just need to execute the 2nd script for the new yumrepo to be fully functional. Note that it will take days for the script to complete due to 10GB of data to rsync down. Hence you should start the script with & option at the end.
  23. [[email protected] ~]# yum-rsync-minimal-centos6.6.sh &
    
  24. The script, yum-rsync-minimal-centos6.6.sh, will always generate a short log at /root/logs/yum-rsync-minimal-centos6.6-yyyymm.log" Note all logs from each month will be kept in 1 log file. You should see following 4 lines AFTER yum-rsync-minimal-centos6.6.sh has finished running.
  25. [[email protected] ~]# more /root/logs/yum-rsync-minimal-centos6.6-201503.log
    Fri Mar 27 12:01:02 UTC 2015
    Rsync and createrepo commands started
    2015-03-27.12-09: Rsync was executed successfully.
    2015-03-27.12-48: Createrepo was executed successfully.
    
    [[email protected] ~]#
    
  26. Now the new yumrepo centos6.6 is fully functional.

Testing yumrepo centos6.6 with a yumclient

Using CentOS 6.6 server named 'lab01' as yumclient.

  1. yumserver01: copy yumserver01:/root/bin/66.repo --> lab01:/etc/yum.repos.d/66.repo.
  2. lab01: rename /etc/yum.repos.d/CentOS-Base.repo --> /etc/yum.repos.d/CentOS-Base.repo.orig.
  3. lab01: list files in /etc/yum.repos.d/ and you should see following:
  4. [[email protected] ~]# ls /etc/yum.repos.d/ | sort
    66.repo
    CentOS-Base.repo.orig
    CentOS-Debuginfo.repo
    CentOS-fasttrack.repo
    CentOS-Media.repo
    ...
    
  5. Run yum clean all and yum -y update
  6. [[email protected] ~]# yum clean all
    ...
    [[email protected] ~]# yum update
    ...
    
  7. As lab01 downloads the RPMs, you can tail httpd access_log on yumserver01 and you should see the files being downloaded.
  8. [[email protected] ~]# tail -f /var/log/httpd/access_log
    ...
    

Use yumupdate.sh on yumclients

Remember that in some cases, /etc/yum.repos.d/CentOS-Base.repo is recreated after running "yum update" (like when CentOS is upgraded from 6.5 --> 6.6). You should not leave CentOS-Base.repo in if you are using a local yumrepo server. Hence, I created a script to run "yum update" on my CentOS servers. The script will rename CentOS-Base.repo if found before and after. It also reboots the server if the kernel is updated.

  1. Create /root/bin/yumupdate.sh with following:
  2. #!/bin/bash
    NowDate=`date "+%Y%m%d-%H%M"`
    LogsYum="/root/logsyum"
    LogFile="yum-update-$NowDate.log"
    
    mkdir -p $LogsYum 2> /dev/null
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$NowDate 2> /dev/null
    yum clean all
    yum -y update 2>&1 | tee $LogsYum/$LogFile
    
    KernelTest=$(grep -ic "kernel" $LogsYum/$LogFile)
    
    if [ $KernelTest -ge 1 ]; then
        echo "# Kernel updated" >> $LogsYum/$LogFile
        mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$NowDate 2> /dev/null
        reboot
        exit 0
    else
        echo "# Kernel NOT updated." >> $LogsYum/$LogFile
        mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$NowDate 2> /dev/null
        exit 0
    fi
    
  3. Log is kept in /root/logsyum/yyyymmdd-hhmm.log.

Keep yumrepo centos6.6 in sync

New RPMs are often released to fix bugs or add features. Thus you need to remember to keep your yumrepo synced with latest RPMs. To do so, set cronjob to run yum-rsync-minimal-centos6.6.sh once every few days. You could get more sophisticated and have it send out email alert if RPMs are downloaded, prompting you to run yumupdate on your fleet of CentOS servers.

As mentioned at the beginning of this webpage, you can also follow my 4-part series on setting up a local yumrepo if you need more details.