Debian Codegraph @

debian/mysql-server-8.0.preinst

raw
1 #!/bin/bash
2 #
3 # summary of how this script can be called:
4 # * <new-preinst> install
5 # * <new-preinst> install <old-version>
6 # * <new-preinst> upgrade <old-version>
7 # * <old-preinst> abort-upgrade <new-version>
8 #
9
10 set -e
11
12 . /usr/share/debconf/confmodule
13
14 if [ -n "$DEBIAN_SCRIPT_DEBUG" ]; then set -v -x; DEBIAN_SCRIPT_TRACE=1; fi
15 ${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
16
17 export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
18 MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
19 DATADIR=/var/lib/mysql
20 LOGDIR=/var/log/mysql
21 UPGRADEDIR=/var/lib/mysql-upgrade
22
23 MYSQL_UPGRADE_INFO_FILE="$DATADIR/mysql_upgrade_info"
24 MARIADB_UPGRADE_INFO_FILE="$DATADIR/mariadb_upgrade_info"
25
26 # To avoid having hardcoded paths in the script, we do a search on the path, as suggested at:
27 # https://www.debian.org/doc/manuals/developers-reference/ch06.en.html#bpp-debian-maint-scripts
28 pathfind() {
29 OLDIFS="$IFS"
30 IFS=:
31 for p in $PATH; do
32 if [ -x "$p/$*" ]; then
33 IFS="$OLDIFS"
34 return 0
35 fi
36 done
37 IFS="$OLDIFS"
38 return 1
39 }
40
41 # Try to stop the server in a sane way. If it does not success let the admin
42 # do it himself. No database directories should be removed while the server
43 # is running! Another mysqld in e.g. a different chroot is fine for us.
44 stop_server() {
45 if [ ! -x /etc/init.d/mysql ]; then return; fi
46
47 set +e
48 if pathfind invoke-rc.d; then
49 cmd="invoke-rc.d mysql stop"
50 else
51 cmd="/etc/init.d/mysql stop"
52 fi
53 $cmd
54 errno=$?
55 set -e
56
57 # 0=ok, 100=no init script (fresh install)
58 if [ "$errno" != 0 -a "$errno" != 100 ]; then
59 echo "${cmd/ */} returned $errno" 1>&2
60 echo "There is a MySQL server running, but we failed in our attempts to stop it." 1>&2
61 echo "Stop it yourself and try again!" 1>&2
62 db_stop
63 exit 1
64 fi
65 }
66
67 ################################ main() ##########################
68
69 this_version=8.0
70 previous_version=8.0
71
72 # Abort if an NDB cluster is in use.
73 if egrep -qi -r '^[^#]*ndb.connectstring|^[[:space:]]*\[[[:space:]]*ndb_mgmd' /etc/mysql/; then
74 db_fset mysql-server/no_upgrade_when_using_ndb seen false || true
75 db_input high mysql-server/no_upgrade_when_using_ndb || true
76 db_go
77 db_stop
78 exit 1
79 fi
80
81 # Check for previous version indicator in upgrade info file
82 if [[ -f "$MYSQL_UPGRADE_INFO_FILE" ]]; then
83 previous_version=$(<"$MYSQL_UPGRADE_INFO_FILE")
84 fi
85
86 if [[ -f "$MARIADB_UPGRADE_INFO_FILE" ]]; then
87 previous_version=$(<"$MARIADB_UPGRADE_INFO_FILE")
88 fi
89
90 # Check if MariaDB database already exists in data directory using upgrade info
91 mariadb_exists=0
92
93 if [[ "$previous_version" == *"MariaDB"* ]]; then
94 mariadb_exists=1
95 fi
96
97 # Move MariaDB Server data to new location to avoid data loss.
98 if [ "$mariadb_exists" = 1 ]; then
99 db_input critical "mysql-server-$this_version/old_mariadb_data_directory_saved" || true
100 db_go
101 echo "The contents of $DATADIR/ indicates a MariaDB database" 1>&2
102 echo "already exists, which is incompatible with MySQL. Therefore the" 1>&2
103 echo "previous data directory will be renamed to $DATADIR-$previous_version and" 1>&2
104 echo "a new data directory will be initialized at $DATADIR." 1>&2
105 echo "Please manually export/import your data (e.g. with mysqldump) if needed." 1>&2
106 mv -f "$DATADIR" "$DATADIR-$previous_version"
107 # Also move away the old debian.cnf file that included credentials that are
108 # no longer valid. If none existed, ignore error and let dpkg continue.
109 mv -f /etc/mysql/debian.cnf "/etc/mysql/debian.cnf-$previous_version" || true
110 fi
111
112 # Safe the user from stupidities.
113 is_downgrade_attempt=0
114 for i in `ls $DATADIR/debian-*.flag 2>/dev/null`; do
115 found_version=`echo $i | sed 's/.*debian-\([0-9\.]\+\).flag/\1/'`
116 if dpkg --compare-versions "$this_version" '<<' "$found_version"; then
117 is_downgrade_attempt=1
118 break;
119 fi
120 done
121 if [ "$is_downgrade_attempt" = 1 ]; then
122 echo "Downgrade from (at least) $found_version to $this_version is not possible." 1>&2
123 if [ -e /etc/mysql/FROZEN -o -h /etc/mysql/FROZEN ]; then
124 echo "Warning: /etc/mysql/FROZEN already exists" >&2
125 else
126 ln -s ../../usr/share/doc/mysql-common/frozen-mode/downgrade /etc/mysql/FROZEN
127 fi
128 fi
129
130 if [ -e /etc/mysql/FROZEN -o -h /etc/mysql/FROZEN ]; then
131 error_message="MySQL has been frozen to prevent damage to your system. Please see /etc/mysql/FROZEN for help."
132 logger -p daemon.err -t /etc/init.d/mysql -i "$error_message"
133 db_fset mysql-server-$this_version/installation_freeze_mode_active seen false || true
134 db_input critical mysql-server-$this_version/installation_freeze_mode_active || true
135 db_go
136 db_stop
137 echo "$error_message" 1>&2
138 exit 0
139 fi
140
141 # to be sure
142 stop_server
143
144 # If we use NIS then errors should be tolerated. It's up to the
145 # user to ensure that the mysql user is correctly setup.
146 # Beware that there are two ypwhich one of them needs the 2>/dev/null!
147 if test -n "`which ypwhich 2>/dev/null`" && ypwhich >/dev/null 2>&1; then
148 set +e
149 fi
150
151 #
152 # Now we have to ensure the following state:
153 # /etc/passwd: mysql:x:100:101:MySQL Server:/nonexistent:/bin/false
154 # /etc/group: mysql:x:101:
155 #
156 # Sadly there could any state be present on the system so we have to
157 # modify everything carefully i.e. not doing a chown before creating
158 # the user etc...
159 #
160 systemd-sysusers ${DPKG_ROOT:+--root="$DPKG_ROOT"} --inline 'u! mysql - "MySQL Server" /nonexistent /bin/false'
161
162 # end of NIS tolerance zone
163 set -e
164
165 # if there's a symlink, let's store where it's pointing, because otherwise
166 # it's going to be lost in some situations
167 for dir in DATADIR LOGDIR; do
168 checkdir=`eval echo "$"$dir`
169 if [ -L "$checkdir" ]; then
170 mkdir -p "$UPGRADEDIR"
171 cp -dT "$checkdir" "$UPGRADEDIR/$dir.link"
172 fi
173 done
174
175 # creating mysql home directory
176 if [ ! -d $DATADIR -a ! -L $DATADIR ]; then
177 mkdir $DATADIR
178 fi
179
180 # checking disc space
181 if LC_ALL=C BLOCKSIZE= df --portability $DATADIR/. | tail -n 1 | awk '{ exit ($4>1000) }'; then
182 echo "ERROR: There's not enough space in $DATADIR/" 1>&2
183 db_stop
184 exit 1
185 fi
186
187 # Since the home directory was created before putting the user into
188 # the mysql group and moreover we cannot guarantee that the
189 # permissions were correctly *before* calling this script, we fix them now.
190 # In case we use NIS and no mysql user is present then this script should
191 # better fail now than later..
192 # The "set +e" is necessary as e.g. a ".journal" of a ext3 partition is
193 # not chgrp'able (#318435).
194 set +e
195 chown mysql:mysql $DATADIR
196 find $DATADIR -follow -not -group mysql -print0 2>/dev/null \
197 | xargs -0 --no-run-if-empty chgrp mysql
198 set -e
199
200
201 db_stop
202
203 #DEBHELPER#
204
205 exit 0