- fix :q! koji se provukao...
[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 # import CN-functions
31 . /usr/share/carnet-tools/functions.sh
32
33 # import GRUB helper functions
34 . /usr/share/kernel-2.6-cn/grub-functions.sh
35
36 ################################################################################
37
38 # starting up backup
39 echo -n "CN: Backed up to /var/backups:"
40
41 # backup lilo.conf
42 if [ -e /etc/lilo.conf ]; then
43     cp_backup_conffile /etc/lilo.conf
44     echo -n " lilo.conf"
45 fi
46
47 # backup old kernel params
48 if [ -e /etc/sysctl.conf ]; then
49     cp_backup_conffile /etc/sysctl.conf
50     echo -n " sysctl.conf"
51 fi
52
53 # backup old kernel params
54 if [ -e /etc/kernel-img.conf ]; then
55     cp_backup_conffile /etc/kernel-img.conf
56     echo -n " kernel-img.conf"
57 fi
58
59 # finished
60 echo "."
61
62 ################################################################################
63
64 # remove obsolete symlinks and kernels
65 rm -f /boot/vmlinuz /boot/vmlinuz.old /boot/vmlinuz.old2 \
66     /boot/vmlinuz.plain /vmlinuz /vmlinuz.old /boot/vmlinuz.plain \
67     /boot/vmlinuz.generic /boot/vmlinuz-generic /boot/vmlinuz-old
68 echo "CN: Removed old symlinks in / and /boot."
69
70 ################################################################################
71
72 # intro msg
73 echo -n "CN: Configuring system (this will take a while):"
74
75 # generate kernel-img.conf
76 if [ ! -e /etc/kernel-img.conf ]; then
77     touch /etc/kernel-img.conf
78 fi
79
80 # update postinst_hook for grub
81 if grep -q postinst_hook /etc/kernel-img.conf; then
82     cp_check_and_sed '^postinst_hook' \
83         's;^postinst_hook[[:blank:]]*=.*;postinst_hook = /usr/sbin/update-grub;g' \
84         /etc/kernel-img.conf || true
85 else
86     echo "postinst_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
87 fi
88
89 # update postrm_hook for grub
90 if grep -q postrm_hook /etc/kernel-img.conf; then
91     cp_check_and_sed '^postrm_hook' \
92         's;^postrm_hook[[:blank:]]*=.*;postrm_hook = /usr/sbin/update-grub;g' \
93         /etc/kernel-img.conf || true
94 else
95     echo "postrm_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
96 fi
97
98 # enable initrd
99 if grep -q do_initrd /etc/kernel-img.conf; then
100     cp_check_and_sed '^do_initrd' \
101         's/^do_initrd[[:blank:]]*=.*/do_initrd = yes/g' \
102         /etc/kernel-img.conf || true
103 else
104     echo "do_initrd = yes" >> /etc/kernel-img.conf
105 fi
106
107 echo -n " kernel-img.conf"
108
109 # generate initial grub loaders
110 if [ ! -d "$grub_dir" ]; then
111     mkdir -p "$grub_dir"
112     # x86
113     if [ -d /usr/lib/grub/i386-pc ]; then
114         cp -a /usr/lib/grub/i386-pc/* "$grub_dir"
115     # x86_64
116     elif [ -d /usr/lib/grub/x86_64-pc ]; then
117         cp -a /usr/lib/grub/x86_64-pc/* "$grub_dir"
118     fi
119 fi
120 echo -n " grub1"
121
122 # create/update grub configuration
123 if [ -e "$menu_file" ]; then
124     # is there uncompatibile grub conf present?
125     if ! grep -q 'AUTOMAGIC KERNELS LIST' "$menu_file"; then
126         mv -f "$menu_file" "$menu_file.old"
127     fi
128 fi
129 if [ ! -e "$menu_file" ]; then
130     yes | /usr/sbin/update-grub >/dev/null 2>&1 || true
131 else
132     /usr/sbin/update-grub >/dev/null 2>&1 || true
133 fi
134 echo -n " grub2"
135
136 # workaround grsec
137 if [ -x /sbin/chpax ]; then
138     if [ -x /usr/sbin/grub ]; then
139         chpax -spmrx /usr/sbin/grub || true
140     fi
141     if [ -x /usr/sbin/grub-probe ]; then
142         chpax -spmrx /usr/sbin/grub-probe || true
143     fi
144 fi
145 echo -n " grub3"
146
147 # workaround execshield
148 SHIELD=$(sysctl -e -n kernel.exec-shield)
149 if [ ! -z "$SHIELD" ]; then
150     sysctl -e -w kernel.exec-shield=0 >/dev/null 2>&1
151     echo -n " execshield1"
152 fi
153
154 # remove obsolete devfs/compat links and restore normal udev behaviour
155 rm -f /etc/udev/rules.d/devfs.rules /etc/udev/rules.d/compat.rules \
156     /etc/udev/rules.d/compat-full.rules
157 #if [ ! -e /etc/udev/rules.d/udev.rules ]; then
158 #    ln -s ../udev.rules /etc/udev/rules.d/udev.rules
159 #fi
160 udevadm control --reload_rules >/dev/null 2>&1 || true
161 udevsettle || true
162 echo -n " udev"
163
164 # update device map if possible
165 if [ -f "$device_map" ]; then
166     mv -f "$device_map" "$device_map.old"
167 fi
168 grub --batch --no-floppy --device-map="$device_map" <<EOF >/dev/null 2>&1
169 quit
170 EOF
171 if [ ! -s "$device_map" ]; then
172     if [ -f "$device_map.old" ]; then
173         mv -f "$device_map.old" "$device_map"
174     fi
175
176 else
177     rm -f "$device_map.old"
178 fi
179 echo -n " grub4"
180
181 # import GRUB helper functions (again, updated device map)
182 . /usr/share/kernel-2.6-cn/grub-functions.sh
183
184 # get install device (0x80 BIOS device)
185 install_device=$(grep '^(hd0)' "$device_map" | \
186     sed -e 's%[^[:space:]]*[[:space:]]*\([^[:space:]]*\)%\1%')
187 if [ -z "$install_device" ]; then
188     echo "."
189     echo "CN: FATAL ERROR while detecting boot disk!"
190     echo "CN: Do not reboot your server and report this to OTRS immediately!"
191     exit 1
192 fi
193
194 # oops, install device is a symlink...
195 if [ -h "$install_device" ]; then
196     install_device_resolved=$(resolve_symlink "$install_device")
197     if [ -z "$install_device_resolved" ]; then
198         install_device_resolved="$install_device"
199     fi
200
201     # try to fix device map with symlink resolved device
202     if [ "x$install_device_resolved" != "x$install_device" ]; then
203         cp_check_and_sed '^\(hd0\)' \
204             "s;^(hd0).*;(hd0)   $install_device_resolved;" "$device_map" \
205             || true
206     fi
207 fi
208 echo -n " grub5"
209
210 # install GRUB loader
211 if ! grub-install --no-floppy "$install_device" >/dev/null 2>&1; then
212     echo "."
213     echo "CN: FATAL ERROR while running grub-install!"
214     echo "CN: Do not reboot your server and report this to OTRS immediately!"
215     exit 1
216 fi
217 echo -n " grub6"
218
219 # fix possible wrong params in menu.lst
220 cp_check_and_sed '^# groot=' \
221   "s;^# groot=.*;# groot=$grub_root_device;g" \
222       "$menu_file" || true
223 cp_check_and_sed '^# kopt=' \
224   "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
225       "$menu_file" || true
226 /usr/sbin/update-grub >/dev/null 2>&1 || true
227 echo -n " grub7"
228
229 # and final stage of devfs/udev brokeness fix
230 if [ "x$install_device_resolved" != "x$install_device" ]; then
231     # get original (symlinked device)
232     root_device=$(find_device_nonresolved "/")
233     if [ -z "$root_device" ]; then
234         root_device=$(find_root_device)
235     fi
236
237     # fix (hd0) mapping
238     cp_check_and_sed '^\(hd0\)' \
239         "s;^(hd0).*;(hd0)       $install_device;" "$device_map" \
240         || true
241
242     # fix global root= invocation
243     cp_check_and_sed '^# kopt=' \
244       "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
245           "$menu_file" || true
246
247     # fix root= invocation for individual kernel profiles
248     cp_check_and_sed '^kernel' \
249       "s;\(^kernel.*\)root=[^[:space:]]*\(.*\);\1root=$root_device\2;g" \
250           "$menu_file" || true
251 fi
252 echo -n " grub8"
253
254 # restore execshield state
255 if [ ! -z "$SHIELD" ]; then
256     sysctl -e -w "kernel.exec-shield=$SHIELD" >/dev/null 2>&1
257     echo -n " execshield2"
258 fi
259
260 # disable lilo
261 if [ -e /etc/lilo.conf ]; then
262     mv -f /etc/lilo.conf /etc/lilo.conf.old
263     echo -n " lilo"
264 fi
265
266 # mdadm
267 if [ -x /usr/share/mdadm/mkconf ]; then
268     if [ ! -e /etc/mdadm/mdadm.conf ]; then
269         touch /etc/mdadm/mdadm.conf
270     fi
271
272     /usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf.$$
273     if ! cmp -s /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf; then
274         mv /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf
275     fi
276     rm -f /etc/mdadm/mdadm.conf.$$ /etc/initramfs-tools/hooks/md \
277         /var/lib/mdadm/CONF-UNCHECKED
278     echo -n " mdadm"
279 fi
280
281 # update initramfs accordingly (because of mdadm and udev)
282 update-initramfs -u -k all >/dev/null 2>&1 || true
283 echo -n " initramfs"
284
285 # finished
286 echo "."
287
288 ################################################################################
289
290 # rest of configuration...
291 echo -n "CN: Modifying the neccessary system files:"
292
293 # remove group 99
294 if getent group proc >/dev/null 2>&1; then
295     groupdel proc >/dev/null 2>&1
296     echo -n " proc"
297 fi
298
299 # remove oidentd from oident group
300 if getent group oident >/dev/null 2>&1; then
301     # sarge default
302     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup' \
303       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup/OIDENT_GROUP=oident/g' \
304           /etc/default/oidentd || true
305
306     # old kernel-2.6-cn default
307     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc' \
308       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc/OIDENT_GROUP=oident/g' \
309           /etc/default/oidentd || true
310
311     echo -n " oidentd"
312 fi
313
314 # default kernel parameters
315 rm -f /etc/sysctl.conf.$$
316 cat > /etc/sysctl.conf.$$ <<EOF
317 kernel.exec-shield=3
318 kernel.maps_protect=1
319 net.core.rmem_default=1048576
320 net.core.wmem_default=1048576
321 net.ipv4.conf.all.accept_redirects=0
322 net.ipv4.conf.all.accept_source_route=0
323 net.ipv4.conf.all.log_martians=1
324 net.ipv4.conf.all.rp_filter=1
325 net.ipv4.conf.all.secure_redirects=1
326 net.ipv4.conf.all.send_redirects=0
327 net.ipv4.icmp_echo_ignore_broadcasts=1
328 net.ipv4.icmp_ignore_bogus_error_responses=1
329 net.ipv4.ip_forward=0
330 net.ipv4.ip_local_port_range=10000 65000
331 net.ipv4.tcp_congestion_control=cubic
332 net.ipv4.tcp_ecn=0
333 net.ipv4.tcp_max_syn_backlog=8192
334 net.ipv4.tcp_retries1=2
335 net.ipv4.tcp_rfc1337=1
336 net.ipv4.tcp_syncookies=1
337 vm.mmap_min_addr=65536
338 EOF
339
340 # old kernel params
341 if [ -e /etc/sysctl.conf ]; then
342     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' \
343         /etc/sysctl.conf >> /etc/sysctl.conf.$$
344 fi
345
346 # add sysctl.conf Debian headers
347 rm -f /etc/sysctl.conf-head
348 cat > /etc/sysctl.conf-head <<EOF
349 #
350 # /etc/sysctl.conf - Configuration file for setting system variables
351 # See sysctl.conf (5) for information.
352 #
353 EOF
354
355 # merge old and new in one conf, primarily respecting old
356 script='
357 my %confhash = ();
358 my ($key, $value);
359 while (<>)
360 {
361   chomp();
362   if (/^\s*(\S+)\s*=\s*(.+)\s*$/o)
363   {
364     my ($key, $value) = ($1, $2);
365     $key =~ s/\//./go;
366     $confhash{$key} = $value;
367   }
368 }
369 for (sort { $a cmp $b } keys %confhash)
370 {
371   print $_, "=", $confhash{$_}, "\n";
372 }
373 '
374 rm -f /etc/sysctl.conf-new
375 perl -e "$script" < /etc/sysctl.conf.$$ > /etc/sysctl.conf-new
376 cat /etc/sysctl.conf-head /etc/sysctl.conf-new > /etc/sysctl.conf.$$
377 rm -f /etc/sysctl.conf-head /etc/sysctl.conf-new
378
379 # finished with merging, move into sysctl.conf
380 cp_mv /etc/sysctl.conf.$$ /etc/sysctl.conf
381 echo -n " sysctl.conf"
382
383 # finished with basic kernel-2.6-cn stuff
384 echo "."
385
386 ################################################################################
387
388 # intro
389 echo -n "CN: Setting up PAM configurations:"
390
391 # update pam_limits accordingly
392 if [ -e /etc/security/limits.conf ]; then
393     rm -f /etc/security/limits.conf.$$
394     cp /etc/security/limits.conf /etc/security/limits.conf.$$
395     cp-update kernel-2.6-cn /etc/security/limits.conf.$$ <<EOF
396 *               soft    core            0
397 *               hard    nofile          4096
398 *               soft    nofile          4096
399 EOF
400     cp_mv /etc/security/limits.conf.$$ /etc/security/limits.conf
401     echo -n " limits"
402 fi
403
404 # check pam.d/login
405 if [ -e /etc/pam.d/login ]; then
406     cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
407         's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
408         /etc/pam.d/login || true
409     echo -n " login"
410 fi
411
412 # check pam.d/ssh
413 if [ -e /etc/pam.d/ssh ]; then
414     cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
415         's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
416         /etc/pam.d/ssh || true
417     echo -n " ssh"
418 fi
419
420 # finished with PAM
421 echo "."
422
423 ################################################################################
424
425 # fix old kernel-2.4-cn postrm
426 if [ -e /var/lib/dpkg/info/kernel-2.4-cn.postrm ]; then
427     echo "CN: Fixed old kernel-2.4-cn postrm."
428     cat > /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ <<EOF
429 #!/bin/sh
430
431 set -e
432
433 # be sure, be safe
434 if [ "$1" != "remove" ]; then
435   exit 0
436 fi
437
438 # import CN-functions
439 . /usr/share/carnet-tools/functions.sh
440
441 # remove us from limits.conf
442 cp-update -r kernel-2.4-cn /etc/security/limits.conf
443
444 # remove us from modules
445 cp-update -r kernel-2.4-cn /etc/modules
446 EOF
447     if ! cmp -s /var/lib/dpkg/info/kernel-2.4-cn.postrm \
448         /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$; then
449         mv /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ \
450             /var/lib/dpkg/info/kernel-2.4-cn.postrm
451         chmod +x /var/lib/dpkg/info/kernel-2.4-cn.postrm
452     fi
453     rm -f /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$
454 fi
455
456 ################################################################################
457
458 # fix old kernel-cn postrm
459 if [ -e /var/lib/dpkg/info/kernel-cn.postrm ]; then
460     echo "CN: Fixed old kernel-cn postrm."
461     cat > /var/lib/dpkg/info/kernel-cn.postrm.$$ <<EOF
462 #!/bin/sh
463
464 set -e
465
466 # be sure, be safe
467 if [ "$1" != "remove" ]; then
468   exit 0
469 fi
470
471 # import CN-functions
472 . /usr/share/carnet-tools/functions.sh
473
474 # remove us from limits.conf
475 cp-update -r kernel-cn /etc/security/limits.conf
476
477 # remove us from modules
478 cp-update -r kernel-cn /etc/modules
479 EOF
480     if ! cmp -s /var/lib/dpkg/info/kernel-cn.postrm \
481         /var/lib/dpkg/info/kernel-cn.postrm.$$; then
482         mv /var/lib/dpkg/info/kernel-cn.postrm.$$ \
483             /var/lib/dpkg/info/kernel-cn.postrm
484         chmod +x /var/lib/dpkg/info/kernel-cn.postrm
485     fi
486     rm -f /var/lib/dpkg/info/kernel-cn.postrm.$$
487 fi
488
489 ################################################################################
490
491 # dh_installdeb will replace this with shell code automatically
492 # generated by other debhelper scripts.
493
494 #DEBHELPER#
495
496 exit 0