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