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