Advertisement
Guest User

/var/lib/dpkg/info/mysql-server-5.5.postinst

a guest
Mar 10th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.03 KB | None | 0 0
  1. #!/bin/bash -e
  2.  
  3. . /usr/share/debconf/confmodule
  4.  
  5. if [ -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.  
  8. export 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.
  11. ERR_LOGGER="logger -p daemon.err -t mysqld_safe -i"
  12.  
  13. invoke() {
  14.   if [ -x /usr/sbin/invoke-rc.d ]; then
  15.     invoke-rc.d mysql $1
  16.   else
  17.     /etc/init.d/mysql $1
  18.   fi
  19. }
  20.  
  21. MYSQL_BOOTSTRAP="/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables"
  22.  
  23. test_mysql_access() {
  24.        mysql --no-defaults -u root -h localhost </dev/null >/dev/null 2>&1
  25. }
  26.  
  27. # call with $1 = "online" to connect to the server, otherwise it bootstraps
  28. set_mysql_rootpw() {
  29.        # forget we ever saw the password.  don't use reset to keep the seen status
  30.        db_set mysql-server/root_password ""
  31.        db_set mysql-server/root_password_again ""
  32.  
  33.        tfile=`mktemp`
  34.        if [ ! -f "$tfile" ]; then
  35.                return 1
  36.        fi
  37.  
  38.        # this avoids us having to call "test" or "[" on $rootpw
  39.        cat << EOF > $tfile
  40. USE mysql;
  41. UPDATE user SET password=PASSWORD("$rootpw") WHERE user='root';
  42. FLUSH PRIVILEGES;
  43. EOF
  44.        if grep -q 'PASSWORD("")' $tfile; then
  45.                retval=0
  46.        elif [ "$1" = "online" ]; then
  47.                mysql --no-defaults -u root -h localhost <$tfile >/dev/null
  48.                retval=$?
  49.        else
  50.                $MYSQL_BOOTSTRAP <$tfile
  51.                retval=$?
  52.        fi
  53.        rm -f $tfile
  54.        return $retval
  55. }
  56.  
  57. # This is necessary because mysql_install_db removes the pid file in /var/run
  58. # and because changed configuration options should take effect immediately.
  59. # In case the server wasn't running at all it should be ok if the stop
  60. # script fails. I can't tell at this point because of the cleaned /var/run.
  61. set +e; invoke stop; set -e
  62.    
  63. case "$1" in
  64.   configure)
  65.     mysql_datadir=/usr/share/mysql
  66.     mysql_statedir=/var/lib/mysql
  67.     mysql_rundir=/var/run/mysqld
  68.     mysql_logdir=/var/log
  69.     mysql_cfgdir=/etc/mysql
  70.     mysql_newlogdir=/var/log/mysql
  71.     mysql_upgradedir=/var/lib/mysql-upgrade
  72.  
  73.     # first things first, if the following symlink exists, it is a preserved
  74.     # copy the old data dir from a mysql upgrade that would have otherwise
  75.     # been replaced by an empty mysql dir.  this should restore it.
  76.     for dir in DATADIR LOGDIR; do
  77.         if [ "$dir" = "DATADIR" ]; then targetdir=$mysql_statedir; else targetdir=$mysql_newlogdir; fi
  78.         savelink="$mysql_upgradedir/$dir.link"
  79.         if [ -L "$savelink" ]; then
  80.             # If the targetdir was a symlink before we upgraded it is supposed
  81.             # to be either still be present or not existing anymore now.
  82.             if [ -L "$targetdir" ]; then
  83.                 rm "$savelink"
  84.             elif [ ! -d "$targetdir" ]; then
  85.                 mv "$savelink" "$targetdir"
  86.             else
  87.                 # this should never even happen, but just in case...
  88.                 mysql_tmp=`mktemp -d -t mysql-symlink-restore-XXXXXX`
  89.                 echo "this is very strange!  see $mysql_tmp/README..." >&2
  90.                 mv "$targetdir" "$mysql_tmp"
  91.                 cat << EOF > "$mysql_tmp/README"
  92.  
  93. if you're reading this, it's most likely because you had replaced /var/lib/mysql
  94. with a symlink, then upgraded to a new version of mysql, and then dpkg
  95. removed your symlink (see #182747 and others).  the mysql packages noticed
  96. that this happened, and as a workaround have restored it.  however, because
  97. /var/lib/mysql seems to have been re-created in the meantime, and because
  98. we don't want to rm -rf something we don't know as much about, we're going
  99. to leave this unexpected directory here.  if your database looks normal,
  100. and this is not a symlink to your database, you should be able to blow
  101. this all away.
  102.  
  103. EOF
  104.            fi
  105.        fi
  106.     rmdir $mysql_upgradedir 2>/dev/null || true
  107.    done
  108.    
  109.    # Ensure the existence and right permissions for the database and
  110.    # log files.
  111.    if [ ! -d "$mysql_statedir"       -a ! -L "$mysql_statedir"       ]; then mkdir "$mysql_statedir"; fi
  112.    if [ ! -d "$mysql_statedir/mysql" -a ! -L "$mysql_statedir/mysql" ]; then mkdir "$mysql_statedir/mysql"; fi
  113.    if [ ! -d "$mysql_newlogdir"      -a ! -L "$mysql_newlogdir"      ]; then mkdir "$mysql_newlogdir"; fi
  114.    # When creating an ext3 jounal on an already mounted filesystem like e.g.
  115.    # /var/lib/mysql, you get a .journal file that is not modifyable by chown.
  116.    # The mysql_datadir must not be writable by the mysql user under any
  117.    # circumstances as it contains scripts that are executed by root.
  118.    set +e
  119.    chown -R 0:0 $mysql_datadir
  120.    chown -R mysql $mysql_statedir
  121.    chmod 700 $mysql_statedir $mysql_statedir/mysql
  122.    if [ ! -d "$mysql_rundir" ]; then mkdir "$mysql_rundir"; fi
  123.    chown -R mysql $mysql_rundir
  124.     chown -R mysql:adm $mysql_newlogdir;    chmod 2750 $mysql_newlogdir;
  125.    for i in log err; do
  126.      touch             $mysql_logdir/mysql.$i
  127.      chown mysql:adm   $mysql_logdir/mysql.$i
  128.      chmod 0640        $mysql_logdir/mysql.$i
  129.    done
  130.    set -e
  131.  
  132.    # This is important to avoid dataloss when there is a removed
  133.    # mysql-server version from Woody lying around which used the same
  134.    # data directory and then somewhen gets purged by the admin.
  135.    db_set mysql-server/postrm_remove_database false || true
  136.  
  137.    # To avoid downgrades.
  138.    touch $mysql_statedir/debian-5.5.flag
  139.  
  140.    # initiate databases. Output is not allowed by debconf :-(
  141.    # Debian: beware of the bashisms...
  142.    # Debian: can safely run on upgrades with existing databases
  143.    set +e
  144.    bash /usr/bin/mysql_install_db --rpm 2>&1 | $ERR_LOGGER
  145.    if [ "$?" != "0" ]; then
  146.      echo "ATTENTION: An error has occured. More info is in the syslog!"
  147.    fi
  148.    set -e
  149.    
  150.    ## On every reconfiguration the maintenance user is recreated.
  151.    #
  152.    # - It is easier to regenerate the password every time but as people
  153.    #   use fancy rsync scripts and file alteration monitors, the existing
  154.    #   password is used and existing files not touched.
  155.    # - The mysqld statement is like that in mysql_install_db because the
  156.    #   server is not already running. This has some implications:
  157.     #   - The amount of newlines and semicolons in the query is important!
  158.    #   - GRANT is not possible with --skip-grant-tables and "INSERT
  159.    #     (user,host..) VALUES" is not --ansi compliant
  160.    # - The echo is just for readability. ash's buildin has no "-e" so use /bin/echo.
  161.     # - The Super_priv, Show_db_priv, Create_tmp_table_priv and Lock_tables_priv
  162.     #   may not be present as old Woody 3.23 databases did not have it and the
  163.     #   admin might not already have run mysql_upgrade which adds them.
  164.     #   As the binlog cron scripts to need at least the Super_priv, I do first
  165.     #   the old query which always succeeds and then the new which may or may not.
  166.  
  167.     # recreate the credentials file if not present or without mysql_upgrade stanza
  168.     dc=$mysql_cfgdir/debian.cnf;
  169.     if [ -e "$dc" -a -n "`fgrep mysql_upgrade $dc 2>/dev/null`" ]; then
  170.         pass="`sed -n 's/^[     ]*password *= *// p' $dc | head -n 1`"
  171.     else
  172.     pass=`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..16)'`;
  173.         if [ ! -d "$mysql_cfgdir" ]; then install -o 0 -g 0 -m 0755 -d $mysql_cfgdir; fi
  174.         cat /dev/null > $dc
  175.         echo "# Automatically generated for Debian scripts. DO NOT TOUCH!" >>$dc
  176.         echo "[client]"                                                    >>$dc
  177.         echo "host     = localhost"                                        >>$dc
  178.         echo "user     = debian-sys-maint"                                 >>$dc
  179.         echo "password = $pass"                                            >>$dc
  180.         echo "socket   = $mysql_rundir/mysqld.sock"                        >>$dc
  181.         echo "[mysql_upgrade]"                                             >>$dc
  182.         echo "host     = localhost"                                        >>$dc
  183.         echo "user     = debian-sys-maint"                                 >>$dc
  184.         echo "password = $pass"                                            >>$dc
  185.         echo "socket   = $mysql_rundir/mysqld.sock"                        >>$dc
  186.         echo "basedir  = /usr"                                             >>$dc
  187.     fi
  188.     # If this dir chmod go+w then the admin did it. But this file should not.
  189.     chown 0:0 $dc
  190.     chmod 0600 $dc
  191.  
  192.     # update privilege tables
  193.     password_column_fix_query=`echo -e \
  194.         "USE mysql\n" \
  195.         "ALTER TABLE user CHANGE Password Password char(41) character set latin1 collate latin1_bin DEFAULT '' NOT NULL"`;
  196.     replace_query=`echo -e \
  197.         "USE mysql\n" \
  198.         "REPLACE INTO user SET " \
  199.         "  host='localhost', user='debian-sys-maint', password=password('$pass'), " \
  200.         "  Select_priv='Y', Insert_priv='Y', Update_priv='Y', Delete_priv='Y', " \
  201.         "  Create_priv='Y', Drop_priv='Y', Reload_priv='Y', Shutdown_priv='Y', " \
  202.         "  Process_priv='Y',  File_priv='Y', Grant_priv='Y', References_priv='Y', " \
  203.         "  Index_priv='Y', Alter_priv='Y', Super_priv='Y', Show_db_priv='Y', "\
  204.         "  Create_tmp_table_priv='Y', Lock_tables_priv='Y', Execute_priv='Y', "\
  205.         "  Repl_slave_priv='Y', Repl_client_priv='Y', Create_view_priv='Y', "\
  206.         "  Show_view_priv='Y', Create_routine_priv='Y', Alter_routine_priv='Y', "\
  207.         "  Create_user_priv='Y', Event_priv='Y', Trigger_priv='Y' "`;
  208.     fix_privs=`echo -e \
  209.         "USE mysql;\n" \
  210.         "ALTER TABLE user ADD column Create_view_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'; " \
  211.         "ALTER TABLE user ADD column Show_view_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'; " \
  212.         "ALTER TABLE user ADD column Create_routine_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'; " \
  213.         "ALTER TABLE user ADD column Alter_routine_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'; " \
  214.         "ALTER TABLE user ADD column Create_user_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'; " \
  215.         "ALTER TABLE user ADD column Event_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'; " \
  216.         "ALTER TABLE user ADD column Trigger_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'; " `
  217.     # Engines supported by etch should be installed per default. The query sequence is supposed
  218.     # to be aborted if the CREATE TABLE fails due to an already existent table in which case the
  219.     # admin might already have chosen to remove one or more plugins. Newlines are necessary.
  220.     install_plugins=`echo -e \
  221.         "USE mysql;\n" \
  222.         "CREATE TABLE plugin (name char(64) COLLATE utf8_bin NOT NULL DEFAULT '', " \
  223.         "  dl char(128) COLLATE utf8_bin NOT NULL DEFAULT '', " \
  224.         "  PRIMARY KEY (name)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='MySQL plugins';\n" \
  225.         "INSERT INTO plugin VALUES ('innodb',    'ha_innodb.so');\n" \
  226.         "INSERT INTO plugin VALUES ('federated', 'ha_federated.so');\n" \
  227.         "INSERT INTO plugin VALUES ('blackhole', 'ha_blackhole.so');\n" \
  228.         "INSERT INTO plugin VALUES ('archive',   'ha_archive.so');" `
  229.  
  230.     # Upgrade password column format before the root password gets set.
  231.     echo "$password_column_fix_query"                        | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
  232.  
  233.     db_get mysql-server/root_password && rootpw="$RET"
  234.     if ! set_mysql_rootpw; then
  235.         password_error="yes"
  236.     fi
  237.  
  238.     echo "$fix_privs"                                        | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
  239.     echo "$replace_query"                                    | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
  240.     set +e
  241.     echo "$install_plugins"                                  | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
  242.     set -e
  243.   ;;
  244.  
  245.   abort-upgrade|abort-remove|abort-configure)
  246.   ;;
  247.  
  248.   *)
  249.     echo "postinst called with unknown argument '$1'" 1>&2
  250.     exit 1
  251.   ;;
  252. esac
  253.  
  254. # here we check to see if we can connect as root without a password
  255. # this should catch upgrades from previous versions where the root
  256. # password wasn't set.  if there is a password, or if the connection
  257. # fails for any other reason, nothing happens.
  258. if [ "$1" = "configure" ]; then
  259.        if test_mysql_access; then
  260.                db_input medium mysql-server/root_password || true
  261.                db_go
  262.                db_get mysql-server/root_password && rootpw="$RET"
  263.  
  264.                if ! set_mysql_rootpw "online"; then
  265.                        password_error="yes"
  266.                fi
  267.        fi
  268.  
  269.        if [ "$password_error" = "yes" ]; then
  270.                db_input high mysql-server/error_setting_password || true
  271.                db_go
  272.        fi
  273.  
  274. fi
  275.  
  276. db_stop # in case invoke failes
  277.  
  278. # Automatically added by dh_apparmor
  279. if [ "$1" = "configure" ]; then
  280.     APP_PROFILE=/etc/apparmor.d/usr.sbin.mysqld
  281.     if [ -f "$APP_PROFILE" ]; then
  282.         # Add the local/ include
  283.         LOCAL_APP_PROFILE=/etc/apparmor.d/local/usr.sbin.mysqld
  284.  
  285.         test -e "$LOCAL_APP_PROFILE" || {
  286.             tmp=`mktemp`
  287.         cat <<EOM > "$tmp"
  288. # Site-specific additions and overrides for usr.sbin.mysqld.
  289. # For more details, please see /etc/apparmor.d/local/README.
  290. EOM
  291.             mkdir `dirname $LOCAL_APP_PROFILE` 2>/dev/null || true
  292.             mv -f "$tmp" "$LOCAL_APP_PROFILE"
  293.             chmod 644 "$LOCAL_APP_PROFILE"
  294.         }
  295.  
  296.         # Reload the profile, including any abstraction updates
  297.         if aa-status --enabled 2>/dev/null; then
  298.             apparmor_parser -r -T -W "$APP_PROFILE" || true
  299.         fi
  300.     fi
  301. fi
  302. # End automatically added section
  303. # Automatically added by dh_installinit
  304. if [ -x "/etc/init.d/mysql" ]; then
  305.     if [ ! -e "/etc/init/mysql.conf" ]; then
  306.         update-rc.d mysql defaults 19 21 >/dev/null
  307.     fi
  308.     invoke-rc.d mysql start || exit $?
  309. fi
  310. # End automatically added section
  311. # Automatically added by dh_installinit
  312. update-rc.d -f mysql remove >/dev/null || exit $?
  313. # End automatically added section
  314.  
  315.  
  316. exit 0
  317. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement