Debian Codegraph @

debian/mariadb-server.preinst

raw
1 #!/bin/bash -e
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 # shellcheck source=/dev/null
11 . /usr/share/debconf/confmodule
12
13 # Just kill the invalid insserv.conf.d directory without fallback
14 if [ -d "/etc/insserv.conf.d/mariadb/" ]
15 then
16 rm -rf "/etc/insserv.conf.d/mariadb/"
17 fi
18
19 if [ -n "$DEBIAN_SCRIPT_DEBUG" ]
20 then
21 set -v -x
22 DEBIAN_SCRIPT_TRACE=1
23 fi
24 ${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
25
26 export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
27 mariadb_datadir=/var/lib/mariadb
28 legacy_mariadb_datadir=/var/lib/mysql
29 mariadb_upgradedir=/var/lib/mysql-upgrade
30
31 MARIADBD_USERS="root"
32
33 # Check if user 'mysql' exists before referring to it in pgrep
34 # to avoid pgrep erroring on 'invalid user name'
35 if id mysql >/dev/null 2>&1
36 then
37 MARIADBD_USERS="$MARIADBD_USERS,mysql"
38 fi
39
40 # Try to stop the server in a sane way. If it does not success let the admin
41 # do it himself. No database directories should be removed while the server
42 # is running! Another mariadbd in e.g. a different chroot is fine for us.
43 stop_server() {
44 # Return immediately if there are no mysqld processes running on a host
45 # (leave containerized processes with the same name in other namespaces)
46 # as there is no point in trying to shutdown in that case.
47 if ! pgrep -x -u "$MARIADBD_USERS" --nslist pid --ns $$ "mysqld|mariadbd" > /dev/null
48 then
49 return
50 fi
51
52 set +e
53 invoke-rc.d mariadb stop
54 invoke-rc.d mysql stop # Backwards compatibility
55 errno=$?
56 set -e
57
58 # systemctl could emit exit code 100=no init script (fresh install)
59 if [ "$errno" != 0 ] && [ "$errno" != 100 ]
60 then
61 echo "Attempt to stop MariaDB/MySQL server returned exitcode $errno" 1>&2
62 echo "There is a MariaDB/MySQL server running, but we failed in our attempts to stop it." 1>&2
63 echo "Check if there is any server running with 'pgrep -af \"mysqld|mariadbd\"' and" 1>&2
64 echo "try to stop it yourself by issuing 'invoke-rc.d mariadb stop'." 1>&2
65 db_stop
66 exit 1
67 fi
68 }
69
70 # Check if the version file mariadb_upgrade_info or mysql_upgrade_info is
71 # included with the provided data directory. If it is a MariaDB version, then
72 # return the major version number only (XX.XX). Return empty if this directory
73 # does not belong to MariaDB.
74 get_mariadb_upgrade_info_major_version() {
75 datadir="$1"
76
77 # Check both mariadb_upgrade_info (higher priority) and mysql_upgrade_info.
78 if [ -f "$datadir/mysql_upgrade_info" ]
79 then
80 read -r full_version < "$datadir/mysql_upgrade_info"
81 fi
82
83 if [ -f "$datadir/mariadb_upgrade_info" ]
84 then
85 read -r full_version < "$datadir/mariadb_upgrade_info"
86 fi
87
88 # If version is not empty and contains 'MariaDB', get the version number.
89 if [ -n "$full_version" ] && echo "$full_version" | grep -q "MariaDB"
90 then
91 # The major version number should the first two components of the version
92 # string (e.g. 11.8 from 11.8.6).
93 echo "$full_version" | awk -F'.' '{print $1"."$2}'
94 fi
95 }
96
97 ################################ main() ##########################
98
99 this_version=__MARIADB_MAJOR_VER__
100 max_upgradeable_version=5.7
101
102 # Check for previous mariadb version in the new datadir.
103 found_version=$(get_mariadb_upgrade_info_major_version "$mariadb_datadir")
104
105 # If nothing was found in the new datadir, check the legacy datadir to see if
106 # it has a MariaDB instance.
107 if [ -z "$found_version" ] && [ -d "$legacy_mariadb_datadir" ]
108 then
109 found_version=$(get_mariadb_upgrade_info_major_version "$legacy_mariadb_datadir")
110
111 # If a mariadb version was found in the legacy datadir, continue using it.
112 if [ -n "$found_version" ]
113 then
114 mariadb_datadir="$legacy_mariadb_datadir"
115 fi
116 fi
117
118 # If an upgrade is detected, proceed with it automatically without
119 # requiring any user interaction.
120 #
121 # However, if the user attempts to downgrade, warn about the incompatibility.
122 # Downgrade is detected if the flag version is bigger than $this_version
123 # (e.g. 10.1 > 10.0) or the flag version is smaller than 10.0 but bigger
124 # than $max_upgradeable_version.
125 if [ -n "$found_version" ]
126 then
127 echo "$mariadb_datadir: found previous version $found_version"
128
129 if dpkg --compare-versions "$found_version" '>>' "$this_version"
130 then
131 downgrade_detected=true
132 fi
133
134 if dpkg --compare-versions "$found_version" '>>' "$max_upgradeable_version" \
135 && dpkg --compare-versions "$found_version" '<<' "10.0"
136 then
137 downgrade_detected=true
138 fi
139
140 fi
141
142 # Don't abort dpkg if downgrade is detected (as was done previously).
143 # Instead simply move the old datadir and create a new for this_version.
144 if [ -n "$downgrade_detected" ]
145 then
146 db_input critical "mariadb-server/old_data_directory_saved" || true
147 db_go
148 echo "The contents of $mariadb_datadir/ indicates a" 1>&2
149 echo "version that cannot automatically be upgraded. Therefore the" 1>&2
150 echo "previous data directory will be renamed to $mariadb_datadir-$found_version and" 1>&2
151 echo "a new data directory will be initialized at $mariadb_datadir." 1>&2
152 echo "Please manually export/import your data (e.g. with mysqldump) if needed." 1>&2
153 mv -f "$mariadb_datadir" "$mariadb_datadir-$found_version"
154 # Also move away the old debian.cnf file that included credentials that are
155 # no longer valid. If none existed, ignore error and let dpkg continue.
156 mv -f /etc/mysql/debian.cnf "/etc/mysql/debian.cnf-$found_version" || true
157 fi
158
159 # to be sure
160 stop_server
161
162 # if there's a symlink, let's store where it's pointing, because otherwise
163 # it's going to be lost in some situations
164 for dir in DATADIR LOGDIR
165 do
166 checkdir=$(eval echo "$"$dir)
167 if [ -L "$checkdir" ]
168 then
169 # Use mkdir option 'Z' to create with correct SELinux context.
170 mkdir -pZ "$mariadb_upgradedir"
171 cp -dT "$checkdir" "$mariadb_upgradedir/$dir.link"
172 fi
173 done
174
175 # creating mysql home directory
176 if [ ! -d $mariadb_datadir ] && [ ! -L $mariadb_datadir ]
177 then
178 # Use mkdir option 'Z' to create with correct SELinux context.
179 mkdir -Z $mariadb_datadir
180 fi
181
182 # As preset blocksize of GNU df is 1024 then available bytes is $df_available_blocks * 1024
183 # 4096 blocks is then lower than 4 MB
184 df_available_blocks="$(LC_ALL=C BLOCKSIZE='' df --output=avail "$mariadb_datadir" | tail -n 1)"
185 if [ "$df_available_blocks" -lt "4096" ]
186 then
187 echo "ERROR: There's not enough space in $mariadb_datadir/" 1>&2
188 db_stop
189 exit 1
190 fi
191
192 db_stop
193
194 #DEBHELPER#
195
196 # dh_installinit/13.11.3 adds this check but only with 'install', so we need to
197 # have and extra one to check 'upgrade'. This ensures that upgrades from
198 # mariadb-server-x.y to mariadb-server (without version suffix) ends up with
199 # the executable bit set on /etc/init.d/mariadb, which otherwise would end up
200 # disabled due to the mariadb-server-x.y.postrm being triggered.
201 # $1 = upgrade
202 # $2 = 1:10.6.11-2
203 if [ "$1" = "upgrade" ] && [ -n "$2" ] && [ -e "/etc/init.d/mariadb" ]
204 then
205 chmod +x "/etc/init.d/mariadb" >/dev/null || true
206 fi
207
208 # dh_installinit/13.11.3 adds this check but with extra condition that there
209 # must be a version passed as '$2', but that will always be empty when install
210 # runs after the unpack that is retriggered for package 'mariadb-server' when
211 # the old 'mariadb-server-10.6' is purged, so we need to repeat the same check
212 # here without any expectation for '$2'. This ensures that upgrades from
213 # mariadb-server-x.y to mariadb-server (without version suffix) ends up with the
214 # executable bit set on /etc/init.d/mariadb.
215 if [ "$1" = "install" ] && [ -e "/etc/init.d/mariadb" ]
216 then
217 chmod +x "/etc/init.d/mariadb" >/dev/null || true
218 fi