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

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