· 7 years ago · Nov 24, 2018, 03:36 PM
1#!/bin/bash -e
2
3. /usr/share/debconf/confmodule
4
5if [ -n "$DEBIAN_SCRIPT_DEBUG" ]; then set -v -x; DEBIAN_SCRIPT_TRACE=1; fi
6${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
7
8export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
9
10# This command can be used as pipe to syslog. With "-s" it also logs to stderr.
11ERR_LOGGER="logger -p daemon.err -t mysqld_safe -i"
12# This will make an error in a logged command immediately apparent by aborting
13# the install, rather than failing silently and leaving a broken install.
14set -o pipefail
15
16MYSQL_BOOTSTRAP="/usr/sbin/mysqld --bootstrap --user=mysql --disable-log-bin --skip-grant-tables --default-storage-engine=myisam"
17
18case "$1" in
19 configure)
20 mysql_statedir=/usr/share/mysql
21 mysql_datadir=/var/lib/mysql
22 mysql_logdir=/var/log/mysql
23 mysql_rundir=/var/run/mysqld
24 mysql_cfgdir=/etc/mysql
25 mysql_upgradedir=/var/lib/mysql-upgrade
26
27 # If the following symlink exists, it is a preserved copy the old data dir
28 # created by the preinst script during a upgrade that would have otherwise
29 # been replaced by an empty mysql dir. This should restore it.
30 for dir in DATADIR LOGDIR; do
31
32 if [ "$dir" = "DATADIR" ]; then
33 targetdir=$mysql_datadir
34 else
35 targetdir=$mysql_logdir
36 fi
37
38 savelink="$mysql_upgradedir/$dir.link"
39 if [ -L "$savelink" ]; then
40 # If the targetdir was a symlink before we upgraded it is supposed
41 # to be either still be present or not existing anymore now.
42 if [ -L "$targetdir" ]; then
43 rm "$savelink"
44 elif [ ! -d "$targetdir" ]; then
45 mv "$savelink" "$targetdir"
46 else
47 # this should never even happen, but just in case...
48 mysql_tmp=`mktemp -d -t mysql-symlink-restore-XXXXXX`
49 echo "this is very strange! see $mysql_tmp/README..." >&2
50 mv "$targetdir" "$mysql_tmp"
51 cat << EOF > "$mysql_tmp/README"
52
53If you're reading this, it's most likely because you had replaced /var/lib/mysql
54with a symlink, then upgraded to a new version of mysql, and then dpkg
55removed your symlink (see #182747 and others). The mysql packages noticed
56that this happened, and as a workaround have restored it. However, because
57/var/lib/mysql seems to have been re-created in the meantime, and because
58we don't want to rm -rf something we don't know as much about, we are going
59to leave this unexpected directory here. If your database looks normal,
60and this is not a symlink to your database, you should be able to blow
61this all away.
62
63EOF
64 fi
65 fi
66 rmdir $mysql_upgradedir 2>/dev/null || true
67
68 done
69
70 # Ensure the existence and right permissions for the database and
71 # log files.
72 if [ ! -d "$mysql_statedir" -a ! -L "$mysql_statedir" ]; then mkdir "$mysql_statedir"; fi
73 if [ ! -d "$mysql_datadir" -a ! -L "$mysql_datadir" ]; then mkdir "$mysql_datadir" ; fi
74 if [ ! -d "$mysql_logdir" -a ! -L "$mysql_logdir" ]; then mkdir "$mysql_logdir" ; fi
75 # When creating an ext3 jounal on an already mounted filesystem like e.g.
76 # /var/lib/mysql, you get a .journal file that is not modifyable by chown.
77 # The mysql_statedir must not be writable by the mysql user under any
78 # circumstances as it contains scripts that are executed by root.
79 set +e
80 chown -R 0:0 $mysql_statedir
81 find $mysql_datadir ! -uid $(id -u mysql) -print0 | xargs -0 -r chown mysql
82 chown -R mysql:adm $mysql_logdir
83 chmod 2750 $mysql_logdir
84 set -e
85
86 # This is important to avoid dataloss when there is a removed
87 # mysql-server version from Woody lying around which used the same
88 # data directory and then somewhen gets purged by the admin.
89 db_set mariadb-server/postrm_remove_database false || true
90
91 # Clean up old flags before setting new one
92 rm -f $mysql_datadir/debian-*.flag
93 # Flag data dir to avoid downgrades
94 touch $mysql_datadir/debian-10.1.flag
95
96 # initiate databases. Output is not allowed by debconf :-(
97 # This will fail if we are upgrading an existing database; in this case
98 # mysql_upgrade, called from the /etc/init.d/mysql start script, will
99 # handle things.
100 # Debian: beware of the bashisms...
101 # Debian: can safely run on upgrades with existing databases
102 set +e
103 bash /usr/bin/mysql_install_db --skip-auth-anonymous-user --auth-root-authentication-method=socket --rpm --cross-bootstrap --user=mysql --disable-log-bin 2>&1 | $ERR_LOGGER
104 set -e
105
106
107 # Create the credentials file if not present. On all new installs the
108 # root account can be used directly for maintenance authenticated by
109 # unix socket and on new installs there is no need to define a
110 # separate Debian maintenance user account.
111 dc=$mysql_cfgdir/debian.cnf;
112 if [ ! -d "$mysql_cfgdir" ]; then
113 install -o 0 -g 0 -m 0755 -d $mysql_cfgdir
114 fi
115 if [ ! -e "$dc" ]; then
116 umask 066
117 cat /dev/null > $dc
118 umask 022
119 echo "# Automatically generated for Debian scripts. DO NOT TOUCH!" >>$dc
120 echo "[client]" >>$dc
121 echo "host = localhost" >>$dc
122 echo "user = root" >>$dc
123 echo "password = " >>$dc
124 echo "socket = $mysql_rundir/mysqld.sock" >>$dc
125 echo "[mysql_upgrade]" >>$dc
126 echo "host = localhost" >>$dc
127 echo "user = root" >>$dc
128 echo "password = " >>$dc
129 echo "socket = $mysql_rundir/mysqld.sock" >>$dc
130 echo "basedir = /usr" >>$dc
131 fi
132 # If this dir chmod go+w then the admin did it. But this file should not.
133 chown 0:0 $dc
134 chmod 0600 $dc
135
136 # Update privilege tables
137 password_column_fix_query=`/bin/echo -e \
138 "USE mysql;\n" \
139 "SET sql_log_bin=0;\n" \
140 "ALTER TABLE user CHANGE Password Password char(41) character set latin1 collate latin1_bin DEFAULT '' NOT NULL;"`
141
142 # Upgrade password column format.
143 # NOTE: $MYSQL_BOOTSTRAP requires one SQL statement per line, semicolon at the end.
144 echo "$password_column_fix_query" | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
145
146 ;;
147
148 abort-upgrade|abort-remove|abort-configure)
149 ;;
150
151 *)
152 echo "postinst called with unknown argument '$1'" 1>&2
153 exit 1
154 ;;
155esac
156
157db_stop # in case invoke failes
158
159# dh_systemd_start doesn't emit anything since we still ship /etc/init.d/mysql.
160# Thus MariaDB server is started via init.d script, which in turn redirects to
161# systemctl. If we upgrade from MySQL mysql.service may be masked, which also
162# means init.d script is disabled. Unmask mysql service explicitly.
163# Check first that the command exists, to avoid emitting any warning messages.
164if [ -x "$(command -v deb-systemd-helper)" ]; then
165 deb-systemd-helper unmask mysql.service > /dev/null
166fi
167
168# Automatically added by dh_systemd_enable
169# This will only remove masks created by d-s-h on package removal.
170deb-systemd-helper unmask mariadb.service >/dev/null || true
171
172# was-enabled defaults to true, so new installations run enable.
173if deb-systemd-helper --quiet was-enabled mariadb.service; then
174 # Enables the unit on first installation, creates new
175 # symlinks on upgrades if the unit file has changed.
176 deb-systemd-helper enable mariadb.service >/dev/null || true
177else
178 # Update the statefile to add new symlinks (if any), which need to be
179 # cleaned up on purge. Also remove old symlinks.
180 deb-systemd-helper update-state mariadb.service >/dev/null || true
181fi
182# End automatically added section
183# Automatically added by dh_systemd_enable
184if deb-systemd-helper debian-installed mariadb.service; then
185 # This will only remove masks created by d-s-h on package removal.
186 deb-systemd-helper unmask mariadb.service >/dev/null || true
187
188 if deb-systemd-helper --quiet was-enabled mariadb.service; then
189 # Create new symlinks, if any.
190 deb-systemd-helper enable mariadb.service >/dev/null || true
191 fi
192fi
193
194# Update the statefile to add new symlinks (if any), which need to be cleaned
195# up on purge. Also remove old symlinks.
196deb-systemd-helper update-state mariadb.service >/dev/null || true
197# End automatically added section
198# Automatically added by dh_installinit
199if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ]; then
200 if [ -x "/etc/init.d/mysql" ]; then
201 update-rc.d mysql defaults 19 21 >/dev/null || exit $?
202 fi
203fi
204# End automatically added section
205
206
207# Modified dh_systemd_start snippet that's not added automatically due /etc/init.d/mysql
208if [ -d /run/systemd/system ]; then
209 systemctl --system daemon-reload >/dev/null || true
210 deb-systemd-invoke start mariadb.service >/dev/null || true
211# Modified dh_installinit snippet to only run with sysvinit
212elif [ -x "/etc/init.d/mysql" ]; then
213 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ]; then
214 invoke-rc.d mysql start || exit $?
215 fi
216fi
217
218exit 0