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