Blame SOURCES/mysql-prepare-db-dir.sh

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