Debian Codegraph @

debian/mysql-server-8.0.postinst

raw
1 #!/bin/bash
2
3 set -e
4
5 . /usr/share/debconf/confmodule
6
7 if [ -n "$DEBIAN_SCRIPT_DEBUG" ]; then set -v -x; DEBIAN_SCRIPT_TRACE=1; fi
8 ${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
9
10 export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
11
12 # This command can be used as pipe to syslog. With "-s" it also logs to stderr.
13 ERR_LOGGER="logger -p daemon.err -t mysqld_safe -i"
14
15 # Start the server with networking disabled and socket in a temporary location
16 # Usage: start_server <datadir> <tmpdir> <skip_grant>
17 # Params:
18 # datadir - Location of database files
19 # tmpdir - Used to store temporary pid and socket files
20 # skip_grant - If set, the server will be started with --skip-grant-tables
21 start_server() {
22 local datadir=$1
23 local tmpdir=$2
24 local skip_grant=$3
25 # If a database is already running, mysqld will keep trying to lock the files for ~100 seconds before failing.
26 # If the ibdata1 file is locked we fail immediately
27 if fuser "$datadir/ibdata1"; then
28 echo "ERROR: Database files are locked. Daemon already running?" >&2
29 return 1
30 fi
31 # The --daemonize flag makes the process fork, with the original exiting once the database is ready for use
32 if [ ! -z "$skip_grant" ]; then
33 mysqld --user=mysql --daemonize --socket="$tmpdir/mysqld.sock" --pid-file="$tmpdir/mysqld.pid" --skip-networking --skip-grant-tables
34 else
35 mysqld --user=mysql --daemonize --socket="$tmpdir/mysqld.sock" --pid-file="$tmpdir/mysqld.pid" --skip-networking
36 fi
37 }
38
39 # Shut down the server by sending a kill signal to the process
40 # Usage: stop_server <tmpdir>
41 # Params:
42 # tmpdir - Location of temporary pid-file
43 # Returns:
44 # 0 - Shutdown successful
45 # 1 - Shutdown took longer than 3 minutes
46 stop_server(){
47 local tmpdir=$1
48
49 # Check if pid file still exists before attempting to get the pid
50 local server_pid=$(cat "$tmpdir/mysqld.pid" 2>/dev/null || true)
51 if [ "$server_pid" = "" ]; then
52 return 0
53 fi
54
55 # Send kill signal
56 kill "$server_pid"
57
58 for i in $(seq 1 180); do
59 sleep 0.1 # A full second is too long, but we need to give the server _some_ time.
60 if ! $(ps $server_pid >/dev/null 2>&1); then
61 return 0
62 fi
63 sleep 0.9
64 done
65 # The server hasn't shut down in a timely manner
66 echo "Error: Unable to shut down server with process id $server_pid" >&2
67 return 1
68 }
69
70 # Runs an arbitrary init sql file supplied in $1. Does not require login access
71 run_init_sql() {
72 tmpdir=`mktemp -d`
73 chown mysql:mysql "$tmpdir"
74 mysqld --user=mysql --init-file="$1" --socket="$tmpdir/mysqld.sock" --pid-file="$tmpdir/mysqld.pid" > /dev/null 2>&1
75 result=$?
76 # Enforce server shutdown, waiting for it to complete so there are no conflicts over port 3306
77 stop_server "$tmpdir"
78 rm -rf "$tmpdir"
79 return $result
80 }
81
82 # To avoid having hardcoded paths in the script, we do a search on the path, as suggested at:
83 # https://www.debian.org/doc/manuals/developers-reference/ch06.en.html#bpp-debian-maint-scripts
84 pathfind() {
85 OLDIFS="$IFS"
86 IFS=:
87 for p in $PATH; do
88 if [ -x "$p/$*" ]; then
89 IFS="$OLDIFS"
90 return 0
91 fi
92 done
93 IFS="$OLDIFS"
94 return 1
95 }
96
97 invoke() {
98 if pathfind invoke-rc.d; then
99 invoke-rc.d mysql $1
100 else
101 /etc/init.d/mysql $1
102 fi
103 }
104
105 # Check if server is able to start. If it fails we abort early and refer
106 # the user to a wiki page with solutions for common configuration problems.
107 test_mysqld_startup() {
108 # mysqld --verbose --help will output a full listing of settings and plugins.
109 # To do so it needs to initialize the database, so it can be used as a test
110 # for whether or not the server can start. We redirect stdout to /dev/null so
111 # only the error messages are left.
112 result=0
113 output=$(mysqld --verbose --help --innodb-read-only 2>&1 > /dev/null) || result=$?
114 if [ ! "$result" = "0" ]; then
115 echo "ERROR: Unable to start MySQL server:" >&2
116 echo "$output" >&2
117 echo "Please take a look at https://wiki.debian.org/Teams/MySQL/FAQ for tips on fixing common upgrade issues." >&2
118 echo "Once the problem is resolved, run apt-get --fix-broken install to retry." >&2
119 fi
120 return $result
121 }
122
123 # The query cache feature has been removed in 8.0, but the 5.7 default config has settings
124 # for it, causing errors when starting the 8.0 server.
125 # To work around this we comment out these options if an old 5.7 config file is present.
126 fix_old_config_options() {
127 sed -e 's/[[:space:]]*query\_cache*/# Deprecated query cache option disabled by maintainer script\n#query\_cache/' \
128 /etc/mysql/mysql.conf.d/mysqld.cnf --in-place=.bak
129 # If nothing was changed, remove the new file
130 if cmp -s /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf.bak; then
131 mv /etc/mysql/mysql.conf.d/mysqld.cnf.bak /etc/mysql/mysql.conf.d/mysqld.cnf
132 fi
133 }
134
135 # Check if there is passwordless root login
136 # Usage: test_mysql_access <tmpdir>
137 # Params:
138 # tmpdir - Location of mysqld.sock file
139 test_mysql_access() {
140 local tmpdir=$1
141 mysql --no-defaults -u root --socket="$tmpdir/mysqld.sock" </dev/null >/dev/null 2>&1
142 }
143
144 this_version=5.7
145
146 # This is necessary because mysql_install_db removes the pid file in /var/run
147 # and because changed configuration options should take effect immediately.
148 # In case the server wasn't running at all it should be ok if the stop
149 # script fails. I can't tell at this point because of the cleaned /var/run.
150 set +e; invoke stop; set -e
151
152 case "$1" in
153 configure)
154 # INSERT ANY VERSIONED UPGRADE PATH HANDLING HERE AND NO LATER
155
156 # Versioned upgrade path handling must be done before freeze mode detection;
157 # otherwise the following freeze mode handling may cause upgrade path
158 # handling to be skipped over multiple version upgrades, meaning that the
159 # upgrade path handling never runs on some systems where freeze mode
160 # remains active for a long period.
161
162 if [ -e /etc/mysql/FROZEN -o -h /etc/mysql/FROZEN ]; then
163 error_message="MySQL has been frozen to prevent damage to your system. Please see /etc/mysql/FROZEN for help."
164 logger -p daemon.err -t /etc/init.d/mysql -i "$error_message"
165 db_input critical mysql-server-$this_version/installation_freeze_mode_active || true
166 db_go
167 db_stop
168 echo "$error_message" 1>&2
169 exit 0
170 fi
171
172 mysql_datadir=/usr/share/mysql
173 mysql_statedir=/var/lib/mysql
174 mysql_rundir=/var/run/mysqld
175 mysql_logdir=/var/log/mysql
176 mysql_cfgdir=/etc/mysql
177 mysql_upgradedir=/var/lib/mysql-upgrade
178 mysql_filesdir=/var/lib/mysql-files
179 mysql_keyringdir=/var/lib/mysql-keyring
180
181 # mysqld gets called during postinst configure, so any
182 # updates to the AppArmor profile must be loaded first (before the
183 # dh_apparmor snippet added by debhelper does it properly at the end of
184 # this script). Otherwise, mysqld cannot for example load
185 # /etc/mysql/mysqld.conf.d/ on upgrade from 5.5 to 5.6, which was added in
186 # 5.6 packaging but not present in the AppArmor profile shipped with 5.5
187 # packaging.
188 #
189 # This a workaround. Status is tracked at https://launchpad.net/bugs/1435368
190 if aa-status --enabled 2>/dev/null; then
191 # It is common for this to fail because
192 # /etc/apparmor.d/local/usr.sbin.mysqld doesn't exist (eg. on first
193 # install). But if this happens, then the workaround is not required,
194 # so it doesn't matter. If instead it causes a security issue, then
195 # that doesn't really matter here as dh_apparmor should handle that
196 # correctly later on.
197 apparmor_parser -r -T -W /etc/apparmor.d/usr.sbin.mysqld 2>/dev/null || true
198 fi
199
200 # New packaging paradigm for my.cnf as of Dec-2014 for sharing mysql
201 # variants in Ubuntu.
202 /usr/share/mysql-common/configure-symlinks install mysql "$mysql_cfgdir/mysql.cnf"
203
204 # Ensure the existence and right permissions for the database, socket
205 # folder, and log files.
206 for d in $mysql_statedir $mysql_filesdir $mysql_keyringdir $mysql_logdir $mysql_rundir
207 do
208 if [ ! -d "$d" -a ! -L "$d" ]; then mkdir "$d"; fi
209 chown -R mysql:mysql $d
210 chmod 0700 $d
211 done
212
213 # When creating an ext3 jounal on an already mounted filesystem like e.g.
214 # /var/lib/mysql, you get a .journal file that is not modifyable by chown.
215 # The mysql_datadir must not be writable by the mysql user under any
216 # circumstances as it contains scripts that are executed by root.
217 set +e
218 chown -R 0:0 $mysql_datadir
219 touch $mysql_logdir/error.log
220 chown -R mysql:adm $mysql_logdir
221 chmod 0750 $mysql_logdir
222 chmod 0640 $mysql_logdir/error.log
223 set -e
224
225 # This is important to avoid dataloss when there is a removed
226 # mysql-server version from Woody lying around which used the same
227 # data directory and then somewhen gets purged by the admin.
228 db_set mysql-server/postrm_remove_database false || true
229
230 # Fix old options that were deprecated in 5.5 and removed in 5.7
231 if dpkg --compare-versions "$2" le "8.0.16-0ubuntu3~"; then
232 echo "Renaming removed key_buffer and myisam-recover options (if present)"
233 fix_old_config_options
234 fi
235
236 # Sanity check to make sure the server can start
237 test_mysqld_startup
238
239 ## Generate debian maintenance user config using auth_socket authentication
240 #
241 # In previous releases, debian-sys-maint was authenticated with an unsecure
242 # random password. This has been replaced with auth_socket to verify that
243 # tools using it are run locally with root permissions.
244 dc=$mysql_cfgdir/debian.cnf;
245 if [ -e "$dc" -a -n "`fgrep mysql_upgrade $dc 2>/dev/null`" ]; then
246 # Remove password option in debian conf file to enforce auth_socket
247 # Password line may contain spaces/tabs followed by the key and value
248 sed -i '/^[[:space:]]*password *= */d' "$dc"
249 # Basedir is deprecated. Remove the option if it's in an existing debian.cnf
250 sed -i '/basedir/d' "$dc"
251 else
252 if [ ! -d "$mysql_cfgdir" ]; then install -o 0 -g 0 -m 0755 -d $mysql_cfgdir; fi
253 umask 066
254 cat /dev/null > $dc
255 umask 022
256 echo "# Automatically generated for Debian scripts. DO NOT TOUCH!" >>$dc
257 echo "[client]" >>$dc
258 echo "host = localhost" >>$dc
259 echo "user = debian-sys-maint" >>$dc
260 echo "socket = $mysql_rundir/mysqld.sock" >>$dc
261 echo "[mysql_upgrade]" >>$dc
262 echo "host = localhost" >>$dc
263 echo "user = debian-sys-maint" >>$dc
264 echo "socket = $mysql_rundir/mysqld.sock" >>$dc
265 fi
266
267 # Enforce root read-only permissions.
268 chown 0:0 $dc
269 chmod 0600 $dc
270
271 # Initiate database. Output is not allowed by debconf :-(
272 # If database doesn't exist we create it.
273 if [ ! "$(ls -A "${mysql_statedir}" -I "lost+found")" ] && [ -d "${mysql_filesdir}" ]; then
274 initfile=`mktemp --tmpdir=/var/lib/mysql-files/`
275 touch "$initfile"
276 chmod 600 "$initfile"
277 chown mysql:mysql "$initfile"
278 echo "USE mysql; " >> "$initfile"
279
280 # Install auth_socket plugin for debian-sys-maint and optionally root user
281 # This would throw an error if the plugin were already installed, but this
282 # should not be the case with the creation of a new database.
283 # Bug: http://bugs.mysql.com/bug.php?id=80642
284 echo "INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';" >> "$initfile"
285
286 # Use root password if provided, otherwise use auth_socket
287 db_get mysql-server/root_password && rootpw="$RET"
288 if [ ! -z "$rootpw" ]; then
289 rootpw=$(printf %q "${rootpw}")
290 echo "ALTER USER 'root'@'localhost' IDENTIFIED WITH 'caching_sha2_password' BY '$rootpw';" >> "$initfile"
291 else
292 echo "ALTER USER 'root'@'localhost' IDENTIFIED WITH 'auth_socket';" >> "$initfile"
293 fi
294
295 # Create debian maintenance user
296 echo "CREATE USER 'debian-sys-maint'@'localhost' IDENTIFIED WITH 'auth_socket' AS 'root';" >> "$initfile"
297 echo "GRANT ALL ON *.* TO 'debian-sys-maint'@'localhost' WITH GRANT OPTION;" >> "$initfile"
298 echo "SHUTDOWN;" >> "$initfile"
299 mysqld --initialize-insecure --user=mysql --init-file="$initfile"> /dev/null 2>&1
300 rm "$initfile"
301 else
302 # Confirm auth_socket plugin is installed, then convert auth for debian-sys-maint user
303 # INSTALL PLUGIN throws an error if the plugin exists, so check for it first.
304 # Bug: http://bugs.mysql.com/bug.php?id=80642
305 authupdatefile=`mktemp --tmpdir=/var/lib/mysql-files/`
306 touch "$authupdatefile"
307 chmod 600 "$authupdatefile"
308 chown mysql:mysql "$authupdatefile"
309 echo "USE mysql; " >> "$authupdatefile"
310 echo "SELECT COUNT(*) INTO @plugin_exists FROM information_schema.plugins WHERE PLUGIN_NAME = 'auth_socket';" >> "$authupdatefile"
311 echo "SET @query = IF(@plugin_exists = 0, 'INSTALL PLUGIN auth_socket SONAME "\""auth_socket.so"\"";', 'SELECT 1;');" >> "$authupdatefile"
312 echo "PREPARE stmt FROM @query;" >> "$authupdatefile"
313 echo "EXECUTE stmt;" >> "$authupdatefile"
314 echo "DEALLOCATE PREPARE stmt;" >> "$authupdatefile"
315 echo "ALTER USER 'debian-sys-maint'@'localhost' IDENTIFIED WITH 'auth_socket' AS 'root';" >> "$authupdatefile"
316 echo "SHUTDOWN;" >> "$authupdatefile"
317 run_init_sql "$authupdatefile"
318 rm "$authupdatefile"
319 fi
320
321 # To avoid downgrades. This has to happen after the database is created, or --initialize will fail
322 touch $mysql_statedir/debian-5.7.flag
323
324 ;;
325
326 abort-upgrade|abort-remove|abort-configure)
327 ;;
328
329 *)
330 echo "postinst called with unknown argument '$1'" 1>&2
331 exit 1
332 ;;
333 esac
334
335 # forget we ever saw the password. don't use reset to keep the seen status
336 db_set mysql-server/root_password ""
337 db_set mysql-server/root_password_again ""
338 db_stop # in case invoke failes
339
340 #DEBHELPER#
341
342 exit 0