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

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