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