7e217c4a45a9fdc2a3964380f69afa74081a907d
[kernel-cn.git] / debian / postinst
1 #!/bin/sh
2 # postinst script for kernel-2.6-cn
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
8 # summary of how this script can be called:
9 #        * <postinst> `configure' <most-recently-configured-version>
10 #        * <old-postinst> `abort-upgrade' <new version>
11 #        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
12 #          <new-version>
13 #        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
14 #          <failed-install-package> <version> `removing'
15 #          <conflicting-package> <version>
16 # for details, see http://www.debian.org/doc/debian-policy/ or
17 # the debian-policy package
18 #
19
20 case "$1" in
21     configure|reconfigure)
22       # continue below
23     ;;
24
25     *)
26         exit 0
27     ;;
28 esac
29
30 # Source debconf library.
31 . /usr/share/debconf/confmodule
32
33 # import CN-functions
34 . /usr/share/carnet-tools/functions.sh
35
36 # import GRUB helper functions
37 . /usr/share/kernel-2.6-cn/grub-functions.sh
38
39 ################################################################################
40
41 # starting up backup
42 echo -n "CN: Backed up to /var/backups:"
43
44 # backup lilo.conf
45 if [ -e /etc/lilo.conf ]; then
46     cp_backup_conffile /etc/lilo.conf
47     echo -n " lilo.conf"
48 fi
49
50 # backup old kernel params
51 if [ -e /etc/sysctl.conf ]; then
52     cp_backup_conffile /etc/sysctl.conf
53     echo -n " sysctl.conf"
54 fi
55
56 # backup old kernel params
57 if [ -e /etc/kernel-img.conf ]; then
58     cp_backup_conffile /etc/kernel-img.conf
59     echo -n " kernel-img.conf"
60 fi
61
62 # finished
63 echo "."
64
65 ################################################################################
66
67 # remove obsolete symlinks and kernels
68 rm -f /boot/vmlinuz /boot/vmlinuz.old /boot/vmlinuz.old2 \
69     /boot/vmlinuz.plain /vmlinuz /vmlinuz.old /boot/vmlinuz.plain \
70     /boot/vmlinuz.generic /boot/vmlinuz-generic /boot/vmlinuz-old
71 echo "CN: Removed old symlinks in / and /boot."
72
73 ################################################################################
74
75 DIVERT_TO="grub grub-probe"
76
77 echo -n "CN: Undiverting binaries if necessary:"
78 for i in $DIVERT_TO; do
79     if [ -e /usr/sbin/$i.real ]; then
80             dpkg-divert --remove --rename --package 'kernel-2.6-cn' \
81                 --divert /usr/sbin/$i.real /usr/sbin/$i >/dev/null
82         echo -n " $i"
83     fi
84 done
85 echo "."
86
87 ################################################################################
88
89 # detect existing Grub2 installation
90
91 GRUB2="no"
92 if [ \( -e /boot/grub/grub.cfg \) -o \( -e /boot/grub/core.img \) ]; then
93     echo "CN: Detected GRUB2 installation, will try to use it."
94     GRUB2="yes"
95     if [ -e /boot/grub/menu.lst ]; then
96         echo "CN: Oops, GRUB1 (Legacy) installation detected. Will try to upgrade to GRUB2."
97     fi
98 else
99     echo "CN: No GRUB2 detected, will continue with GRUB1 as default option."
100 fi
101
102 ################################################################################
103
104 # intro msg
105 echo -n "CN: Configuring system (this will take a while):"
106
107 # generate kernel-img.conf
108 if [ ! -e /etc/kernel-img.conf ]; then
109     touch /etc/kernel-img.conf
110 fi
111
112 # update postinst_hook for grub/grub2
113 if grep -q postinst_hook /etc/kernel-img.conf; then
114     cp_check_and_sed '^postinst_hook' \
115         's;^postinst_hook[[:blank:]]*=.*;postinst_hook = /usr/sbin/update-grub;g' \
116         /etc/kernel-img.conf || true
117 else
118     echo "postinst_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
119 fi
120
121 # update postrm_hook for grub/grub2
122 if grep -q postrm_hook /etc/kernel-img.conf; then
123     cp_check_and_sed '^postrm_hook' \
124         's;^postrm_hook[[:blank:]]*=.*;postrm_hook = /usr/sbin/update-grub;g' \
125         /etc/kernel-img.conf || true
126 else
127     echo "postrm_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
128 fi
129
130 # enable initrd
131 if grep -q do_initrd /etc/kernel-img.conf; then
132     cp_check_and_sed '^do_initrd' \
133         's/^do_initrd[[:blank:]]*=.*/do_initrd = yes/g' \
134         /etc/kernel-img.conf || true
135 else
136     echo "do_initrd = yes" >> /etc/kernel-img.conf
137 fi
138
139 echo -n " kernel-img.conf"
140
141 # generate initial grub loaders
142 if [ "x$GRUB2" = "xno" ]; then
143     # Grub1 part
144     if [ ! -d "$grub_dir" ]; then
145         mkdir -p "$grub_dir"
146         # x86
147         if [ -d /usr/lib/grub/i386-pc ]; then
148             cp -a /usr/lib/grub/i386-pc/* "$grub_dir"
149         # x86_64
150         elif [ -d /usr/lib/grub/x86_64-pc ]; then
151             cp -a /usr/lib/grub/x86_64-pc/* "$grub_dir"
152         fi
153     fi
154 else
155     # Grub2 part
156     if [ ! -d /boot/grub ]; then
157         mkdir -p /boot/grub
158         if [ -d /usr/lib/grub/i386-pc ]; then
159             cp -a /usr/lib/grub/i386-pc/* /boot/grub
160         fi
161     fi
162 fi
163 echo -n " grub1"
164
165 # create/update grub configuration
166 if [ "x$GRUB2" = "xno" ]; then
167     # Grub1 part
168     if [ -e "$menu_file" ]; then
169         # is there uncompatibile grub conf present?
170         if ! grep -q 'AUTOMAGIC KERNELS LIST' "$menu_file"; then
171             mv -f "$menu_file" "$menu_file.old"
172         fi
173     fi
174     if [ ! -e "$menu_file" ]; then
175         yes | update-grub >/dev/null 2>&1 || true
176     else
177         update-grub >/dev/null 2>&1 || true
178     fi
179 else
180     # Grub2 part
181     touch /boot/grub/grub.cfg
182     update-grub >/dev/null 2>&1 || true
183 fi
184 echo -n " grub2"
185
186 # workaround grsec
187 if uname -a | grep -q grsec; then
188     if [ -x /sbin/chpax ]; then
189         # grub
190         if [ -x /usr/sbin/grub.real ]; then
191             chpax -spmrx /usr/sbin/grub.real >/dev/null 2>&1 || true
192         elif [ -x /usr/sbin/grub ]; then
193             chpax -spmrx /usr/sbin/grub >/dev/null 2>&1 || true
194         fi
195
196         # grub-probe
197         if [ -x /usr/sbin/grub-probe.real ]; then
198             chpax -spmrx /usr/sbin/grub-probe.real >/dev/null 2>&1 || true
199         elif [ -x /usr/sbin/grub-probe ]; then
200             chpax -spmrx /usr/sbin/grub-probe >/dev/null 2>&1 || true
201         fi
202
203         echo -n " grsec"
204     fi
205 fi
206
207 # remove obsolete devfs/compat links and restore normal udev behaviour
208 rm -f /etc/udev/rules.d/devfs.rules /etc/udev/rules.d/compat.rules \
209     /etc/udev/rules.d/compat-full.rules
210 #if [ ! -e /etc/udev/rules.d/udev.rules ]; then
211 #    ln -s ../udev.rules /etc/udev/rules.d/udev.rules
212 #fi
213 udevadm control --reload_rules >/dev/null 2>&1 || true
214 udevadm settle || true
215 echo -n " udev"
216
217 # update device map if possible
218 if [ "x$GRUB2" = "xno" ]; then
219     # Grub1 part
220     if [ -f "$device_map" ]; then
221         mv -f "$device_map" "$device_map.old"
222     fi
223     grub --batch --no-floppy --device-map="$device_map" <<'EOF' >/dev/null 2>&1
224 quit
225 EOF
226     if [ ! -s "$device_map" ]; then
227         if [ -f "$device_map.old" ]; then
228             mv -f "$device_map.old" "$device_map"
229         fi
230
231     else
232         rm -f "$device_map.old"
233     fi
234 else
235     # Grub2 part
236     grub-mkdevicemap --no-floppy
237     grub-install --no-floppy --grub-setup=/bin/true "$(grub-probe -t drive /boot/grub)" >/dev/null 2>&1
238 fi
239 echo -n " grub3"
240
241 # import GRUB helper functions (again, updated device map)
242 . /usr/share/kernel-2.6-cn/grub-functions.sh
243
244 if [ "x$GRUB2" = "xno" ]; then
245     # Grub1 part
246     # get install device (0x80 BIOS device)
247     install_device=$(grep '^(hd0)' "$device_map" | \
248         sed -e 's%[^[:space:]]*[[:space:]]*\([^[:space:]]*\)%\1%')
249     if [ -z "$install_device" ]; then
250         echo "."
251         echo "CN: FATAL ERROR while detecting boot disk!"
252         echo "CN: Do not reboot your server and report this to OTRS immediately!"
253         exit 1
254     fi
255
256     # oops, install device is a symlink...
257     if [ -h "$install_device" ]; then
258         install_device_resolved=$(resolve_symlink "$install_device")
259         if [ -z "$install_device_resolved" ]; then
260             install_device_resolved="$install_device"
261         fi
262
263         # try to fix device map with symlink resolved device
264         if [ "x$install_device_resolved" != "x$install_device" ]; then
265             cp_check_and_sed '^\(hd0\)' \
266                 "s;^(hd0).*;(hd0)       $install_device_resolved;" "$device_map" \
267                 || true
268         fi
269     fi
270 else
271     # Grub2 part
272     install_device="(hd0)"
273 fi
274 echo -n " grub4"
275
276 # install GRUB loader
277 # this will work for both Grub1 and Grub2
278 if ! grub-install --no-floppy "$install_device" >/dev/null 2>&1; then
279     echo "."
280     echo "CN: FATAL ERROR while running grub-install on $install_device!"
281     echo "CN: Do not reboot your server and report this to OTRS immediately!"
282     exit 1
283 fi
284 echo -n " grub5"
285
286 # fix possible wrong params in menu.lst
287 if [ "x$GRUB2" = "xno" ]; then
288     # Grub1 part
289     cp_check_and_sed '^# groot=' \
290       "s;^# groot=.*;# groot=$grub_root_device;g" \
291           "$menu_file" || true
292     cp_check_and_sed '^# kopt=' \
293       "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
294           "$menu_file" || true
295 fi
296 update-grub >/dev/null 2>&1 || true
297 echo -n " grub6"
298
299 # fix possible wrong params in menu.lst
300 if [ "x$GRUB2" = "xno" ]; then
301     # Grub1 part
302     # ... and final stage of devfs/udev brokeness fix
303     if [ "x$install_device_resolved" != "x$install_device" ]; then
304         # get original (symlinked device)
305         root_device=$(find_device_nonresolved "/")
306         if [ -z "$root_device" ]; then
307             root_device=$(find_root_device)
308         fi
309
310         # fix (hd0) mapping
311         cp_check_and_sed '^\(hd0\)' \
312             "s;^(hd0).*;(hd0)   $install_device;" "$device_map" \
313             || true
314
315         # fix global root= invocation
316         cp_check_and_sed '^# kopt=' \
317           "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
318               "$menu_file" || true
319
320         # fix root= invocation for individual kernel profiles
321         cp_check_and_sed '^kernel' \
322           "s;\(^kernel.*\)root=[^[:space:]]*\(.*\);\1root=$root_device\2;g" \
323               "$menu_file" || true
324     fi
325 fi
326 echo -n " grub7"
327
328 # install to other boot sectors if needed (better safe than sorry)
329 if [ "x$GRUB2" = "xyes" ]; then
330     # Grub2 part
331     db_get grub-pc/install_devices
332     for i in `echo $RET | sed -e 's/,/ /g'`; do
333         real_device="$(readlink -f "$i")"
334         if [ -e "$real_device" ]; then
335             grub-install --force --no-floppy $real_device \
336                 >/dev/null 2>&1 || true
337         fi
338     done
339 fi
340 echo -n " grub8"
341
342 # disable lilo
343 if [ -e /etc/lilo.conf ]; then
344     mv -f /etc/lilo.conf /etc/lilo.conf.old
345     echo -n " lilo"
346 fi
347
348 # mdadm
349 if [ -x /usr/share/mdadm/mkconf ]; then
350     if [ ! -e /etc/mdadm/mdadm.conf ]; then
351         touch /etc/mdadm/mdadm.conf
352     fi
353
354     /usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf.$$
355     if ! cmp -s /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf; then
356         mv /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf
357     fi
358     rm -f /etc/mdadm/mdadm.conf.$$ /etc/initramfs-tools/hooks/md \
359         /var/lib/mdadm/CONF-UNCHECKED
360     echo -n " mdadm"
361 fi
362
363 # update initramfs accordingly (because of mdadm and udev)
364 update-initramfs -u -k all >/dev/null 2>&1 || true
365 echo -n " initramfs"
366
367 # finished
368 echo "."
369
370 ################################################################################
371
372 # rest of configuration...
373 echo -n "CN: Modifying the neccessary system files:"
374
375 # remove group 99
376 if getent group proc >/dev/null 2>&1; then
377     groupdel proc >/dev/null 2>&1
378     echo -n " proc"
379 fi
380
381 # remove oidentd from oident group
382 if getent group oident >/dev/null 2>&1; then
383     # sarge default
384     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup' \
385       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup/OIDENT_GROUP=oident/g' \
386           /etc/default/oidentd || true
387
388     # old kernel-2.6-cn default
389     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc' \
390       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc/OIDENT_GROUP=oident/g' \
391           /etc/default/oidentd || true
392
393     echo -n " oidentd"
394 fi
395
396 # default kernel parameters
397 rm -f /etc/sysctl.conf.$$
398
399 # old kernel params (skipping some of the obsolete or overrided entries)
400 if [ -e /etc/sysctl.conf ]; then
401     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' \
402         /etc/sysctl.conf >> /etc/sysctl.conf.$$
403 fi
404
405 # finished with merging, move into sysctl.conf
406 cp_mv /etc/sysctl.conf.$$ /etc/sysctl.conf
407 echo -n " sysctl.conf"
408
409 # reload sysctl ASAP
410 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
411   invoke-rc.d procps restart >/dev/null 2>&1 || true
412 else
413   /etc/init.d/procps restart >/dev/null 2>&1 || true
414 fi
415
416 # finished with basic kernel-2.6-cn stuff
417 echo "."
418
419 ################################################################################
420
421 # intro
422 echo -n "CN: Setting up PAM configurations:"
423
424 # update pam_limits accordingly
425 if [ -e /etc/security/limits.conf ]; then
426     rm -f /etc/security/limits.conf.$$
427     cp /etc/security/limits.conf /etc/security/limits.conf.$$
428     cp-update kernel-2.6-cn /etc/security/limits.conf.$$ <<'EOF'
429 *       soft    core        0
430 *               hard    nofile          16384
431 *               soft    nofile          16384
432 @users  soft    nproc       100
433 @users  hard    nproc       150
434 EOF
435     cp_mv /etc/security/limits.conf.$$ /etc/security/limits.conf
436     echo -n " limits"
437 fi
438
439 # check pam.d/login
440 if [ -e /etc/pam.d/login ]; then
441     cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
442         's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
443         /etc/pam.d/login || true
444     echo -n " login"
445 fi
446
447 # check pam.d/ssh
448 if [ -e /etc/pam.d/ssh ]; then
449     cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
450         's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
451         /etc/pam.d/ssh || true
452     echo -n " ssh"
453 fi
454
455 # finished with PAM
456 echo "."
457
458 ################################################################################
459
460 # fix old kernel-2.4-cn postrm
461 if [ -e /var/lib/dpkg/info/kernel-2.4-cn.postrm ]; then
462     echo "CN: Fixed old kernel-2.4-cn postrm."
463     cat > /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ <<'EOF'
464 #!/bin/sh
465
466 set -e
467
468 # be sure, be safe
469 if [ "$1" != "remove" ]; then
470   exit 0
471 fi
472
473 # import CN-functions
474 . /usr/share/carnet-tools/functions.sh
475
476 # remove us from limits.conf
477 cp-update -r kernel-2.4-cn /etc/security/limits.conf
478
479 # remove us from modules
480 cp-update -r kernel-2.4-cn /etc/modules
481 EOF
482     if ! cmp -s /var/lib/dpkg/info/kernel-2.4-cn.postrm \
483         /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$; then
484         mv /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ \
485             /var/lib/dpkg/info/kernel-2.4-cn.postrm
486         chmod +x /var/lib/dpkg/info/kernel-2.4-cn.postrm
487     fi
488     rm -f /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$
489 fi
490
491 ################################################################################
492
493 # fix old kernel-cn postrm
494 if [ -e /var/lib/dpkg/info/kernel-cn.postrm ]; then
495     echo "CN: Fixed old kernel-cn postrm."
496     cat > /var/lib/dpkg/info/kernel-cn.postrm.$$ <<'EOF'
497 #!/bin/sh
498
499 set -e
500
501 # be sure, be safe
502 if [ "$1" != "remove" ]; then
503   exit 0
504 fi
505
506 # import CN-functions
507 . /usr/share/carnet-tools/functions.sh
508
509 # remove us from limits.conf
510 cp-update -r kernel-cn /etc/security/limits.conf
511
512 # remove us from modules
513 cp-update -r kernel-cn /etc/modules
514 EOF
515     if ! cmp -s /var/lib/dpkg/info/kernel-cn.postrm \
516         /var/lib/dpkg/info/kernel-cn.postrm.$$; then
517         mv /var/lib/dpkg/info/kernel-cn.postrm.$$ \
518             /var/lib/dpkg/info/kernel-cn.postrm
519         chmod +x /var/lib/dpkg/info/kernel-cn.postrm
520     fi
521     rm -f /var/lib/dpkg/info/kernel-cn.postrm.$$
522 fi
523
524 ################################################################################
525
526 # dh_installdeb will replace this with shell code automatically
527 # generated by other debhelper scripts.
528
529 #DEBHELPER#
530
531 exit 0