Iredmail installation script analysis (iii) --- conf/global DISTRO value source and operating system judgment
When introducing the conf/global file, the author has determined the type of the operating system and assigned a value to DISTRO. Part of the Code, it is clear that the value of KERNEL_NAME in the file is the operating system to determine the completion of the operation, the specific analysis of how the value is obtained. It is obtained through the command "uname-s | tr '[a-z]'' [A-Z]', but the author here converts the lower case to the upper case. Next, we will analyze how to obtain different DISTRO values based on different operating systems. The Code is as follows:
if [ X"${KERNEL_NAME}" == X'LINUX' ]; 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' fielif [ X"${KERNEL_NAME}" == X'FREEBSD' ]; 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}" == X'OPENBSD' ]; 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 255fi
The code is long, but the context is clear. First, determine the operating system based on the value of KERNEL_NAME. The author gives three types of judgments: linux openbsd freebsd, that is, the script can only be deployed on these three platforms. If you need to expand the script, you can add a new judgment. If the three are not, the script will return 255, the following code: echo "Error: Your OS is not supported yet. "exit 255 next, analyze what the script does when the KERNEL_NAME value is LINUX. First, define a variable export DIR_RC_SCRIPTS = '/etc/init. d 'linux generally stores startup scripts in this directory, and linux itself has many versions. Therefore, the author makes multiple types of judgments here, the judgment file is/etc/redhat-release/etc/lsb-release/etc/debian_version. Files correspond to rhel/centos ubuntu debian, which also indicates that only these three types are supported in LINUX versions, but not in other LINUX versions, finally, the author returns the following code: defines an UNSUPPORTED_RELEASE variable. Later code will call this variable to determine whether it is supported. For these three LINUX systems, the author subdivided them into different small versions. First, let's look at the rhel/centos series. The following code: First, based on the redhat-release file, defines the DISTRO value if [-f/etc/redhat-release]; thenexport DISTRO = 'rhel 'so that there is a judgment basis for calling in the get_all.sh script, then judge the specific 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
Obviously, only versions 6 and 7 are supported, that is, if not, the value of unsupport_release is YES. Rhel has two decompilation versions: centos and Scientific Linux. Therefore, the author defines a variable to distinguish DISTRO_CODENAME. The Code is as follows:
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
Obviously, in the/etc/redhat-release file, different release versions have different keywords, so that the author has already divided the operating systems of the Red Hat system. Next, the ubuntu judgment is much simpler. See the figure to see whether the/etc/lsb-release file exists. Assign DISTRO to UBUNTU, then determine the specific version, and check the code.
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
From the program, we can see that the ubuntu id, RELEASE, and CODENAME values are all in the/etc/lsb-release file. After matching with grep and awk, different variable values are assigned. Use grep to filter out unsupported versions of the system versions 12, 13, and 14.10. Currently, the supported versions are 14.04, and 15.04 is followed by DEBIAN. It is also used to determine whether a file exists, the DISTRO value is DEBIAN. Next, judge the version number: # Get major release version number export DISTRO_VERSION = "$ (cat/etc/debian_version) "cat/etc/debian_version: a command can get the specific version. It is easier to judge the specific version than rhel. For details, see the code:
# 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
Obviously, only the DEBIAN 7 and 8 versions are supported. The DISTRO value and operating system judgment are over. If you want to add a special operating system or define it yourself, add your own code here.