1f8cff
#!/bin/sh
1f8cff
1f8cff
# This script creates the mysql data directory during first service start.
1f8cff
# In subsequent starts, it does nothing much.
1f8cff
1f8cff
# extract value of a MySQL option from config files
1f8cff
# Usage: get_mysql_option SECTION VARNAME DEFAULT
1f8cff
# result is returned in $result
1f8cff
# We use my_print_defaults which prints all options from multiple files,
1f8cff
# with the more specific ones later; hence take the last match.
1f8cff
get_mysql_option(){
1f8cff
        if [ $# -ne 3 ] ; then
1f8cff
          echo "get_mysql_option requires 3 arguments: section option default_value"
1f8cff
          return
1f8cff
        fi
1f8cff
        result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
1f8cff
        if [ -z "$result" ]; then
1f8cff
            # if not found, use the default value
1f8cff
            result="$3"
1f8cff
        fi
1f8cff
}
1f8cff
1f8cff
# Defaults here had better match what mysqld_safe will default to
1f8cff
get_mysql_option mysqld datadir "/var/lib/mysql"
1f8cff
datadir="$result"
1f8cff
get_mysql_option mysqld_safe log-error "/var/log/mariadb/mariadb.log"
1f8cff
errlogfile="$result"
1f8cff
get_mysql_option mysqld socket "$datadir/mysql.sock"
1f8cff
socketfile="$result"
1f8cff
1f8cff
1f8cff
1f8cff
# Absorb configuration settings from the specified systemd service file,
1f8cff
# or the default "mysqld" service if not specified
1f8cff
SERVICE_NAME="$1"
1f8cff
if [ x"$SERVICE_NAME" = x ]
1f8cff
then
1f8cff
    SERVICE_NAME=mysqld.service
1f8cff
fi
1f8cff
1f8cff
myuser=`systemctl show -p User "${SERVICE_NAME}" |
1f8cff
  sed 's/^User=//'`
1f8cff
if [ x"$myuser" = x ]
1f8cff
then
1f8cff
    myuser=mysql
1f8cff
fi
1f8cff
1f8cff
mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
1f8cff
  sed 's/^Group=//'`
1f8cff
if [ x"$mygroup" = x ]
1f8cff
then
1f8cff
    mygroup=mysql
1f8cff
fi
1f8cff
1f8cff
1f8cff
1f8cff
# Set up the errlogfile with appropriate permissions
1f8cff
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
1f8cff
    case $(basename "$errlogfile") in
1f8cff
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
1f8cff
        *) ;;
1f8cff
    esac
1f8cff
else
1f8cff
    # Provide some advice if the log file cannot be created by this script
1f8cff
    errlogdir=$(dirname "$errlogfile")
1f8cff
    if ! [ -d "$errlogdir" ] ; then
1f8cff
        echo "The directory $errlogdir does not exist."
1f8cff
        exit 1
1f8cff
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
1f8cff
        echo "The log file $errlogfile cannot be written, please, fix its permissions."
1f8cff
        echo "The daemon will be run under $myuser:$mygroup"
1f8cff
        exit 1
1f8cff
    fi
1f8cff
fi
1f8cff
1f8cff
1f8cff
1f8cff
# We check if there is already a process using the socket file,
1f8cff
# since otherwise this systemd service file could report false
1f8cff
# positive result when starting and mysqld_safe could remove
1f8cff
# a socket file, which actually uses a different daemon.
1f8cff
if fuser "$socketfile" &>/dev/null ; then
1f8cff
    echo "Socket file $socketfile exists." >&2
1f8cff
    echo "Is another MySQL daemon already running with the same unix socket?" >&2
1f8cff
    exit 1
1f8cff
fi
1f8cff
1f8cff
1f8cff
1f8cff
export LC_ALL=C
1f8cff
1f8cff
# Returns content of the specified directory
1f8cff
# If listing files fails, fake-file is returned so which means
1f8cff
# we'll behave like there was some data initialized
1f8cff
# Some files or directories are fine to be there, so those are
1f8cff
# explicitly removed from the listing
1f8cff
# @param <dir> datadir
1f8cff
list_datadir ()
1f8cff
{
1f8cff
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
1f8cff
    -e '^lost+found$' \
1f8cff
    -e '\.err$' \
1f8cff
    -e '^.bash_history$'
1f8cff
}
1f8cff
1f8cff
# Checks whether datadir should be initialized
1f8cff
# @param <dir> datadir
1f8cff
should_initialize ()
1f8cff
{
1f8cff
    test -z "$(list_datadir "$1")"
1f8cff
}
1f8cff
1f8cff
# Make the data directory if doesn't exist or empty
1f8cff
if should_initialize "$datadir" ; then
1f8cff
    # First, make sure $datadir is there with correct permissions
1f8cff
    # (note: if it's not, and we're not root, this'll fail ...)
1f8cff
    if [ ! -e "$datadir" -a ! -h "$datadir" ]
1f8cff
    then
1f8cff
        mkdir -p "$datadir" || exit 1
1f8cff
    fi
1f8cff
    chown "$myuser:$mygroup" "$datadir"
1f8cff
    chmod 0755 "$datadir"
1f8cff
    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
1f8cff
1f8cff
1f8cff
1f8cff
    # Now create the database
1f8cff
    echo "Initializing MariaDB database"
1f8cff
    # Avoiding deletion of files not created by mysql_install_db is
1f8cff
    # guarded by time check and sleep should help work-arounded
1f8cff
    # potential issues on systems with 1 second resolution timestamps
1f8cff
    # https://bugzilla.redhat.com/show_bug.cgi?id=1335849#c19
1f8cff
    INITDB_TIMESTAMP=`LANG=C date -u`
1f8cff
    sleep 1
1f8cff
    /usr/bin/mysql_install_db --rpm --datadir="$datadir" --user="$myuser"
1f8cff
    ret=$?
1f8cff
    if [ $ret -ne 0 ] ; then
1f8cff
        echo "Initialization of MariaDB database failed." >&2
1f8cff
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured or there is some problem with permissions of $datadir." >&2
1f8cff
        # Clean up any partially-created database files
1f8cff
        if [ ! -e "$datadir/mysql/user.frm" ] && [ -d "$datadir" ] ; then
1f8cff
            echo "Initialization of MariaDB database was not finished successfully." >&2
1f8cff
            echo "Files created so far will be removed." >&2
1f8cff
            find "$datadir" -mindepth 1 -maxdepth 1 -newermt "$INITDB_TIMESTAMP" \
1f8cff
                 -not -name "lost+found" -exec rm -rf {} +
1f8cff
            if [ $? -ne 0 ] ; then
1f8cff
                echo "Removing of created files was not successfull." >&2
1f8cff
                echo "Please, clean directory $datadir manually." >&2
1f8cff
            fi
1f8cff
        else
1f8cff
            echo "However, part of data has been initialized and those will not be removed." >&2
1f8cff
            echo "Please, clean directory $datadir manually." >&2
1f8cff
        fi
1f8cff
        exit $ret
1f8cff
    fi
1f8cff
else
1f8cff
    if [ -d "$datadir/mysql/" ] ; then
1f8cff
        # mysql dir exists, it seems data are initialized properly
1f8cff
        echo "Database MariaDB is probably initialized in $datadir already, nothing is done."
1f8cff
        echo "If this is not the case, make sure the $datadir is empty before running `basename $0`."
1f8cff
    else
1f8cff
        # if the directory is not empty but mysql/ directory is missing, then
1f8cff
        # print error and let user to initialize manually or empty the directory
1f8cff
        echo "Database MariaDB is not initialized, but the directory $datadir is not empty, so initialization cannot be done."
1f8cff
        echo "Make sure the $datadir is empty before running `basename $0`."
1f8cff
        exit 1
1f8cff
    fi
1f8cff
fi
1f8cff
1f8cff
exit 0