Iredmail installation script analysis (iii) --- conf/global DISTRO value source and operating system judgment, iredmaildistro
When introducing the conf/global file, the author has determined the type of the operating system and assigned a value to DISTRO.
Some code,
Obviously, the value of KERNEL_NAME in the file is the completed operating system. The specific analysis shows 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, we will analyze what the script does when the value of KERNEL_NAME is LINUX. First, we define a variable.
export DIR_RC_SCRIPTS='/etc/init.d'
In linux, boot scripts are usually stored in this directory, and LINUX itself has many versions. Therefore, the author makes multiple types of judgments here, the judgment files are respectively/etc/redhat-release/etc/lsb-release/etc/debian_version, which correspond to rhel/centos ubuntu debian, it also indicates that only these three types are supported in the LINUX version. Other LINUX versions are not supported. Finally, the author returns the following code:
Defines an UNSUPPORTED_RELEASE variable. Later code should call this variable to determine whether it is supported.
For these three types of LINUX, the author subdivided them into different small versions. First, let's look at the rhel/centos series. The following code:
First, the DISTRO value is defined based on the existing redhat-release file.
if [ -f /etc/redhat-release ]; then
export DISTRO='RHEL'
In this way, we have a judgment basis for calling the get_all.sh script, and then determine 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 will be much simpler, as shown in the figure.
Based on whether the/etc/lsb-release file exists, assign DISTRO to UBUNTU, and then determine the specific version to 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.
Next, DEBIAN is used to judge files,
If the file exists, the DISTRO value is DEBIAN. Then, judge the version number:
# Get major release version number export DISTRO_VERSION="$(cat /etc/debian_version)"
The cat/etc/debian_version command can get the specific version, which is easier to judge than rhel.
Next, let's determine the specific version, as shown in 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.