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