* izmjena imena iz kernel-2.6-cn u genericki kernel-cn
[kernel-cn.git] / debian / postinst
index bfbb597..29e424e 100755 (executable)
@@ -1,5 +1,5 @@
 #!/bin/sh
-# postinst script for spamassassin-cn
+# postinst script for kernel-cn
 #
 # see: dh_installdeb(1)
 
@@ -27,9 +27,15 @@ case "$1" in
     ;;
 esac
 
+# Source debconf library.
+. /usr/share/debconf/confmodule
+
 # import CN-functions
 . /usr/share/carnet-tools/functions.sh
 
+# import GRUB helper functions
+. /usr/share/kernel-cn/grub-functions.sh
+
 ################################################################################
 
 # starting up backup
@@ -37,20 +43,20 @@ echo -n "CN: Backed up to /var/backups:"
 
 # backup lilo.conf
 if [ -e /etc/lilo.conf ]; then
-  cp_backup_conffile /etc/lilo.conf
-  echo -n " lilo.conf"
+    cp_backup_conffile /etc/lilo.conf
+    echo -n " lilo.conf"
 fi
 
 # backup old kernel params
 if [ -e /etc/sysctl.conf ]; then
-  cp_backup_conffile /etc/sysctl.conf
-  echo -n " sysctl.conf"
+    cp_backup_conffile /etc/sysctl.conf
+    echo -n " sysctl.conf"
 fi
 
 # backup old kernel params
 if [ -e /etc/kernel-img.conf ]; then
-  cp_backup_conffile /etc/kernel-img.conf
-  echo -n " kernel-img.conf"
+    cp_backup_conffile /etc/kernel-img.conf
+    echo -n " kernel-img.conf"
 fi
 
 # finished
@@ -58,6 +64,43 @@ echo "."
 
 ################################################################################
 
+# remove obsolete symlinks and kernels
+rm -f /boot/vmlinuz /boot/vmlinuz.old /boot/vmlinuz.old2 \
+    /boot/vmlinuz.plain /vmlinuz /vmlinuz.old /boot/vmlinuz.plain \
+    /boot/vmlinuz.generic /boot/vmlinuz-generic /boot/vmlinuz-old
+echo "CN: Removed old symlinks in / and /boot."
+
+################################################################################
+
+DIVERT_TO="grub grub-probe"
+
+echo -n "CN: Undiverting binaries if necessary:"
+for i in $DIVERT_TO; do
+    if [ -e /usr/sbin/$i.real ]; then
+            dpkg-divert --remove --rename --package 'kernel-2.6-cn' \
+                --divert /usr/sbin/$i.real /usr/sbin/$i >/dev/null
+        echo -n " $i"
+    fi
+done
+echo "."
+
+################################################################################
+
+# detect existing Grub2 installation
+
+GRUB2="no"
+if [ \( -e /boot/grub/grub.cfg \) -o \( -e /boot/grub/core.img \) -o \( -e /usr/lib/grub/i386-pc/ext2.mod \) ]; then
+    echo "CN: Detected GRUB2 installation, will try to use it."
+    GRUB2="yes"
+    if [ -e /boot/grub/menu.lst ]; then
+        echo "CN: Oops, GRUB1 (Legacy) installation detected. Will try to upgrade to GRUB2."
+    fi
+else
+    echo "CN: No GRUB2 detected, will continue with GRUB1 as default option."
+fi
+
+################################################################################
+
 # intro msg
 echo -n "CN: Configuring system (this will take a while):"
 
@@ -66,20 +109,20 @@ if [ ! -e /etc/kernel-img.conf ]; then
     touch /etc/kernel-img.conf
 fi
 
-# update postinst_hook for grub
+# update postinst_hook for grub/grub2
 if grep -q postinst_hook /etc/kernel-img.conf; then
     cp_check_and_sed '^postinst_hook' \
-      's;^postinst_hook[[:blank:]]*=.*;postinst_hook = /usr/sbin/update-grub;g' \
-          /etc/kernel-img.conf || true
+        's;^postinst_hook[[:blank:]]*=.*;postinst_hook = /usr/sbin/update-grub;g' \
+        /etc/kernel-img.conf || true
 else
     echo "postinst_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
 fi
 
-# update postrm_hook for grub
+# update postrm_hook for grub/grub2
 if grep -q postrm_hook /etc/kernel-img.conf; then
     cp_check_and_sed '^postrm_hook' \
-      's;^postrm_hook[[:blank:]]*=.*;postrm_hook = /usr/sbin/update-grub;g' \
-          /etc/kernel-img.conf || true
+        's;^postrm_hook[[:blank:]]*=.*;postrm_hook = /usr/sbin/update-grub;g' \
+        /etc/kernel-img.conf || true
 else
     echo "postrm_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
 fi
@@ -87,8 +130,8 @@ fi
 # enable initrd
 if grep -q do_initrd /etc/kernel-img.conf; then
     cp_check_and_sed '^do_initrd' \
-      's/^do_initrd[[:blank:]]*=.*/do_initrd = yes/g' \
-          /etc/kernel-img.conf || true
+        's/^do_initrd[[:blank:]]*=.*/do_initrd = yes/g' \
+        /etc/kernel-img.conf || true
 else
     echo "do_initrd = yes" >> /etc/kernel-img.conf
 fi
@@ -96,46 +139,218 @@ fi
 echo -n " kernel-img.conf"
 
 # generate initial grub loaders
-if [ ! -d /boot/grub ]; then
-    mkdir -p /boot/grub
-    if [ -d /usr/lib/grub/i386-pc ]; then
-        cp -a /usr/lib/grub/i386-pc/* /boot/grub
+if [ "x$GRUB2" = "xno" ]; then
+    # Grub1 part
+    if [ ! -d "$grub_dir" ]; then
+        mkdir -p "$grub_dir"
+        # x86
+        if [ -d /usr/lib/grub/i386-pc ]; then
+            cp -a /usr/lib/grub/i386-pc/* "$grub_dir"
+        # x86_64
+        elif [ -d /usr/lib/grub/x86_64-pc ]; then
+            cp -a /usr/lib/grub/x86_64-pc/* "$grub_dir"
+        fi
+    fi
+else
+    # Grub2 part
+    if [ ! -d /boot/grub ]; then
+        mkdir -p /boot/grub
+        if [ -d /usr/lib/grub/i386-pc ]; then
+            cp -a /usr/lib/grub/i386-pc/* /boot/grub
+        fi
     fi
 fi
+echo -n " grub1"
 
 # create/update grub configuration
-if [ -e /boot/grub/menu.lst ]; then
-    # is there uncompatibile grub conf present?
-    if ! grep -q 'AUTOMAGIC KERNELS LIST' /boot/grub/menu.lst; then
-        mv -f /boot/grub/menu.lst /boot/grub/menu.lst.old
+if [ "x$GRUB2" = "xno" ]; then
+    # Grub1 part
+    if [ -e "$menu_file" ]; then
+        # is there uncompatibile grub conf present?
+        if ! grep -q 'AUTOMAGIC KERNELS LIST' "$menu_file"; then
+            mv -f "$menu_file" "$menu_file.old"
+        fi
+    fi
+    if [ ! -e "$menu_file" ]; then
+        yes | update-grub >/dev/null 2>&1 || true
+    else
+        update-grub >/dev/null 2>&1 || true
+    fi
+else
+    # Grub2 part
+    touch /boot/grub/grub.cfg
+    update-grub >/dev/null 2>&1 || true
+fi
+echo -n " grub2"
+
+# workaround grsec
+if uname -a | grep -q grsec; then
+    if [ -x /sbin/chpax ]; then
+        # grub
+        if [ -x /usr/sbin/grub.real ]; then
+            chpax -spmrx /usr/sbin/grub.real >/dev/null 2>&1 || true
+        elif [ -x /usr/sbin/grub ]; then
+            chpax -spmrx /usr/sbin/grub >/dev/null 2>&1 || true
+        fi
+
+        # grub-probe
+        if [ -x /usr/sbin/grub-probe.real ]; then
+            chpax -spmrx /usr/sbin/grub-probe.real >/dev/null 2>&1 || true
+        elif [ -x /usr/sbin/grub-probe ]; then
+            chpax -spmrx /usr/sbin/grub-probe >/dev/null 2>&1 || true
+        fi
+
+        echo -n " grsec"
     fi
 fi
-if [ ! -e /boot/grub/menu.lst ]; then
-    yes | /usr/sbin/update-grub >/dev/null 2>&1 || true
+
+# remove obsolete devfs/compat links and restore normal udev behaviour
+rm -f /etc/udev/rules.d/devfs.rules /etc/udev/rules.d/compat.rules \
+    /etc/udev/rules.d/compat-full.rules
+#if [ ! -e /etc/udev/rules.d/udev.rules ]; then
+#    ln -s ../udev.rules /etc/udev/rules.d/udev.rules
+#fi
+udevadm control --reload_rules >/dev/null 2>&1 || true
+udevadm settle || true
+echo -n " udev"
+
+# update device map if possible
+if [ "x$GRUB2" = "xno" ]; then
+    # Grub1 part
+    if [ -f "$device_map" ]; then
+        mv -f "$device_map" "$device_map.old"
+    fi
+    # possible situation when upgrading from Grub1 to Grub2
+    if [ ! -x /usr/sbin/grub ]; then
+        echo "."
+        echo "CN: FATAL ERROR while trying to execute GRUB1!"
+        echo "CN: Do not reboot your server and report this to OTRS immediately!"
+        exit 1
+    fi
+    grub --batch --no-floppy --device-map="$device_map" <<'EOF' >/dev/null 2>&1
+quit
+EOF
+    if [ ! -s "$device_map" ]; then
+        if [ -f "$device_map.old" ]; then
+            mv -f "$device_map.old" "$device_map"
+        fi
+
+    else
+        rm -f "$device_map.old"
+    fi
 else
-    /usr/sbin/update-grub >/dev/null 2>&1 || true
+    # Grub2 part
+    grub-mkdevicemap --no-floppy
+    grub-install --no-floppy --grub-setup=/bin/true "$(grub-probe -t drive /boot/grub)" >/dev/null 2>&1
 fi
+echo -n " grub3"
+
+# import GRUB helper functions (again, updated device map)
+. /usr/share/kernel-cn/grub-functions.sh
+
+if [ "x$GRUB2" = "xno" ]; then
+    # Grub1 part
+    # get install device (0x80 BIOS device)
+    install_device=$(grep '^(hd0)' "$device_map" | \
+        sed -e 's%[^[:space:]]*[[:space:]]*\([^[:space:]]*\)%\1%')
+    if [ -z "$install_device" ]; then
+        echo "."
+        echo "CN: FATAL ERROR while detecting boot disk!"
+        echo "CN: Do not reboot your server and report this to OTRS immediately!"
+        exit 1
+    fi
 
-# install grub loader
-SHIELD=$(sysctl -e -n kernel.exec-shield)
-if [ ! -z "$SHIELD" ]; then
-    sysctl -w -e kernel.exec-shield=0 >/dev/null 2>&1
+    # oops, install device is a symlink...
+    if [ -h "$install_device" ]; then
+        install_device_resolved=$(resolve_symlink "$install_device")
+        if [ -z "$install_device_resolved" ]; then
+            install_device_resolved="$install_device"
+        fi
+
+        # try to fix device map with symlink resolved device
+        if [ "x$install_device_resolved" != "x$install_device" ]; then
+            cp_check_and_sed '^\(hd0\)' \
+                "s;^(hd0).*;(hd0)      $install_device_resolved;" "$device_map" \
+                || true
+        fi
+    fi
+else
+    # Grub2 part
+    install_device="(hd0)"
 fi
-if ! grub-install --no-floppy '(hd0)' >/dev/null 2>&1; then
+echo -n " grub4"
+
+# install GRUB loader
+# this will work for both Grub1 and Grub2
+if ! grub-install --no-floppy "$install_device" >/dev/null 2>&1; then
     echo "."
-    echo "CN: FATAL ERROR running grub-install!"
+    echo "CN: FATAL ERROR while running grub-install on $install_device!"
     echo "CN: Do not reboot your server and report this to OTRS immediately!"
     exit 1
 fi
-if [ ! -z "$SHIELD" ]; then
-    sysctl -w -e "kernel.exec-shield=$SHIELD" >/dev/null 2>&1
+echo -n " grub5"
+
+# fix possible wrong params in menu.lst
+if [ "x$GRUB2" = "xno" ]; then
+    # Grub1 part
+    cp_check_and_sed '^# groot=' \
+      "s;^# groot=.*;# groot=$grub_root_device;g" \
+          "$menu_file" || true
+    cp_check_and_sed '^# kopt=' \
+      "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
+          "$menu_file" || true
+fi
+update-grub >/dev/null 2>&1 || true
+echo -n " grub6"
+
+# fix possible wrong params in menu.lst
+if [ "x$GRUB2" = "xno" ]; then
+    # Grub1 part
+    # ... and final stage of devfs/udev brokeness fix
+    if [ "x$install_device_resolved" != "x$install_device" ]; then
+        # get original (symlinked device)
+        root_device=$(find_device_nonresolved "/")
+        if [ -z "$root_device" ]; then
+            root_device=$(find_root_device)
+        fi
+
+        # fix (hd0) mapping
+        cp_check_and_sed '^\(hd0\)' \
+            "s;^(hd0).*;(hd0)  $install_device;" "$device_map" \
+            || true
+
+        # fix global root= invocation
+        cp_check_and_sed '^# kopt=' \
+          "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
+              "$menu_file" || true
+
+        # fix root= invocation for individual kernel profiles
+        cp_check_and_sed '^kernel' \
+          "s;\(^kernel.*\)root=[^[:space:]]*\(.*\);\1root=$root_device\2;g" \
+              "$menu_file" || true
+    fi
+fi
+echo -n " grub7"
+
+# install to other boot sectors if needed (better safe than sorry)
+if [ "x$GRUB2" = "xyes" ]; then
+    # Grub2 part
+    db_get grub-pc/install_devices
+    for i in `echo $RET | sed -e 's/,/ /g'`; do
+        real_device="$(readlink -f "$i")"
+        if [ -e "$real_device" ]; then
+            grub-install --force --no-floppy $real_device \
+                >/dev/null 2>&1 || true
+        fi
+    done
 fi
+echo -n " grub8"
 
 # disable lilo
 if [ -e /etc/lilo.conf ]; then
     mv -f /etc/lilo.conf /etc/lilo.conf.old
+    echo -n " lilo"
 fi
-echo -n " lilo"
 
 # mdadm
 if [ -x /usr/share/mdadm/mkconf ]; then
@@ -152,7 +367,7 @@ if [ -x /usr/share/mdadm/mkconf ]; then
     echo -n " mdadm"
 fi
 
-# update initramfs accordingly
+# update initramfs accordingly (because of mdadm and udev)
 update-initramfs -u -k all >/dev/null 2>&1 || true
 echo -n " initramfs"
 
@@ -167,8 +382,8 @@ echo -n "CN: Modifying the neccessary system files:"
 # remove group 99
 if getent group proc >/dev/null 2>&1; then
     groupdel proc >/dev/null 2>&1
+    echo -n " proc"
 fi
-echo -n " proc"
 
 # remove oidentd from oident group
 if getent group oident >/dev/null 2>&1; then
@@ -187,76 +402,25 @@ fi
 
 # default kernel parameters
 rm -f /etc/sysctl.conf.$$
-cat > /etc/sysctl.conf.$$ <<EOF
-kernel.exec-shield=3
-kernel.maps_protect=1
-net.core.rmem_default=1048576
-net.core.wmem_default=1048576
-net.ipv4.conf.all.accept_redirects=0
-net.ipv4.conf.all.accept_source_route=0
-net.ipv4.conf.all.log_martians=1
-net.ipv4.conf.all.rp_filter=1
-net.ipv4.conf.all.secure_redirects=1
-net.ipv4.conf.all.send_redirects=0
-net.ipv4.icmp_echo_ignore_broadcasts=1
-net.ipv4.icmp_ignore_bogus_error_responses=1
-net.ipv4.ip_forward=0
-net.ipv4.ip_local_port_range=10000 65000
-net.ipv4.tcp_congestion_control=cubic
-net.ipv4.tcp_ecn=0
-net.ipv4.tcp_max_syn_backlog=8192
-net.ipv4.tcp_retries1=2
-net.ipv4.tcp_rfc1337=1
-net.ipv4.tcp_syncookies=1
-vm.mmap_min_addr=65536
-EOF
 
-# old kernel params
+# old kernel params (skipping some of the obsolete or overrided entries)
 if [ -e /etc/sysctl.conf ]; then
-  #cat /etc/sysctl.conf >> /etc/sysctl.conf.$$
-  #ignore some of 2.4 stuff
-  egrep -v 'net\.core\.(r|w)mem_max|net\.ipv4\.tcp_(r|w)mem|vm\.bdflush|net\.ipv4\.ip_local_port_range|kernel\.rtsig-max|net\.ipv4\.tcp_syncookies' \
-    /etc/sysctl.conf >> /etc/sysctl.conf.$$
+    egrep -v 'net\.core\.(r|w)mem_max|net\.ipv4\.tcp_(r|w)mem|vm\.bdflush|net\.ipv4\.ip_local_port_range|kernel\.rtsig-max|net\.ipv4\.tcp_syncookies|kernel\.exec-shield|net\.ipv4\.tcp_max_syn_backlog|net\.ipv4\.tcp_congestion_control|kernel\.exec-shield|kernel\.maps_protect' \
+        /etc/sysctl.conf >> /etc/sysctl.conf.$$
 fi
 
-# add sysctl.conf Debian headers
-rm -f /etc/sysctl.conf-head
-cat > /etc/sysctl.conf-head <<EOF
-#
-# /etc/sysctl.conf - Configuration file for setting system variables
-# See sysctl.conf (5) for information.
-#
-EOF
-
-# merge old and new in one conf, primarily respecting old
-script='
-my %confhash = ();
-my ($key, $value);
-while (<>)
-{
-  chomp();
-  if (/^\s*(\S+)\s*=\s*(.+)\s*$/o)
-  {
-    my ($key, $value) = ($1, $2);
-    $key =~ s/\//./go;
-    $confhash{$key} = $value;
-  }
-}
-for (sort { $a cmp $b } keys %confhash)
-{
-  print $_, "=", $confhash{$_}, "\n";
-}
-'
-rm -f /etc/sysctl.conf-new
-perl -e "$script" < /etc/sysctl.conf.$$ > /etc/sysctl.conf-new
-cat /etc/sysctl.conf-head /etc/sysctl.conf-new > /etc/sysctl.conf.$$
-rm -f /etc/sysctl.conf-head /etc/sysctl.conf-new
-
 # finished with merging, move into sysctl.conf
 cp_mv /etc/sysctl.conf.$$ /etc/sysctl.conf
 echo -n " sysctl.conf"
 
-# finished with basic kernel-2.6-cn stuff
+# reload sysctl ASAP
+if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
+  invoke-rc.d procps restart >/dev/null 2>&1 || true
+else
+  /etc/init.d/procps restart >/dev/null 2>&1 || true
+fi
+
+# finished with basic kernel-cn stuff
 echo "."
 
 ################################################################################
@@ -266,31 +430,33 @@ echo -n "CN: Setting up PAM configurations:"
 
 # update pam_limits accordingly
 if [ -e /etc/security/limits.conf ]; then
-  rm -f /etc/security/limits.conf.$$
-  cp /etc/security/limits.conf /etc/security/limits.conf.$$
-  cp-update kernel-2.6-cn /etc/security/limits.conf.$$ <<EOF
-*               soft    core            0
-*              hard    nofile          4096
-*              soft    nofile          4096
+    rm -f /etc/security/limits.conf.$$
+    cp /etc/security/limits.conf /etc/security/limits.conf.$$
+    cp-update kernel-cn /etc/security/limits.conf.$$ <<'EOF'
+*       soft    core        0
+*              hard    nofile          16384
+*              soft    nofile          16384
+@users  soft    nproc       100
+@users  hard    nproc       150
 EOF
-  cp_mv /etc/security/limits.conf.$$ /etc/security/limits.conf
-  echo -n " limits"
+    cp_mv /etc/security/limits.conf.$$ /etc/security/limits.conf
+    echo -n " limits"
 fi
 
 # check pam.d/login
 if [ -e /etc/pam.d/login ]; then
-  cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
-    's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
-    /etc/pam.d/login || true
-  echo -n " login"
+    cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
+        's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
+        /etc/pam.d/login || true
+    echo -n " login"
 fi
 
 # check pam.d/ssh
 if [ -e /etc/pam.d/ssh ]; then
-  cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
-    's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
-    /etc/pam.d/ssh || true
-  echo -n " ssh"
+    cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
+        's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
+        /etc/pam.d/ssh || true
+    echo -n " ssh"
 fi
 
 # finished with PAM
@@ -298,79 +464,6 @@ echo "."
 
 ################################################################################
 
-# remove obsolete links
-rm -f /boot/vmlinuz /boot/vmlinuz.old /boot/vmlinuz.old2 \
-  /boot/vmlinuz.plain /vmlinuz /vmlinuz.old /boot/vmlinuz.plain
-echo "CN: Removed old symlinks in / and /boot."
-
-################################################################################
-
-# fix old kernel-2.4-cn postrm
-if [ -e /var/lib/dpkg/info/kernel-2.4-cn.postrm ]; then
-    echo "CN: Fixed old kernel-2.4-cn postrm."
-    cat > /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ <<EOF
-#!/bin/sh
-
-set -e
-
-# be sure, be safe
-if [ "$1" != "remove" ]; then
-  exit 0
-fi
-
-# import CN-functions
-. /usr/share/carnet-tools/functions.sh
-
-# remove us from limits.conf
-cp-update -r kernel-2.4-cn /etc/security/limits.conf
-
-# remove us from modules
-cp-update -r kernel-2.4-cn /etc/modules
-EOF
-    if ! cmp -s /var/lib/dpkg/info/kernel-2.4-cn.postrm \
-        /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$; then
-        mv /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ \
-            /var/lib/dpkg/info/kernel-2.4-cn.postrm
-        chmod +x /var/lib/dpkg/info/kernel-2.4-cn.postrm
-    fi
-    rm -f /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$
-fi
-
-################################################################################
-
-# fix old kernel-cn postrm
-if [ -e /var/lib/dpkg/info/kernel-cn.postrm ]; then
-    echo "CN: Fixed old kernel-cn postrm."
-    cat > /var/lib/dpkg/info/kernel-cn.postrm.$$ <<EOF
-#!/bin/sh
-
-set -e
-
-# be sure, be safe
-if [ "$1" != "remove" ]; then
-  exit 0
-fi
-
-# import CN-functions
-. /usr/share/carnet-tools/functions.sh
-
-# remove us from limits.conf
-cp-update -r kernel-cn /etc/security/limits.conf
-
-# remove us from modules
-cp-update -r kernel-cn /etc/modules
-EOF
-    if ! cmp -s /var/lib/dpkg/info/kernel-cn.postrm \
-        /var/lib/dpkg/info/kernel-cn.postrm.$$; then
-        mv /var/lib/dpkg/info/kernel-cn.postrm.$$ \
-            /var/lib/dpkg/info/kernel-cn.postrm
-        chmod +x /var/lib/dpkg/info/kernel-cn.postrm
-    fi
-    rm -f /var/lib/dpkg/info/kernel-cn.postrm.$$
-fi
-
-################################################################################
-
 # dh_installdeb will replace this with shell code automatically
 # generated by other debhelper scripts.