0d2dc2
#!/bin/sh
0d2dc2
0d2dc2
# This script creates the mysql data directory during first service start.
0d2dc2
# In subsequent starts, it does nothing much.
0d2dc2
0d2dc2
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
0d2dc2
0d2dc2
export LC_ALL=C
0d2dc2
0d2dc2
# Returns content of the specified directory
0d2dc2
# If listing files fails, fake-file is returned so which means
0d2dc2
# we'll behave like there was some data initialized
0d2dc2
# Some files or directories are fine to be there, so those are
0d2dc2
# explicitly removed from the listing
0d2dc2
# @param <dir> datadir
0d2dc2
list_datadir ()
0d2dc2
{
0d2dc2
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
0d2dc2
    -e '^lost+found$' \
0d2dc2
    -e '\.err$' \
0d2dc2
    -e '^.bash_history$'
0d2dc2
}
0d2dc2
0d2dc2
# Checks whether datadir should be initialized
0d2dc2
# @param <dir> datadir
0d2dc2
should_initialize ()
0d2dc2
{
0d2dc2
    test -z "$(list_datadir "$1")"
0d2dc2
}
0d2dc2
0d2dc2
# If two args given first is user, second is group
0d2dc2
# otherwise the arg is the systemd service file
0d2dc2
if [ "$#" -eq 2 ]
0d2dc2
then
0d2dc2
    myuser="$1"
0d2dc2
    mygroup="$2"
0d2dc2
else
0d2dc2
    # Absorb configuration settings from the specified systemd service file,
0d2dc2
    # or the default service if not specified
0d2dc2
    SERVICE_NAME="$1"
0d2dc2
    if [ x"$SERVICE_NAME" = x ]
0d2dc2
    then
0d2dc2
        SERVICE_NAME=@DAEMON_NAME@.service
0d2dc2
    fi
0d2dc2
0d2dc2
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
0d2dc2
      sed 's/^User=//'`
0d2dc2
    if [ x"$myuser" = x ]
0d2dc2
    then
0d2dc2
        myuser=mysql
0d2dc2
    fi
0d2dc2
0d2dc2
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
0d2dc2
      sed 's/^Group=//'`
0d2dc2
    if [ x"$mygroup" = x ]
0d2dc2
    then
0d2dc2
        mygroup=mysql
0d2dc2
    fi
0d2dc2
fi
0d2dc2
0d2dc2
# Set up the errlogfile with appropriate permissions
0d2dc2
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
0d2dc2
    case $(basename "$errlogfile") in
0d2dc2
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
0d2dc2
        *) ;;
0d2dc2
    esac
0d2dc2
else
0d2dc2
    # Provide some advice if the log file cannot be created by this script
0d2dc2
    errlogdir=$(dirname "$errlogfile")
0d2dc2
    if ! [ -d "$errlogdir" ] ; then
0d2dc2
        echo "The directory $errlogdir does not exist." >&2
0d2dc2
        exit 1
0d2dc2
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
0d2dc2
        echo "The log file $errlogfile cannot be written, please, fix its permissions." >&2
0d2dc2
        echo "The daemon will be run under $myuser:$mygroup" >&2
0d2dc2
        exit 1
0d2dc2
    fi
0d2dc2
fi
0d2dc2
0d2dc2
# Make the data directory if doesn't exist or empty
0d2dc2
if should_initialize "$datadir" ; then
0d2dc2
    # First, make sure $datadir is there with correct permissions
0d2dc2
    # (note: if it's not, and we're not root, this'll fail ...)
0d2dc2
    if [ ! -e "$datadir" -a ! -h "$datadir" ]
0d2dc2
    then
0d2dc2
        mkdir -p "$datadir" || exit 1
0d2dc2
    fi
0d2dc2
    chown "$myuser:$mygroup" "$datadir"
0d2dc2
    chmod 0755 "$datadir"
0d2dc2
    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
0d2dc2
0d2dc2
    # Now create the database
0d2dc2
    echo "Initializing @NICE_PROJECT_NAME@ database" >&2
0d2dc2
    # Avoiding deletion of files not created by mysql_install_db is
0d2dc2
    # guarded by time check and sleep should help work-arounded
0d2dc2
    # potential issues on systems with 1 second resolution timestamps
0d2dc2
    # https://bugzilla.redhat.com/show_bug.cgi?id=1335849#c19
0d2dc2
    INITDB_TIMESTAMP=`LANG=C date -u`
0d2dc2
    sleep 1
0d2dc2
    @bindir@/mysql_install_db --rpm --datadir="$datadir" --user="$myuser" --skip-test-db >&2
0d2dc2
    ret=$?
0d2dc2
    if [ $ret -ne 0 ] ; then
0d2dc2
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
0d2dc2
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured or there is some problem with permissions of $datadir." >&2
0d2dc2
        # Clean up any partially-created database files
0d2dc2
        if [ ! -e "$datadir/mysql/user.frm" ] && [ -d "$datadir" ] ; then
0d2dc2
            echo "Initialization of @NICE_PROJECT_NAME@ database was not finished successfully." >&2
0d2dc2
            echo "Files created so far will be removed." >&2
0d2dc2
            find "$datadir" -mindepth 1 -maxdepth 1 -newermt "$INITDB_TIMESTAMP" \
0d2dc2
                 -not -name "lost+found" -exec rm -rf {} +
0d2dc2
            if [ $? -ne 0 ] ; then
0d2dc2
                echo "Removing of created files was not successfull." >&2
0d2dc2
                echo "Please, clean directory $datadir manually." >&2
0d2dc2
            fi
0d2dc2
        else
0d2dc2
            echo "However, part of data has been initialized and those will not be removed." >&2
0d2dc2
            echo "Please, clean directory $datadir manually." >&2
0d2dc2
        fi
0d2dc2
        exit $ret
0d2dc2
    fi
0d2dc2
    # upgrade does not need to be run on a fresh datadir
0d2dc2
    echo "@VERSION@-MariaDB" >"$datadir/mysql_upgrade_info"
0d2dc2
else
0d2dc2
    if [ -d "$datadir/mysql/" ] ; then
0d2dc2
        # mysql dir exists, it seems data are initialized properly
0d2dc2
        echo "Database @NICE_PROJECT_NAME@ is probably initialized in $datadir already, nothing is done."
0d2dc2
        echo "If this is not the case, make sure the $datadir is empty before running `basename $0`."
0d2dc2
    else
0d2dc2
        # if the directory is not empty but mysql/ directory is missing, then
0d2dc2
        # print error and let user to initialize manually or empty the directory
0d2dc2
        echo "Database @NICE_PROJECT_NAME@ is not initialized, but the directory $datadir is not empty, so initialization cannot be done." >&2
0d2dc2
        echo "Make sure the $datadir is empty before running `basename $0`." >&2
0d2dc2
        exit 1
0d2dc2
    fi
0d2dc2
fi
0d2dc2
0d2dc2
exit 0