izmjene za jessie
[kernel-cn.git] / debian / postinst
1 #!/bin/sh
2 # postinst script for kernel-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-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 if necessary:"
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 # detect existing Grub2 installation
90
91 GRUB2="no"
92 if [ \( -e /boot/grub/grub.cfg \) -o \( -e /boot/grub/core.img \) -o \( -e /usr/lib/grub/i386-pc/ext2.mod \) ]; then
93     echo "CN: Detected GRUB2 installation, will try to use it."
94     GRUB2="yes"
95     if [ -e /boot/grub/menu.lst ]; then
96         echo "CN: Oops, GRUB1 (Legacy) installation detected. Will try to upgrade to GRUB2."
97     fi
98 else
99     echo "CN: No GRUB2 detected, will continue with GRUB1 as default option."
100 fi
101
102 ################################################################################
103
104 # check if we are under Xen DomU PV
105 # (perhaps check /sys/hypervisor/uuid in the future)
106
107 GRUB_MBR="yes"
108 if [ \( -w /dev/xvda \) -o \( -w /dev/xvdb \) ]; then
109     echo "CN: Detected DomU instance, won't install Grub MBR."
110     GRUB_MBR="no"
111 fi
112
113 ################################################################################
114
115 # intro msg
116 echo -n "CN: Configuring system (this will take a while):"
117
118 # generate kernel-img.conf
119 if [ ! -e /etc/kernel-img.conf ]; then
120     touch /etc/kernel-img.conf
121 fi
122
123 # update postinst_hook for grub/grub2
124 if grep -q postinst_hook /etc/kernel-img.conf; then
125     cp_check_and_sed '^postinst_hook' \
126         's;^postinst_hook[[:blank:]]*=.*;postinst_hook = /usr/sbin/update-grub;g' \
127         /etc/kernel-img.conf || true
128 else
129     echo "postinst_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
130 fi
131
132 # update postrm_hook for grub/grub2
133 if grep -q postrm_hook /etc/kernel-img.conf; then
134     cp_check_and_sed '^postrm_hook' \
135         's;^postrm_hook[[:blank:]]*=.*;postrm_hook = /usr/sbin/update-grub;g' \
136         /etc/kernel-img.conf || true
137 else
138     echo "postrm_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
139 fi
140
141 # enable initrd
142 if grep -q do_initrd /etc/kernel-img.conf; then
143     cp_check_and_sed '^do_initrd' \
144         's/^do_initrd[[:blank:]]*=.*/do_initrd = yes/g' \
145         /etc/kernel-img.conf || true
146 else
147     echo "do_initrd = yes" >> /etc/kernel-img.conf
148 fi
149
150 echo -n " kernel-img.conf"
151
152 # generate initial grub loaders
153 if [ "x$GRUB2" = "xno" ]; then
154     # Grub1 part
155     if [ ! -d "$grub_dir" ]; then
156         mkdir -p "$grub_dir"
157         # x86
158         if [ -d /usr/lib/grub/i386-pc ]; then
159             cp -a /usr/lib/grub/i386-pc/* "$grub_dir"
160         # x86_64
161         elif [ -d /usr/lib/grub/x86_64-pc ]; then
162             cp -a /usr/lib/grub/x86_64-pc/* "$grub_dir"
163         fi
164     fi
165 else
166     # Grub2 part
167     if [ ! -d /boot/grub ]; then
168         mkdir -p /boot/grub
169         if [ -d /usr/lib/grub/i386-pc ]; then
170             cp -a /usr/lib/grub/i386-pc/* /boot/grub
171         fi
172     fi
173 fi
174 echo -n " grub1"
175
176 # create/update grub configuration
177 if [ "x$GRUB2" = "xno" ]; then
178     # Grub1 part
179     if [ -e "$menu_file" ]; then
180         # is there uncompatibile grub conf present?
181         if ! grep -q 'AUTOMAGIC KERNELS LIST' "$menu_file"; then
182             mv -f "$menu_file" "$menu_file.old"
183         fi
184     fi
185     if [ ! -e "$menu_file" ]; then
186         yes | update-grub >/dev/null 2>&1 || true
187     else
188         update-grub >/dev/null 2>&1 || true
189     fi
190 else
191     # Grub2 part
192     touch /boot/grub/grub.cfg
193     update-grub >/dev/null 2>&1 || true
194 fi
195 echo -n " grub2"
196
197 # workaround grsec
198 if uname -a | grep -q grsec; then
199     if [ -x /sbin/chpax ]; then
200         # grub
201         if [ -x /usr/sbin/grub.real ]; then
202             chpax -spmrx /usr/sbin/grub.real >/dev/null 2>&1 || true
203         elif [ -x /usr/sbin/grub ]; then
204             chpax -spmrx /usr/sbin/grub >/dev/null 2>&1 || true
205         fi
206
207         # grub-probe
208         if [ -x /usr/sbin/grub-probe.real ]; then
209             chpax -spmrx /usr/sbin/grub-probe.real >/dev/null 2>&1 || true
210         elif [ -x /usr/sbin/grub-probe ]; then
211             chpax -spmrx /usr/sbin/grub-probe >/dev/null 2>&1 || true
212         fi
213
214         echo -n " grsec"
215     fi
216 fi
217
218 # remove obsolete devfs/compat links and restore normal udev behaviour
219 rm -f /etc/udev/rules.d/devfs.rules /etc/udev/rules.d/compat.rules \
220     /etc/udev/rules.d/compat-full.rules
221 #if [ ! -e /etc/udev/rules.d/udev.rules ]; then
222 #    ln -s ../udev.rules /etc/udev/rules.d/udev.rules
223 #fi
224 udevadm control --reload_rules >/dev/null 2>&1 || true
225 udevadm settle || true
226 echo -n " udev"
227
228 # update device map if possible
229 if [ "x$GRUB2" = "xno" ]; then
230     # Grub1 part
231     if [ -f "$device_map" ]; then
232         mv -f "$device_map" "$device_map.old"
233     fi
234     # possible situation when upgrading from Grub1 to Grub2
235     if [ ! -x /usr/sbin/grub ]; then
236         echo "."
237         echo "CN: FATAL ERROR while trying to execute GRUB1!"
238         echo "CN: Do not reboot your server and report this to syshelp@carnet.hr immediately!"
239         exit 1
240     fi
241     grub --batch --no-floppy --device-map="$device_map" <<'EOF' >/dev/null 2>&1 || true
242 quit
243 EOF
244     if [ ! -s "$device_map" ]; then
245         if [ -f "$device_map.old" ]; then
246             mv -f "$device_map.old" "$device_map"
247         fi
248
249     else
250         rm -f "$device_map.old"
251     fi
252 else
253     # Grub2 part
254     grub-mkdevicemap --no-floppy >/dev/null 2>&1 || true
255     grub-install --no-floppy --grub-setup=/bin/true "$(grub-probe -t drive /boot/grub)" >/dev/null 2>&1 || true
256 fi
257 echo -n " grub3"
258
259 # import GRUB helper functions (again, updated device map)
260 . /usr/share/kernel-cn/grub-functions.sh
261
262 if [ "x$GRUB2" = "xno" ]; then
263     # Grub1 part
264     # get install device (0x80 BIOS device)
265     install_device=$(grep '^(hd0)' "$device_map" | \
266         sed -e 's%[^[:space:]]*[[:space:]]*\([^[:space:]]*\)%\1%')
267     if [ -z "$install_device" ]; then
268         echo "."
269         echo "CN: FATAL ERROR while detecting boot disk!"
270         echo "CN: Do not reboot your server and report this to syshelp@carnet.hr immediately!"
271         exit 1
272     fi
273
274     # oops, install device is a symlink...
275     if [ -h "$install_device" ]; then
276         install_device_resolved=$(resolve_symlink "$install_device")
277         if [ -z "$install_device_resolved" ]; then
278             install_device_resolved="$install_device"
279         fi
280
281         # try to fix device map with symlink resolved device
282         if [ "x$install_device_resolved" != "x$install_device" ]; then
283             cp_check_and_sed '^\(hd0\)' \
284                 "s;^(hd0).*;(hd0)       $install_device_resolved;" "$device_map" \
285                 || true
286         fi
287     fi
288 else
289     # Grub2 part
290     install_device="(hd0)"
291 fi
292 echo -n " grub4"
293
294 # install GRUB loader: this will work for both Grub1 and Grub2
295 if [ "x$GRUB_MBR" = "xyes" ]; then
296     if ! grub-install --no-floppy "$install_device" >/dev/null 2>&1; then
297         echo "."
298         echo "CN: FATAL ERROR while running grub-install on $install_device!"
299         echo "CN: Do not reboot your server and report this to syshelp@carnet.hr immediately!"
300         exit 1
301     fi
302     echo -n " grub5"
303 fi
304
305 # fix possible wrong params in menu.lst
306 if [ "x$GRUB2" = "xno" ]; then
307     # Grub1 part
308     cp_check_and_sed '^# groot=' \
309       "s;^# groot=.*;# groot=$grub_root_device;g" \
310           "$menu_file" || true
311     cp_check_and_sed '^# kopt=' \
312       "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
313           "$menu_file" || true
314 fi
315 update-grub >/dev/null 2>&1 || true
316 echo -n " grub6"
317
318 # fix possible wrong params in menu.lst
319 if [ "x$GRUB2" = "xno" ]; then
320     # Grub1 part
321     # ... and final stage of devfs/udev brokeness fix
322     if [ "x$install_device_resolved" != "x$install_device" ]; then
323         # get original (symlinked device)
324         root_device=$(find_device_nonresolved "/")
325         if [ -z "$root_device" ]; then
326             root_device=$(find_root_device)
327         fi
328
329         # fix (hd0) mapping
330         cp_check_and_sed '^\(hd0\)' \
331             "s;^(hd0).*;(hd0)   $install_device;" "$device_map" \
332             || true
333
334         # fix global root= invocation
335         cp_check_and_sed '^# kopt=' \
336           "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
337               "$menu_file" || true
338
339         # fix root= invocation for individual kernel profiles
340         cp_check_and_sed '^kernel' \
341           "s;\(^kernel.*\)root=[^[:space:]]*\(.*\);\1root=$root_device\2;g" \
342               "$menu_file" || true
343     fi
344 fi
345 echo -n " grub7"
346
347 # install to other boot sectors if needed (better safe than sorry)
348 if [ "x$GRUB_MBR" = "xyes" ]; then
349     if [ "x$GRUB2" = "xyes" ]; then
350         # Grub2 part
351         db_get grub-pc/install_devices
352         for i in `echo $RET | sed -e 's/,/ /g'`; do
353             real_device="$(readlink -f "$i")"
354             if [ -e "$real_device" ]; then
355                 grub-install --force --no-floppy $real_device \
356                     >/dev/null 2>&1 || true
357             fi
358         done
359     fi
360     echo -n " grub8"
361 fi
362
363 # disable lilo
364 if [ -e /etc/lilo.conf ]; then
365     mv -f /etc/lilo.conf /etc/lilo.conf.old
366     echo -n " lilo"
367 fi
368
369 # mdadm
370 if [ -x /usr/share/mdadm/mkconf ]; then
371     if [ ! -e /etc/mdadm/mdadm.conf ]; then
372         touch /etc/mdadm/mdadm.conf
373     fi
374
375     /usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf.$$
376     if ! cmp -s /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf; then
377         mv /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf
378     fi
379     rm -f /etc/mdadm/mdadm.conf.$$ /etc/initramfs-tools/hooks/md \
380         /var/lib/mdadm/CONF-UNCHECKED
381     echo -n " mdadm"
382 fi
383
384 # update initramfs accordingly (because of mdadm and udev)
385 update-initramfs -u -k all >/dev/null 2>&1 || true
386 echo -n " initramfs"
387
388 # finished
389 echo "."
390
391 ################################################################################
392
393 # rest of configuration...
394 echo -n "CN: Modifying the neccessary system files:"
395
396 # remove group 99
397 if getent group proc >/dev/null 2>&1; then
398     groupdel proc >/dev/null 2>&1
399     echo -n " proc"
400 fi
401
402 # remove oidentd from oident group
403 if getent group oident >/dev/null 2>&1; then
404     # sarge default
405     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup' \
406       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup/OIDENT_GROUP=oident/g' \
407           /etc/default/oidentd || true
408
409     # old kernel-2.6-cn default
410     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc' \
411       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc/OIDENT_GROUP=oident/g' \
412           /etc/default/oidentd || true
413
414     echo -n " oidentd"
415 fi
416
417 # default kernel parameters
418 rm -f /etc/sysctl.conf.$$
419
420 # old kernel params (skipping some of the obsolete or overrided entries)
421 if [ -e /etc/sysctl.conf ]; then
422     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\.maps_protect' \
423         /etc/sysctl.conf >> /etc/sysctl.conf.$$
424 fi
425
426 # finished with merging, move into sysctl.conf
427 cp_mv /etc/sysctl.conf.$$ /etc/sysctl.conf
428 echo -n " sysctl.conf"
429
430 # reload sysctl ASAP
431 service procps restart
432
433 # finished with basic kernel-cn stuff
434 echo "."
435
436 ################################################################################
437
438 # intro
439 echo -n "CN: Setting up PAM configurations:"
440
441 # update pam_limits accordingly
442 if [ -e /etc/security/limits.conf ]; then
443     rm -f /etc/security/limits.conf.$$
444     cp /etc/security/limits.conf /etc/security/limits.conf.$$
445     cp-update kernel-cn /etc/security/limits.conf.$$ <<'EOF'
446 *       soft    core        0
447 *               hard    nofile          16384
448 *               soft    nofile          16384
449 @users  soft    nproc       100
450 @users  hard    nproc       150
451 EOF
452     cp_mv /etc/security/limits.conf.$$ /etc/security/limits.conf
453     echo -n " limits"
454 fi
455
456 # check pam.d/login
457 if [ -e /etc/pam.d/login ]; then
458     cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
459         's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
460         /etc/pam.d/login || true
461     echo -n " login"
462 fi
463
464 # check pam.d/ssh
465 if [ -e /etc/pam.d/ssh ]; then
466     cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
467         's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
468         /etc/pam.d/ssh || true
469     echo -n " ssh"
470 fi
471
472 # finished with PAM
473 echo "."
474
475 ################################################################################
476
477 # dh_installdeb will replace this with shell code automatically
478 # generated by other debhelper scripts.
479
480 #DEBHELPER#
481
482 exit 0