7a5573f
#!/bin/sh
7a5573f
7a5573f
# This script creates the mysql data directory during first service start.
7a5573f
# In subsequent starts, it does nothing much.
dd8b0ea
#
dd8b0ea
# This script is meant to be run as non-root user either during initscript
dd8b0ea
# or systemd service execution, before starting the mysqld daemon.
dd8b0ea
# Running it as root may have some security risks, because it touches files
dd8b0ea
# that can be symlinks pointing to unexpected locations.
dd8b0ea
#
dd8b0ea
# On the other hand, when using non-standard locations for datadir and logfile,
dd8b0ea
# this script might not be able to create the files and the daemon won't start
dd8b0ea
# properly. A solution for that is to created the locations for datadir and
dd8b0ea
# logfile with correct ownership before starting the daemon.
7a5573f
75032c4
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
7a5573f
99fd710
# If two args given first is user, second is group
99fd710
# otherwise the arg is the systemd service file
99fd710
if [ "$#" -eq 2 ]
7a5573f
then
99fd710
    myuser="$1"
99fd710
    mygroup="$2"
99fd710
else
99fd710
    # Absorb configuration settings from the specified systemd service file,
75032c4
    # or the default service if not specified
99fd710
    SERVICE_NAME="$1"
99fd710
    if [ x"$SERVICE_NAME" = x ]
99fd710
    then
99fd710
        SERVICE_NAME=@DAEMON_NAME@.service
99fd710
    fi
7a5573f
99fd710
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
99fd710
      sed 's/^User=//'`
99fd710
    if [ x"$myuser" = x ]
99fd710
    then
99fd710
        myuser=mysql
99fd710
    fi
7a5573f
99fd710
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
99fd710
      sed 's/^Group=//'`
99fd710
    if [ x"$mygroup" = x ]
99fd710
    then
99fd710
        mygroup=mysql
99fd710
    fi
7a5573f
fi
7a5573f
7a5573f
# Set up the errlogfile with appropriate permissions
dd8b0ea
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
dd8b0ea
    case $(basename "$errlogfile") in
dd8b0ea
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
dd8b0ea
        *) ;;
dd8b0ea
    esac
dd8b0ea
else
dd8b0ea
    # Provide some advice if the log file cannot be created by this script
dd8b0ea
    errlogdir=$(dirname "$errlogfile")
99fd710
    if ! [ -d "$errlogdir" ] ; then
99fd710
        echo "The directory $errlogdir does not exist."
dd8b0ea
        exit 1
dd8b0ea
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
dd8b0ea
        echo "The log file $errlogfile cannot be written, please, fix its permissions."
dd8b0ea
        echo "The daemon will be run under $myuser:$mygroup"
dd8b0ea
        exit 1
99fd710
    fi
99fd710
fi
7a5573f
dd8b0ea
dd8b0ea
dd8b0ea
export LC_ALL=C
dd8b0ea
dd8b0ea
# Returns content of the specified directory
dd8b0ea
# If listing files fails, fake-file is returned so which means
dd8b0ea
# we'll behave like there was some data initialized
dd8b0ea
# Some files or directories are fine to be there, so those are
dd8b0ea
# explicitly removed from the listing
dd8b0ea
# @param <dir> datadir
dd8b0ea
list_datadir ()
dd8b0ea
{
dd8b0ea
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
dd8b0ea
    -e '^lost+found$' \
dd8b0ea
    -e '\.err$' \
dd8b0ea
    -e '^\.bash_history$'
dd8b0ea
}
dd8b0ea
dd8b0ea
# Checks whether datadir should be initialized
dd8b0ea
# @param <dir> datadir
dd8b0ea
should_initialize ()
dd8b0ea
{
dd8b0ea
    test -z "$(list_datadir "$1")"
dd8b0ea
}
dd8b0ea
dd8b0ea
# Make the data directory if doesn't exist or empty
dd8b0ea
if should_initialize "$datadir" ; then
7a5573f
7a5573f
    # Now create the database
75032c4
    echo "Initializing @NICE_PROJECT_NAME@ database"
1d9080e
    @libexecdir@/mysqld --initialize-insecure --datadir="$datadir" --user="$myuser"
7a5573f
    ret=$?
7a5573f
    if [ $ret -ne 0 ] ; then
75032c4
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
99fd710
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured." >&2
7a5573f
        # Clean up any partially-created database files
7a5573f
        if [ ! -e "$datadir/mysql/user.frm" ] ; then
7a5573f
            rm -rf "$datadir"/*
7a5573f
        fi
7a5573f
        exit $ret
7a5573f
    fi
0d2208e
    # upgrade does not need to be run on a fresh datadir
0d2208e
    echo "@VERSION@" >"$datadir/mysql_upgrade_info"
7a5573f
fi
7a5573f
7a5573f
exit 0