码迷,mamicode.com
首页 > 其他好文 > 详细

iredmail安装脚本分析(三)---conf/global DISTRO值的来源及操作系统的判断

时间:2015-10-15 12:39:13      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

作者在引入conf/global 文件时,就已经对操作系统的类型进行判断,同时也对DISTRO进行了赋值。

部分代码,如图:

技术分享

显然文件里的KERNEL_NAME的值就是判断完成的操作系统,具体分析该值是如何得到的。

技术分享

就是通过命令“uname –s  | tr ‘[a-z]’ ‘[A-Z]’得到的,不过作者在此处把小写换成了大写。 接下来分析根据不同操作系统,怎么获得不同的DISTRO值,代码如下:

if [ X"${KERNEL_NAME}" == XLINUX ]; then
    # Directory of RC scripts.
    export DIR_RC_SCRIPTS=/etc/init.d

    if [ -f /etc/redhat-release ]; then
        # RHEL/CentOS
        export DISTRO=RHEL

        # Get distribution version
        if grep \ 6 /etc/redhat-release &>/dev/null; then
            # version 6.x
            export DISTRO_VERSION=6
        elif grep \ 7 /etc/redhat-release &>/dev/null; then
            # version 7.x
            export DISTRO_VERSION=7
        else
            export UNSUPPORTED_RELEASE=YES
        fi

        # Get distribution name as DISTRO_CODENAME
        if grep ^Red /etc/redhat-release &>/dev/null; then
            # RHEL
            export DISTRO_CODENAME=rhel
        elif grep ^CentOS /etc/redhat-release &>/dev/null; then
            # CentOS
            export DISTRO_CODENAME=centos
        elif grep ^Scientific /etc/redhat-release &>/dev/null; then
            # Scientific Linux
            export DISTRO_CODENAME=scientific
        else
            export UNSUPPORTED_RELEASE=YES
        fi

    elif [ -f /etc/lsb-release ]; then
        # Ubuntu
        export DISTRO=UBUNTU

        # Ubuntu version number and code name:
        #   - 14.04: trusty
        #   - 15.04: vivid
        export DISTRO_ID="$(grep ‘DISTRIB_ID‘ /etc/lsb-release | awk -F‘=‘ ‘{print $2}‘)"
        export DISTRO_VERSION="$(grep ‘DISTRIB_RELEASE‘ /etc/lsb-release | awk -F‘=‘ ‘{print $2}‘)"
        export DISTRO_CODENAME="$(grep ‘DISTRIB_CODENAME‘ /etc/lsb-release | awk -F‘=‘ ‘{print $2}‘)"

        # Unsupported releases: 12.x, 13.x, 14.10
        if echo "${DISTRO_VERSION}" | grep -E ^(12|13|14\.10) &>/dev/null ; then
            export UNSUPPORTED_RELEASE=YES
        fi
    elif [ -f /etc/debian_version ]; then
        # Debian
        export DISTRO=DEBIAN

        # Get major release version number
        export DISTRO_VERSION="$(cat /etc/debian_version)"

        # Set distro code name and unsupported releases.
        if grep ^7 /etc/debian_version &>/dev/null ||             grep -i ^wheezy /etc/debian_version &>/dev/null; then
            export DISTRO_VERSION=7
            export DISTRO_CODENAME=wheezy
        elif grep ^8 /etc/debian_version &>/dev/null ||             grep -i ^jessie /etc/debian_version &>/dev/null; then
            export DISTRO_VERSION=8
            export DISTRO_CODENAME=jessie
        else
            export UNSUPPORTED_RELEASE=YES
        fi

        # Override settings.
        export SHELL_NOLOGIN=/usr/sbin/nologin
    else
        export UNSUPPORTED_RELEASE=YES
    fi
elif [ X"${KERNEL_NAME}" == XFREEBSD ]; then
    export DISTRO=FREEBSD
    export DISTRO_VERSION="$(uname -r |awk -F‘[.-]‘ ‘{print $1}‘)"

    # Directory of RC scripts.
    export DIR_RC_SCRIPTS=/usr/local/etc/rc.d
    export PYTHON_BIN=/usr/local/bin/python

    # Unsupported releases: 7, 8.
    if echo "${DISTRO_VERSION}" | grep ^[78] &>/dev/null ; then
        export UNSUPPORTED_RELEASE=YES
    fi

    export SHELL_BASH=/usr/local/bin/bash

    # Default password scheme.
    export DEFAULT_PASSWORD_SCHEME=BCRYPT

elif [ X"${KERNEL_NAME}" == XOPENBSD ]; then
    export DISTRO=OPENBSD
    export DISTRO_VERSION="$(uname -r)"

    # Directory of RC scripts.
    export DIR_RC_SCRIPTS=/etc/rc.d
    export RC_CONF_LOCAL=/etc/rc.conf.local
    export SHELL_BASH=/usr/local/bin/bash
    export PYTHON_BIN=/usr/local/bin/python

    # Unsupported release: 5.6 and earlier versions.
    if echo "${DISTRO_VERSION}" | grep ^5.[123456] &>/dev/null ; then
        export UNSUPPORTED_RELEASE=YES
    fi

    # Default password scheme.
    export DEFAULT_PASSWORD_SCHEME=BCRYPT

else
    # Not support *BSD and other distrobutions yet.
    echo "Error: Your OS is not supported yet."
    exit 255
fi

代码比较长,不过脉络比较清晰,首先根据KERNEL_NAME的值判断是哪种操作系统,作者给出的判断是3种,分别是LINUX   OPENBSD   FREEBSD  , 也就是该脚本只能在这3种平台上进行部署,如果需要自己扩展的话,就可以在增加新的判断,最后如果3种都不是的话,就直接返回255,如下代码:

echo "Error: Your OS is not supported yet."
exit 255

接下来分析当KERNEL_NAME的值为LINUX,脚本做了什么,首先定义了一个变量

export DIR_RC_SCRIPTS=/etc/init.d

linux中一般启动脚本都是放在这个目录里的,LINUX本身也有很多版本,因此作者在此处又进行多种类型的判断,判断的依据文件分别是/etc/redhat-release  /etc/lsb-release  /etc/debian_version这3种文件分别对应rhel/centos  ubuntu  debian  , 从而也说明在LINUX的版本中,只支持这3种,其他的LINUX版本是不支持的,最后作者返回一句代码:

技术分享

定义了一个UNSUPPORTED_RELEASE变量,在后续的代码应该会调用这个变量来判断是否支持。

针对这3种LINUX,作者又具体细分到不同的小版本中,首先看rhel/centos 系列,下面代码:

首先根据存在redhat-release文件,定义了DISTRO的值

if [ -f /etc/redhat-release ]; then
export DISTRO=RHEL

这样在get_all.sh的脚本里调用时就有了判断依据,接着判断具体的版本,

if grep \ 6 /etc/redhat-release &>/dev/null; then
            # version 6.x
            export DISTRO_VERSION=6
        elif grep \ 7 /etc/redhat-release &>/dev/null; then
            # version 7.x
            export DISTRO_VERSION=7
        else
            export UNSUPPORTED_RELEASE=YES
        fi

显然只支持6和7两个版本,也就是安装时,如果不是这2个版本的话,就会返回unsupport_release的值为YES了。

rhel还有两个个反编译版本即centos ,Scientific Linux,    因此作者又定义了一个变量来区别 DISTRO_CODENAME, 代码如下:

if grep ^Red /etc/redhat-release &>/dev/null; then
            # RHEL
            export DISTRO_CODENAME=rhel
        elif grep ^CentOS /etc/redhat-release &>/dev/null; then
            # CentOS
            export DISTRO_CODENAME=centos
        elif grep ^Scientific /etc/redhat-release &>/dev/null; then
            # Scientific Linux
            export DISTRO_CODENAME=scientific
        else
            export UNSUPPORTED_RELEASE=YES
        fi

显然在/etc/redhat-release文件里,不同的发行版本有不同的关键字,这样关于红帽系的操作系统,作者就已经区分完毕了。


接下来关于ubuntu的判断就简单多了,看图

技术分享

根据/etc/lsb-release的文件是否存在,赋值DISTRO为UBUNTU,然后判断具体版本,看代码

        export DISTRO_ID="$(grep ‘DISTRIB_ID‘ /etc/lsb-release | awk -F‘=‘ ‘{print $2}‘)"
        export DISTRO_VERSION="$(grep ‘DISTRIB_RELEASE‘ /etc/lsb-release | awk -F‘=‘ ‘{print $2}‘)"
        export DISTRO_CODENAME="$(grep ‘DISTRIB_CODENAME‘ /etc/lsb-release | awk -F‘=‘ ‘{print $2}‘)"

        # Unsupported releases: 12.x, 13.x, 14.10
        if echo "${DISTRO_VERSION}" | grep -E ^(12|13|14\.10) &>/dev/null ; then
            export UNSUPPORTED_RELEASE=YES
        fi

从程序可以看出UBUNTU的ID ,  RELEASE ,  CODENAME的值都在/etc/lsb-release的文件里,用grep 和awk匹配后,赋予不同变量的值。

用grep 筛选出系统版本为12,13,14.10的都为不支持版本,目前支持的版本为14.04,15.04


 

 

iredmail安装脚本分析(三)---conf/global DISTRO值的来源及操作系统的判断

标签:

原文地址:http://www.cnblogs.com/xielinux/p/4881982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!