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

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