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