2023-05-23 08:03:53 +02:00
|
|
|
#!/bin/bash
|
2012-01-23 12:20:16 +01:00
|
|
|
#
|
|
|
|
# summary of how this script can be called:
|
|
|
|
# * <new-preinst> install
|
|
|
|
# * <new-preinst> install <old-version>
|
|
|
|
# * <new-preinst> upgrade <old-version>
|
|
|
|
# * <old-preinst> abort-upgrade <new-version>
|
|
|
|
#
|
|
|
|
|
2023-05-23 08:03:53 +02:00
|
|
|
set -e
|
|
|
|
|
2023-03-29 11:42:12 +02:00
|
|
|
# shellcheck source=/dev/null
|
2012-01-23 12:20:16 +01:00
|
|
|
. /usr/share/debconf/confmodule
|
|
|
|
|
2020-04-07 11:10:03 +02:00
|
|
|
# Just kill the invalid insserv.conf.d directory without fallback
|
2023-03-30 12:58:54 +02:00
|
|
|
if [ -d "/etc/insserv.conf.d/mariadb/" ]
|
|
|
|
then
|
2023-03-29 11:28:51 +02:00
|
|
|
rm -rf "/etc/insserv.conf.d/mariadb/"
|
2020-04-07 11:10:03 +02:00
|
|
|
fi
|
|
|
|
|
2023-03-30 12:58:54 +02:00
|
|
|
if [ -n "$DEBIAN_SCRIPT_DEBUG" ]
|
|
|
|
then
|
|
|
|
set -v -x
|
|
|
|
DEBIAN_SCRIPT_TRACE=1
|
|
|
|
fi
|
2012-01-23 12:20:16 +01:00
|
|
|
${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
|
|
|
|
|
|
|
|
export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
|
2024-05-27 08:04:34 +02:00
|
|
|
mariadb_datadir=/var/lib/mysql
|
|
|
|
mariadb_upgradedir=/var/lib/mysql-upgrade
|
2012-01-23 12:20:16 +01:00
|
|
|
|
2023-10-08 04:42:31 +02:00
|
|
|
MARIADBD_USERS="root"
|
|
|
|
|
|
|
|
# Check if user 'mysql' exists before referring to it in pgrep
|
|
|
|
# to avoid pgrep erroring on 'invalid user name'
|
|
|
|
if id mysql >/dev/null 2>&1
|
|
|
|
then
|
|
|
|
MARIADBD_USERS="$MARIADBD_USERS,mysql"
|
|
|
|
fi
|
|
|
|
|
2012-01-23 12:20:16 +01:00
|
|
|
# Try to stop the server in a sane way. If it does not success let the admin
|
|
|
|
# do it himself. No database directories should be removed while the server
|
2020-06-11 00:39:11 +02:00
|
|
|
# is running! Another mariadbd in e.g. a different chroot is fine for us.
|
2012-01-23 12:20:16 +01:00
|
|
|
stop_server() {
|
2023-03-29 11:28:51 +02:00
|
|
|
# Return immediately if there are no mysqld processes running on a host
|
|
|
|
# (leave containerized processes with the same name in other namespaces)
|
|
|
|
# as there is no point in trying to shutdown in that case.
|
2023-10-08 04:42:31 +02:00
|
|
|
if ! pgrep -x -u "$MARIADBD_USERS" --nslist pid --ns $$ "mysqld|mariadbd" > /dev/null
|
2023-03-30 12:58:54 +02:00
|
|
|
then
|
|
|
|
return
|
|
|
|
fi
|
2023-03-29 11:28:51 +02:00
|
|
|
|
|
|
|
set +e
|
|
|
|
invoke-rc.d mariadb stop
|
|
|
|
invoke-rc.d mysql stop # Backwards compatibility
|
|
|
|
errno=$?
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# systemctl could emit exit code 100=no init script (fresh install)
|
2023-03-30 12:58:54 +02:00
|
|
|
if [ "$errno" != 0 ] && [ "$errno" != 100 ]
|
|
|
|
then
|
2023-03-29 11:28:51 +02:00
|
|
|
echo "Attempt to stop MariaDB/MySQL server returned exitcode $errno" 1>&2
|
|
|
|
echo "There is a MariaDB/MySQL server running, but we failed in our attempts to stop it." 1>&2
|
|
|
|
echo "Stop it yourself and try again!" 1>&2
|
|
|
|
db_stop
|
|
|
|
exit 1
|
|
|
|
fi
|
2012-01-23 12:20:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
################################ main() ##########################
|
|
|
|
|
2022-05-01 08:27:39 +02:00
|
|
|
# @TODO: Rewrite this to use the new upstream /var/lib/mysql_upgrade_info file
|
|
|
|
# instead of the legacy /var/lib/debian-XX.X.flag file
|
|
|
|
this_version=__MARIADB_MAJOR_VER__
|
2017-02-15 13:08:03 +01:00
|
|
|
max_upgradeable_version=5.7
|
2016-10-09 22:39:01 +02:00
|
|
|
|
|
|
|
# Check if a flag file is found that indicates a previous MariaDB or MySQL
|
|
|
|
# version was installed. If multiple flags are found, check which one was
|
|
|
|
# the biggest version number.
|
2024-05-27 08:04:34 +02:00
|
|
|
for flag in "$mariadb_datadir"/debian-*.flag
|
2016-10-09 22:39:01 +02:00
|
|
|
do
|
|
|
|
|
|
|
|
# The for loop leaves $flag as the query string if there are no results,
|
|
|
|
# so the check below is needed to stop further processing when there are
|
|
|
|
# no real results.
|
2024-05-27 08:04:34 +02:00
|
|
|
if [ "$flag" = "$mariadb_datadir/debian-*.flag" ]
|
2016-10-09 22:39:01 +02:00
|
|
|
then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
2023-10-08 04:42:31 +02:00
|
|
|
# The whole flag_version thing should be rewritten, so ignore minor Shellcheck
|
|
|
|
# nag for now
|
|
|
|
# shellcheck disable=SC2001
|
2020-04-07 11:10:03 +02:00
|
|
|
flag_version=$(echo "$flag" | sed 's/.*debian-\([0-9\.]\+\).flag/\1/')
|
2016-10-09 22:39:01 +02:00
|
|
|
|
|
|
|
# Initialize value if empty
|
|
|
|
if [ -z "$found_version" ]
|
|
|
|
then
|
|
|
|
found_version=$flag_version
|
|
|
|
fi
|
2012-01-23 12:20:16 +01:00
|
|
|
|
2016-10-09 22:39:01 +02:00
|
|
|
# Update value if now bigger then before
|
|
|
|
if dpkg --compare-versions "$flag_version" '>>' "$found_version"
|
|
|
|
then
|
|
|
|
found_version=$flag_version
|
2012-01-23 12:20:16 +01:00
|
|
|
fi
|
2016-10-09 22:39:01 +02:00
|
|
|
|
2012-01-23 12:20:16 +01:00
|
|
|
done
|
2016-10-09 22:39:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
# If an upgrade is detected, proceed with it automatically without
|
|
|
|
# requiring any user interaction.
|
|
|
|
#
|
|
|
|
# However, if the user attempts to downgrade, warn about the incompatibility.
|
|
|
|
# Downgrade is detected if the flag version is bigger than $this_version
|
|
|
|
# (e.g. 10.1 > 10.0) or the flag version is smaller than 10.0 but bigger
|
|
|
|
# than $max_upgradeable_version.
|
2023-03-29 11:42:12 +02:00
|
|
|
if [ -n "$found_version" ]
|
2016-10-09 22:39:01 +02:00
|
|
|
then
|
|
|
|
|
2020-04-07 11:10:03 +02:00
|
|
|
# MySQL 8.0 in Ubuntu has a bug in packaging and the file is name wrongly
|
|
|
|
# 'debian-5.7.flag', so in case '5.7' was encountered an extra check needs to
|
|
|
|
# be done to see is there is a file called undo_001, which is a sign of 8.0.
|
2024-05-27 08:04:34 +02:00
|
|
|
if [ "$found_version" == "5.7" ] && [ -f "$mariadb_datadir/undo_001" ]
|
2020-04-07 11:10:03 +02:00
|
|
|
then
|
|
|
|
# Seems to be a 8.0, flag has wrongly 5.7 (know bug)
|
|
|
|
found_version=8.0
|
|
|
|
fi
|
|
|
|
|
2024-05-27 08:04:34 +02:00
|
|
|
echo "$mariadb_datadir: found previous version $found_version"
|
2016-10-09 22:39:01 +02:00
|
|
|
|
|
|
|
if dpkg --compare-versions "$found_version" '>>' "$this_version"
|
|
|
|
then
|
|
|
|
downgrade_detected=true
|
2012-01-23 12:20:16 +01:00
|
|
|
fi
|
2016-10-09 22:39:01 +02:00
|
|
|
|
|
|
|
if dpkg --compare-versions "$found_version" '>>' "$max_upgradeable_version" \
|
2023-03-29 11:28:51 +02:00
|
|
|
&& dpkg --compare-versions "$found_version" '<<' "10.0"
|
2016-10-09 22:39:01 +02:00
|
|
|
then
|
|
|
|
downgrade_detected=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2023-01-06 06:56:14 +01:00
|
|
|
# If there is no debian-*.flag, and no version was detected, but a file that
|
|
|
|
# indicated MySQL 8.0 is found (undo_001 is created by default in MySQL 8.0+
|
|
|
|
# installs), then that file is enough of additional indication to trigger the
|
|
|
|
# move of the data directory.
|
|
|
|
if [ -z "$found_version" ] &&
|
2024-05-27 08:04:34 +02:00
|
|
|
[ -z "$(find $mariadb_datadir/debian-*.flag 2> /dev/null)" ] &&
|
|
|
|
[ -f "$mariadb_datadir/undo_001" ]
|
2023-01-06 06:56:14 +01:00
|
|
|
then
|
2024-05-27 08:04:34 +02:00
|
|
|
echo "$mariadb_datadir: no server version flag found, assuming MySQL 8.0 data encountered"
|
2023-01-06 06:56:14 +01:00
|
|
|
downgrade_detected=true
|
|
|
|
found_version="previous" # Just use dummy name as we don't know real version
|
|
|
|
fi
|
2016-10-09 22:39:01 +02:00
|
|
|
|
|
|
|
# Don't abort dpkg if downgrade is detected (as was done previously).
|
|
|
|
# Instead simply move the old datadir and create a new for this_version.
|
2023-03-29 11:42:12 +02:00
|
|
|
if [ -n "$downgrade_detected" ]
|
2016-10-09 22:39:01 +02:00
|
|
|
then
|
2022-05-01 08:27:39 +02:00
|
|
|
db_input critical "mariadb-server/old_data_directory_saved" || true
|
2016-10-09 22:39:01 +02:00
|
|
|
db_go
|
2024-05-27 08:04:34 +02:00
|
|
|
echo "The file $mariadb_datadir/debian-$found_version.flag indicates a" 1>&2
|
2016-10-09 22:39:01 +02:00
|
|
|
echo "version that cannot automatically be upgraded. Therefore the" 1>&2
|
2024-05-27 08:04:34 +02:00
|
|
|
echo "previous data directory will be renamed to $mariadb_datadir-$found_version and" 1>&2
|
|
|
|
echo "a new data directory will be initialized at $mariadb_datadir." 1>&2
|
2016-10-09 22:39:01 +02:00
|
|
|
echo "Please manually export/import your data (e.g. with mysqldump) if needed." 1>&2
|
2024-05-27 08:04:34 +02:00
|
|
|
mv -f "$mariadb_datadir" "$mariadb_datadir-$found_version"
|
2016-10-09 22:39:01 +02:00
|
|
|
# Also move away the old debian.cnf file that included credentials that are
|
2023-01-06 06:56:14 +01:00
|
|
|
# no longer valid. If none existed, ignore error and let dpkg continue.
|
|
|
|
mv -f /etc/mysql/debian.cnf "/etc/mysql/debian.cnf-$found_version" || true
|
2012-01-23 12:20:16 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# to be sure
|
|
|
|
stop_server
|
|
|
|
|
|
|
|
# If we use NIS then errors should be tolerated. It's up to the
|
|
|
|
# user to ensure that the mysql user is correctly setup.
|
|
|
|
# Beware that there are two ypwhich one of them needs the 2>/dev/null!
|
2023-10-08 04:42:31 +02:00
|
|
|
if test -n "$(command -v ypwhich 2>/dev/null)" && ypwhich > /dev/null 2>&1
|
2023-03-30 12:58:54 +02:00
|
|
|
then
|
2012-01-23 12:20:16 +01:00
|
|
|
set +e
|
|
|
|
fi
|
|
|
|
|
|
|
|
#
|
|
|
|
# Now we have to ensure the following state:
|
2023-10-08 04:42:31 +02:00
|
|
|
# /etc/passwd: mysql:x:100:101:MariaDB Server:/nonexistent:/bin/false
|
2012-01-23 12:20:16 +01:00
|
|
|
# /etc/group: mysql:x:101:
|
2016-10-09 22:39:01 +02:00
|
|
|
#
|
2012-01-23 12:20:16 +01:00
|
|
|
# Sadly there could any state be present on the system so we have to
|
|
|
|
# modify everything carefully i.e. not doing a chown before creating
|
|
|
|
# the user etc...
|
|
|
|
#
|
|
|
|
|
|
|
|
# creating mysql group if he isn't already there
|
2023-03-30 12:58:54 +02:00
|
|
|
if ! getent group mysql >/dev/null
|
|
|
|
then
|
2023-03-29 11:28:51 +02:00
|
|
|
# Adding system group: mysql.
|
|
|
|
addgroup --system mysql >/dev/null
|
2012-01-23 12:20:16 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# creating mysql user if he isn't already there
|
2023-03-30 12:58:54 +02:00
|
|
|
if ! getent passwd mysql >/dev/null
|
|
|
|
then
|
2023-03-29 11:28:51 +02:00
|
|
|
# Adding system user: mysql.
|
|
|
|
adduser \
|
|
|
|
--system \
|
|
|
|
--disabled-login \
|
|
|
|
--ingroup mysql \
|
|
|
|
--no-create-home \
|
|
|
|
--home /nonexistent \
|
2023-10-08 04:42:31 +02:00
|
|
|
--gecos "MariaDB Server" \
|
2023-03-29 11:28:51 +02:00
|
|
|
--shell /bin/false \
|
2023-10-08 04:42:31 +02:00
|
|
|
mysql >/dev/null 2>&1
|
2012-01-23 12:20:16 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# end of NIS tolerance zone
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# if there's a symlink, let's store where it's pointing, because otherwise
|
|
|
|
# it's going to be lost in some situations
|
2023-03-30 12:58:54 +02:00
|
|
|
for dir in DATADIR LOGDIR
|
|
|
|
do
|
2020-04-07 11:10:03 +02:00
|
|
|
checkdir=$(eval echo "$"$dir)
|
2023-10-08 04:42:31 +02:00
|
|
|
if [ -L "$checkdir" ]
|
|
|
|
then
|
2020-04-07 11:10:03 +02:00
|
|
|
# Use mkdir option 'Z' to create with correct SELinux context.
|
2024-05-27 08:04:34 +02:00
|
|
|
mkdir -pZ "$mariadb_upgradedir"
|
|
|
|
cp -dT "$checkdir" "$mariadb_upgradedir/$dir.link"
|
2020-04-07 11:10:03 +02:00
|
|
|
fi
|
2012-01-23 12:20:16 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
# creating mysql home directory
|
2024-05-27 08:04:34 +02:00
|
|
|
if [ ! -d $mariadb_datadir ] && [ ! -L $mariadb_datadir ]
|
2023-03-30 12:58:54 +02:00
|
|
|
then
|
2023-03-29 11:28:51 +02:00
|
|
|
# Use mkdir option 'Z' to create with correct SELinux context.
|
2024-05-27 08:04:34 +02:00
|
|
|
mkdir -Z $mariadb_datadir
|
2012-01-23 12:20:16 +01:00
|
|
|
fi
|
|
|
|
|
2023-10-08 04:42:31 +02:00
|
|
|
# As preset blocksize of GNU df is 1024 then available bytes is $df_available_blocks * 1024
|
|
|
|
# 4096 blocks is then lower than 4 MB
|
2024-08-06 14:45:24 +02:00
|
|
|
df_available_blocks="$(LC_ALL=C BLOCKSIZE='' df --output=avail "$mariadb_datadir" | tail -n 1)"
|
2023-10-08 04:42:31 +02:00
|
|
|
if [ "$df_available_blocks" -lt "4096" ]
|
2023-03-30 12:58:54 +02:00
|
|
|
then
|
2024-08-06 14:45:24 +02:00
|
|
|
echo "ERROR: There's not enough space in $mariadb_datadir/" 1>&2
|
2023-10-08 04:42:31 +02:00
|
|
|
db_stop
|
|
|
|
exit 1
|
2012-01-23 12:20:16 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Since the home directory was created before putting the user into
|
2016-10-09 22:39:01 +02:00
|
|
|
# the mysql group and moreover we cannot guarantee that the
|
2012-01-23 12:20:16 +01:00
|
|
|
# permissions were correctly *before* calling this script, we fix them now.
|
|
|
|
# In case we use NIS and no mysql user is present then this script should
|
|
|
|
# better fail now than later..
|
|
|
|
# The "set +e" is necessary as e.g. a ".journal" of a ext3 partition is
|
|
|
|
# not chgrp'able (#318435).
|
|
|
|
set +e
|
2024-05-27 08:04:34 +02:00
|
|
|
find $mariadb_datadir ! -uid "$(id -u mysql)" -print0 | xargs -0 -r chown mysql
|
|
|
|
find $mariadb_datadir -follow -not -group mysql -print0 2>/dev/null \
|
2012-01-23 12:20:16 +01:00
|
|
|
| xargs -0 --no-run-if-empty chgrp mysql
|
|
|
|
set -e
|
|
|
|
|
|
|
|
db_stop
|
|
|
|
|
|
|
|
#DEBHELPER#
|