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