美文网首页Linux
本地/局域网YUM源搭建、自定义分组

本地/局域网YUM源搭建、自定义分组

作者: HughCn | 来源:发表于2018-08-10 12:38 被阅读0次

    环境:CentOS 7.5.1804 Core root 用户

    背景:

    部分客户端采用linux系统,在部署时,没有公网环境,安装软件时,不容易解决依赖问题。

    目标:

    • 搭建本地的专用YUM源,仅存放项目中需要用到的rpm包(第三方及依赖和内部研发)。
    • 源内的软件自定义分组
    • 需要在win下搭建源的http服务

    设计如下:

    • 使用VirtualBox安装CentOS 7.5.1804 Core(虚拟机网卡配置为桥接网卡)作为rpm包的下载机。以下简称:源下载机
    • windows下使用nginx作为http服务(绿色,相对路径,方便携带)
      nginx目录结构如下:
    2018/08/09  16:58    <DIR>          .
    2018/08/09  16:58    <DIR>          ..
    2018/08/09  10:13    <DIR>          conf
    2018/08/09  10:13    <DIR>          contrib
    2018/08/09  10:13    <DIR>          docs
    2018/08/09  16:58    <DIR>          logs
    2018/08/09  16:58    <DIR>          nginx
    2018/08/09  10:13    <DIR>          temp
    2018/08/09  16:41    <DIR>          www
    2018/05/18  10:17         3,063,296 nginx.exe
    2018/08/09  10:11                21 reload.bat
    2018/08/09  10:12                20 start.bat
    2018/08/09  10:12                19 stop.bat
                   4 个文件      3,063,356 字节
                   9 个目录 96,812,961,792 可用字节
    

    3个bat文件是分别是开启,停止,重新加载配置文件。
    start.bat

    start "" "nginx.exe"
    

    stop.bat

    nginx.exe -s quit
    

    reload.bat

    nginx.exe -s reload
    

    其中www为web访问的目录,www目录结构如下

    2018/08/09  16:41    <DIR>          .
    2018/08/09  16:41    <DIR>          ..
    2018/08/09  16:46    <DIR>          ClientScripts
    2018/08/09  13:14    <DIR>          PHRepo
                   0 个文件              0 字节
                   4 个目录 96,812,961,792 可用字节
    

    ClientScripts 为存放的本地源repo创建模板,方便客户机部署。
    PHRepo 为本地源目录,该目录下结构为CentOS/7/x86_64,按照 <发行版/版本号/架构> 命名,方便后续扩展。

    源下载机搭建

    本例中没有直接使用的虚拟机的硬盘作为存储目录(占用空间多,来回COPY麻烦),而是用将物理机的文件夹进行共享,源下载机直接对该共享目录进行源的createrepo操作。

    1. mount共享目录

    将www下PHRepo目录共享: \\192.168.1.221\PHRepo\ ,权限为username 可读写
    然后在源下载机下进行mount

    mkdir /PHRepo
    mount -t cifs -o username="username",password="password",iocharset="utf8",nobrl //192.168.1.221/PHRepo /PHRepo 
    

    为了以后使用方便,将上面的命令改成了脚本,以后使用时只需要执行一下。
    mntPHRepo.sh

    #!/bin/sh
    USERNAME="username"
    PASSWORD="password"
    IOCHARSET="utf8"
    SMBPATH="//192.168.1.221/PHRepo/"
    MOUNTPATH="/PHRepo"
    
    mount -t cifs -o username="${USERNAME}",password="${PASSWORD}",iocharset="${IOCHARSET}",nobrl ${SMBPATH} ${MOUNTPATH} && echo "mount SMB success" || echo "mount SMB Fail"
    
     ./mntPHRepo.sh
    

    2.使用yum install --downloadonly --downloaddir= 进行离线下载不安装。

    yum 下离线下载有多种方式,downloadonly插件、yumdownloader、缓存等。这里只用了自带的 downloadonly 方式。其他可百度 一堆一堆的。
    phyumdown.sh

    #!/bin/bash
    
    DOWNLOADDIR="/PHRepo/centos/7/os/x86_64"
    RPMNAMES=$@
    
    usage(){
            echo "Usage: $0 rpmName1 rpmName2 rpmNameN"
    }
    
    download(){
            yum install --downloadonly --downloaddir=${DOWNLOADDIR} ${RPMNAMES} && echo "Download ${RPMNAMES} Success!" || echo "Download @{RPMNAMES} Fail!"
    }
    
    if [[ $# -eq 0 ]]; then
            usage
    else
            if [[ -d ${DOWNLOADDIR} ]]; then
                    download
            else
                    echo "DownloadDir ${DOWNLOADDIR} not exists! Download Fail!"
            fi
    fi
    

    使用时,脚本名后面接rpm的名称,多个用空格分开。

    ./phyumdown.sh vim wget bzip2
    

    ps:脚本里面的DOWNLOADDIR文件夹路径需要手动创建下,这个文件夹结构是按照版本、架构来创建的,便于后期扩展。

    3.创建分组

    创建分组文件 phcomps.xml 格式如下

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
    <comps>
    
      <group>
       <id>basic</id>
       <default>false</default>
       <uservisible>true</uservisible>
       <display_order>1024</display_order>
       <name>Basci Group</name>
       <description></description>
        <packagelist>
          <packagereq type="mandatory">bzip2</packagereq>
          <packagereq type="mandatory">wget</packagereq>
        </packagelist>
      </group>
    </comps>
    

    id:组的id仅仅是在comps.xml文件中作为该组的一个标识,这是必须的;
    name:表示用户可以看到的组的名字,它也是必须的;
    default:它表示在系统安装过程中,当选择定制(custom)安装时,该组是否在缺省情况下被选中。如果没有说明,则缺省情况下为不选中。
    uservisible:它表示该组在缺省情况下是否在安装时可以看到,如果没有说明,缺省设置为YES,为可以看到。
    description:它表示对该组进行简短的描述,这是必须的;
    packagelist:它说明在该组内的一系列安装包,这也是必须的。
    packagereq:包名
    属性:
    type:当进行安装时,判定对应的包是否是组的"强制"部分、或"缺省"部分或"可选"部分。它可以是"mandatory"、"default"或"optional"之一。
    requires:它说明只有当它所需要(依赖)的包也安装情况下,此包才安装进系统。
    参考文章:
    http://yum.baseurl.org/wiki/YumGroups
    https://www.ibm.com/developerworks/cn/linux/l-custm/index.html

    4.创建repo

    创建源需要使用命令 createrepo ,这个需要单独安装,并且有依赖包,在安装前需要先离线现在到repodata目录去,然后在执行 yum install createrepo。 原因见下方:踩坑

    安装好后,就可以进行创建操作了。封装脚本如下:
    phyumcreate.sh

    #!/bin/bash
    
    #############################################################
    #    clean .repodata
    #    createrepo
    #       ./repodata exists -> --update
    #       phcomps.xml exists -> --groupfile phcomps.xml
    #    --pretty
    #    ${DOWNLOADDIR}
    #
    #                                       made by Hugh 180808
    #############################################################
    
    DOWNLOADDIR="/PHRepo/centos/7/os/x86_64"
    
    REPODATADIR="${DOWNLOADDIR}/repodata"
    TMPREPODATADIR="${DOWNLOADDIR}/.repodata"
    
    PHGROUPXML="phcomps.xml"
    CMD="createrepo"
    
    # check begin
    if [[ ! -d ${DOWNLOADDIR} ]]; then
            echo "DownloadDir ${DOWNLOADDIR} not exists! Script will exit!" && exit
    fi
    
    
    if [[ -d ${TMPREPODATADIR} ]] ; then
            rm -rf ${TMPREPODATADIR} && echo "rm ${TMPREPODATADIR} success!" || (echo "rm ${TMPREPODATADIR} fail! Script will exit!" && exit)
    fi
    
    # end check
    
    # join cmd
    
    if [[ -d ${REPODATADIR} ]] ; then
            CMD="${CMD} --update"
    fi
    
    
    if [[ -e ${DOWNLOADDIR}/${PHGROUPXML} ]] ; then
            CMD="${CMD} --groupfile ${PHGROUPXML}"
    fi
    
    CMD="${CMD} --pretty ${DOWNLOADDIR}"
    
    echo "the ph createrepo command is : ${CMD}"
    
    ${CMD} && echo "Create Repo Success!" || echo "Create Repo Fail!"
    

    执行phyumcreate.sh,创建完成,可在文件夹下看到成功创建repodata文件夹

    2018/08/09  15:54    <DIR>          .
    2018/08/09  15:54    <DIR>          ..
    2018/08/09  15:54            11,867 46b92d40086259937418c90301cbee5042d2fd4c342f84b6207b719563efd791-other.xml.gz
    2018/08/09  15:54            18,439 69b713820b8d405d45252e6db3dff3a014cb06f1ae02f5cf7f58d722026a5db9-primary.xml.gz
    2018/08/09  15:54            35,021 739d977457946ef678a7bbe1a26a5f755485e572ba0ce780cf89200ce0c02d56-primary.sqlite.bz2
    2018/08/09  15:54               484 8d974879197345f42069312dadfb1a0af5ac5adde35f946b30b1a3c7a9937232-phcomps.xml
    2018/08/09  15:54            16,698 9bf888660815a61234d89413e25831d466e321fcdc0bc22b0fa0ca38a05f7948-other.sqlite.bz2
    2018/08/09  15:54            35,388 a1d19aa02141fcb7343c40c092f3ab9032ff53f850aa00d1c4f38919125487b4-filelists.sqlite.bz2
    2018/08/09  15:54            25,903 ae79a79b67f165a9cce063fae49a67517edfe5666c39d9f656ee30850d004f89-filelists.xml.gz
    2018/08/09  15:54               294 b747fd73bebd5df3f4bc5b1b7377a1dd865cdfbd46bd95f9f102e72f0516171d-phcomps.xml.gz
    2018/08/09  15:54             3,687 repomd.xml
                   9 个文件        147,781 字节
                   2 个目录 96,943,001,600 可用字节
    

    在windows上架设http 文件服务器。

    ngnix.conf 配置文件如下

    
    #user  nobody;
    worker_processes  1;
    
    error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       2345;
            server_name  localhost;
    
            #charset utf-8;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   www;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
            }
            
            
            
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    
    

    里面主要修改了 端口号为2345 修改www根目录 / 为目录浏览功能
    修改完成后,运行start.bat开启服务。
    打开chrome,http://192.168.1.221:2345/PHRepo/centos/7/os/x86_64/
    可查看到repo目录正常访问

    OffLine04.jpg

    创建linux客户机repo模板 PHRepo.repo ,并存放到www/ClientScripts/centos下

    # PHRepo.repo
    #   
    # Http template
    #
    #   [PHRepo]
    #   name=PHRepo
    #   baseurl=http://ip:port/PHRepo/centos/$releasever/os/$basearch/
    #   gpgcheck=0
    #   enable=1
    #
    # FileSystem template
    #   [PHRepo]
    #   name=PHRepo
    #   baseurl=baseurl=file:///PHRepo/centos/$releasever/os/$basearch/
    #   gpgcheck=0
    #   enable=1    
    #
    
    
    [PHRepo]
    name=PHRepo
    baseurl=http://{ip:port}/PHRepo/centos/$releasever/os/$basearch/
    gpgcheck=0
    enable=1
    
    

    创建自动脚本 PHRepo.sh ,并存放到www/ClientScripts/CentOS下

    #!/bin/bash
    
    SCRIPTPATH=$(cd `dirname $0` ; pwd)
    REPOFILEPATH="${SCRIPTPATH}/PHRepo.repo"
    
    if [[ -f ${REPOFILEPATH} ]] ; then
        grep -q "name=PHRepo" ${REPOFILEPATH}
        if [[ ! $? -eq 0 ]] ; then
            echo "The PHRepo.repo is not a .repo Files,Please retry Copy/Download it." && exit  
        fi
        
        grep -q "http://{ip:port}/" ${REPOFILEPATH}
        if [[ $? -eq 0 ]] ; then 
            echo "Error,modify the baseurl in the PHRepo.repo and execute the script again!" && exit 
        else
            echo "Back /etc/yum.repos.d/*.repo /etc/yum.repos.d/"
            mkdir /etc/yum.repos.d/bak
            mv --backup=simple /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
            
            cp ${REPOFILEPATH} /etc/yum.repos.d/ && \
            yum clean all && \
            yum clean plugins && \
            yum makecache && \
            yum repolist PHRepo && \
            exit        
        fi 
        
    else
        echo "Error, file ${REPOFILEPATH} not exists !"
        echo "tips:"
        echo "1.copy or curl the PHRepo.repo file to the script path!"
        echo "2.modify the baseurl in the PHRepo.repo!"
        echo "3.execute the script again!"
        
        exit 
        
    fi
    

    目标CentOS使用源进行rpm安装

    1.通过ssh客户端工具或curl命令,将clientScript/centos目录下的PHRepo.sh和PHReop.repo copy或download到 /root 目录中。

    curl -O http://192.168.1.221:2345/ClientScripts/centos/PHRepo.sh
    curl -O http://192.168.1.221:2345/ClientScripts/centos/PHRepo.repo    
    

    2.修改PHRepo.repo中的baseurl为ngnix提供的地址
    3.给PHRepo.sh添加执行权限

    chmod 777 PHRepo.sh
    

    4.运行脚本PHRepo.sh
    ./PHRepo.sh

    踩坑:

    1. centos 下安装vim后在使用 yum downloadonly 和 yumdownloader 安装vim时,只会下载vim的rpm包,不会下载依赖包,安装vim后,在下载其他rpm时,如果依赖和vim的依赖部分相同,那么相同的部分仍然不会下载到downloaddir去,这样在后期别的机器使用这个源安装时,可能会缺少依赖。所以源下载机的环境要求纯净,最好不要安装任何其他rpm包,如果确实需要安装(比如上面用到的createrepo),最好在安装之前先将要安装的rpm 进行downloadonly操作,将依赖和rpm包都放到 downloaddir目录去。
    1. mount cifs 时nobrl这个参数很重要,开始的时候,没有添加,导致在后面createrepo时,产生错误。

    Saving Primary metadata
    Saving file lists metadata
    Saving other metadata
    Generating sqlite DBs
    (process:3797): GLib-CRITICAL **: g_timer_stop: assertion 'timer != NULL' failed
    (process:3797): GLib-CRITICAL **: g_timer_destroy: assertion 'timer != NULL' failed
    Could not remove temp metadata dir: .repodata
    Error was [Errno 39] Directory not empty: '/PHRepo/centos/7/os/x86_64/.repodata'
    Please clean up this directory manually.
    Traceback (most recent call last):
    File "/usr/share/createrepo/genpkgmetadata.py", line 309, in <module>
    main(sys.argv[1:])
    File "/usr/share/createrepo/genpkgmetadata.py", line 274, in main
    mdgen.doRepoMetadata()
    File "/usr/lib/python2.7/site-packages/createrepo/init.py", line 1014, in doRepoMetadata
    gen_func(complete_path, csum)
    File "/usr/lib64/python2.7/site-packages/sqlitecachec.py", line 61, in getOtherdata
    self.repoid))
    TypeError: Can not create db_info table: database is locked

    相关文章

      网友评论

        本文标题:本地/局域网YUM源搭建、自定义分组

        本文链接:https://www.haomeiwen.com/subject/nuoqbftx.html