Update of grml_cd stuff, several new files and features
[grml-live.git] / etc / grml / fai / grml / grml_cleanup_chroot
1 #!/bin/bash
2 # Filename:      grml.clean
3 # Purpose:       clean up grml chroot
4 # Authors:       (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is copyright by Michael Prokop
7 # Latest change: Thu May 17 15:44:56 CEST 2007 [mika]
8 ################################################################################
9
10 # misc stuff
11   umask 022
12   . /etc/grml_colors        || exit 5
13   . /etc/grml/lsb-functions || exit 6
14
15 #  /usr/share/doc/texlive-latex-recommended/latex/
16 #  /usr/share/doc/texlive-latex-base/latex/
17 #  /usr/share/doc/texlive-base-bin/pdftex/thanh/
18 #  /usr/share/doc/texlive-latex-base/latex/base/
19 #  /usr/share/doc/texlive-latex-base/latex/hyperref/
20 #  /usr/share/doc/texlive-latex-base/generic/babel/ 
21 #  /usr/share/doc/texlive-latex-recommended/latex/koma-script/
22 #  /usr/share/doc/texmf/pgf/pgfmanual.pdf.gz
23
24 # variables
25   grep -q small /etc/grml_version && GRML_SMALL=yes  || GRML_SMALL=''
26   [ -x /lib64 ] && GRML64=yes || GRML64=''
27   if [ -n "$GRML_SMALL" ] ; then
28      echo -n 'Are you sure you want to run GRML_SMALL stuff? [y|N] '
29      read a
30      if [ "$a" == 'y' ] ; then
31         echo 'Continuing as requested.'
32      else
33         echo 'Exiting as requested.'
34         unset GRML_SMALL
35         exit 1
36      fi
37   fi
38
39 # detect kernel version, assume newest kernel version
40   KERNEL=$(ls -1 /lib/modules/ | sort -r -u | head -1)
41
42 if [ -f /usr/share/fonts/X11/misc/artwiz-cursor.pcf.gz ] ; then
43   if dpkg-divert --list artwiz-cursor | grep -q /usr/share/fonts/X11/misc/cursor.pcf.gz ; then
44      eerror "Broken diversion of artwiz-cursor, see #341397"
45      exit 1
46   fi
47 fi
48
49 #if [ -r /usr/share/fonts/X11/misc/cursor.pcf.gz ] ; then
50 #  einfo "Fixing the broken artwiz-cursor"
51 #  ( cd /usr/share/fonts/X11/misc/ && \
52 #    dpkg-divert --remove /usr/share/fonts/X11/misc/cursor.pcf.gz && mkfontdir )
53 #  eend $?
54 #fi
55
56 # start of main cleanup process...
57 if ! [ -r /boot/config-"$KERNEL" ] ; then
58   eerror "/boot/config-$KERNEL not available. \$KERNEL not set to correct value?"
59   eend 1
60   exit 9
61 fi
62
63 einfo "Preparing grml-system: `cat /etc/grml_version`." ; eend 0
64
65 einfo "Setting up /etc/motd.tail"
66 echo "" > /etc/motd.tail ; eend 0
67
68 einfo "Do not forget to run the final check:"
69  eindent
70   einfo 'adjust grml-version: vim -X /etc/grml_version'
71   einfo 'is runlevel.conf ok: vimdiff -X /etc/runlevel.conf /etc/runlevel.conf.livecd'
72   einfo 'find old files:      find /etc -name \*dpkg\*'
73   einfo "delete conffiles:    dpkg --purge \`dpkg --list | grep '^rc' | awk '{print \$2}'\`"
74   einfo 'dangling symlinks:   ls **/*(-@)'
75  eoutdent
76
77 einfo "Setting up /etc/modules: "
78 cat>/etc/modules<<EOF
79 # /etc/modules: kernel modules to load at boot time.
80 #
81 # This file should contain the names of kernel modules that are
82 # to be loaded at boot time, one per line.  Comments begin with
83 # a \`#', and everything on the line after them are ignored.
84 EOF
85 echo "" >> /etc/modules
86 grep '^[a-z]' /etc/grml/modules >> /etc/modules
87 eend 0
88
89 einfo "Setting up /etc/inittab"
90 if [ -z "$GRML_SMALL" ] ; then
91    cp /etc/inittab.grml  /etc/inittab
92 else
93    cp /etc/inittab.small /etc/inittab
94 fi
95 eend 0
96
97 if [ -z "$GRML_SMALL" ] ; then
98   if [ -L /usr/include/linux ] ; then
99     einfo "/usr/include/linux is ok" ; eend 0
100   elif [ -n "$GRML64" ] ; then
101       if [ -d /usr/src/linux-headers-"$KERNEL" ] ; then
102          einfo "grml64 detected, assuming /usr/src/linux-headers-$KERNEL is ok." ; eend 0
103       else
104          eerror "/usr/src/linux-headers-$KERNEL not found, exiting." ; eend 1
105          exit 10
106       fi
107   elif [ -d /usr/src/linux-headers-"$KERNEL" ] ; then
108        eerror "/usr/include/linux is NOT a symlink to /usr/src/linux/include/linux/ - error"
109        eend 1
110        exit 10
111   else
112        ewarn "linux-headers-$KERNEL not installed." ; eend 0
113   fi
114 fi
115
116 einfo "Creating ~/.zshrc"
117   touch ~/.zshrc
118 eend $?
119
120 if [ -r /usr/share/lintian/overrides/ion3 ] ; then
121   einfo "Stripping ion3-stuff"
122   cat /usr/share/lintian/overrides/ion3 | sed 's#.*./usr#/usr#' | \
123   sed 's/.comment//' | xargs strip --strip-unneeded ; eend $?
124 else
125   ewarn "ion3 not installed" ; eend 0
126 fi
127
128 if [ -d /usr/lib/valgrind/x86-linux/ ] ; then
129   einfo "Stripping /usr/lib/valgrind/x86-linux/"
130   strip  --strip-unneeded  /usr/lib/valgrind/x86-linux/*
131   eend $?
132 else
133   ewarn "valgrind not installed" ; eend 0
134 fi
135
136 if [ -x /usr/sbin/localepurge ] ; then
137   einfo "Running localepurge"
138   localepurge ; eend $?
139 else
140   ewarn "localepurg not available" ; eend 0
141 fi
142
143 if [ -d /var/lib/backuppc/pc/localhost/ ] ; then
144   einfo "Cleaning /var/lib/backuppc/pc/localhost/"
145   rm -rf /var/lib/backuppc/pc/localhost/* ; eend $?
146 else
147   ewarn "backuppc not installed" ; eend 0
148 fi
149
150 # find /var -name \*.pid
151 einfo "Removing pid-files"
152   find /var/run -name \*.pid -exec rm {} \;
153 eend $?
154
155 einfo "Unmounting proc, /dev/pts, /dev/capi"
156   umount /proc     2>/dev/null
157   umount /dev/pts  2>/dev/null
158   umount /dev/capi 2>/dev/null
159   umount -a 2>/dev/null
160 eend $?
161
162 #einfo "Removing initrd-images"
163 #  rm -f /boot/initrd.img-*
164 #eend $?
165
166 einfo "Removing /var/lib/apt/lists/*-stuff, dpkg-status-old and pkgcache.bin"
167   # for i in $(find /var/lib/apt/lists -type f \( -name \*Packages -o -name \*Sources \) 2>/dev/null); do  :>"$i"; done
168   # for i in /var/lib/apt/lists/*_{Release,Packages,Sources}; do :>$i; touch -t 198103190000.00 $i; done
169   # sync-available 2>/dev/null
170   # apt-get check
171   rm -f /var/lib/apt/lists/*Packages
172   rm -f /var/lib/apt/lists/*Release
173   rm -f /var/lib/apt/lists/*Sources
174   rm -f /var/lib/apt/lists/*.gpg
175   rm -f /var/cache/apt-show-versions/*
176   # :> /var/cache/apt/pkgcache.bin
177   # :> /var/cache/apt/srcpkgcache.bin
178   rm -f /var/cache/debconf/templates.dat-old
179   rm -f /var/cache/apt/*.bin
180   apt-get check 2>/dev/null
181   dpkg --clear-avail
182   apt-cache gencaches 2>/dev/null
183   rm -f /var/lib/dpkg/status-old
184   rm -f /var/lib/dpkg/available-old
185 eend $?
186
187 if [ -x /usr/bin/grep-dctrl ] ; then
188    einfo "Cleaning up /var/lib/dpkg/status"
189    grep-dctrl -v -F Status "purge ok not-installed" /var/lib/dpkg/status > status.new && \
190    cp status.new /var/lib/dpkg/status && chmod 644 /var/lib/dpkg/status  && \
191    chown root:root /var/lib/dpkg/status && rm status.new && eend 0 || eend 1
192 else
193    ewarn "Warning: /usr/bin/grep-dctrl not available."
194 fi
195
196 einfo "Copying original runlevel.conf to /etc/runlevel.conf"
197   if ! [ -z $GRML_SMALL ] ; then
198     cp /etc/runlevel.conf.livecd.small /etc/runlevel.conf
199   else
200     cp /etc/runlevel.conf.livecd /etc/runlevel.conf
201   fi
202 eend $?
203
204 einfo "Removing x-server-conffiles"
205   rm -f /etc/X11/xorg.conf /etc/X11/XF86Config-4 \
206         /etc/X11/XF86Config /etc/XF86Config /etc/XF86Config-4 
207 eend $?
208
209 einfo "Removing ssh-keys"
210   rm -f /etc/ssh/*key*
211 eend $?
212
213 # remove only "temporary" or saved files in the given directories
214 nuke(){
215   for i in `find "$@" -name \*.gz -o -name \*.bz2 -o -name \*.0 2>/dev/null`; do
216     rm -f "$i"
217   done
218 }
219
220 # set all files in the given directories to a length of zero
221 zero(){
222   for i in `find "$@" -type f -size +0 -not -name \*.ini 2>/dev/null`; do
223     :> "$i"
224   done
225 }
226
227 #rmdir /mnt/cdrom?* /mnt/hd?* 2>/dev/null
228
229 #for i in `find /usr/*/man -name \*.\[0-9ln\]` ; do
230 #  [ -f "$i".gz -o -f "$i".bz2 ] && rm -f "$i"
231 #done
232
233 if [ -d /var/spool/squid/ ] ; then
234   einfo "Cleaning /var/spool/squid/0*"
235   rm -rf /var/spool/squid/0*
236   eend $?
237 else
238   ewarn "update-ca-certificates not installed" ; eend 0
239 fi
240
241 # delete old dowloaded packages
242 einfo "Running apt-get clean"
243   apt-get clean
244 eend $?
245
246 # remove files from packages which have been removed
247 # COLUMNS=200 dpkg --purge `dpkg -l | grep '^rc' | awk '{ print $2 }'` 
248
249 if [ -x /usr/bin/nvi ] ; then
250   if ! [ -d /var/tmp/vi.recover ] ; then
251     einfo "Creating nvi /var/tmp directory"
252     mkdir -p /var/tmp/vi.recover
253     chown root:root /var/tmp/vi.recover
254     chmod 1777 /var/tmp/vi.recover
255     eend $?
256   fi
257 else
258   ewarn "nvi not installed" ; eend 0
259 fi
260
261 if [ -x /sbin/depmod ] ; then
262   if [ -r /lib/modules/$KERNEL ] ; then
263     einfo "Updating modules.dep for kernel $KERNEL"
264     # depmod -a $KERNEL
265     depmod -ae -F /boot/System.map-"$KERNEL" "$KERNEL"
266     eend $?
267   fi
268 else
269   ewarn "depmod not installed" ; eend 0
270 fi
271
272 if [ -n "$NOONLINE" ] ; then
273    einfo "\$NOONLINE is set, skipping online tasks" ; eend 0
274 else
275
276 if [ -x /usr/bin/freshclam ] ; then
277   einfo "Updating clamav database via running freshclam"
278   /usr/bin/freshclam ; eend $?
279 else
280   ewarn "freshclam not installed" ; eend 0
281 fi
282
283 if [ -x /usr/bin/update-pciids ] ; then
284   einfo "Updating PCI- and USB-ids"
285   update-pciids
286   update-usbids
287 #  ls -la /usr/share/misc/*.ids* 
288 #  ls -la /var/lib/usbutils/usb*
289   eend $?
290 else
291   ewarn "update-pciids/update-usbids not installed" ; eend 0
292 fi
293
294 if [ -x /usr/sbin/nessus-update-plugins ] ; then
295    einfo "Updating nessus-plugins"
296    nessus-update-plugins
297    eend $?
298 else
299   ewarn "nessus-update-plugins not installed" ; eend 0
300 fi
301
302 fi # end of $NOONLINE-check
303
304 einfo "Cleaning and removing some misc files and directories"
305   rm -f  /etc/sysconfig/* /etc/motd.dpkg-new /etc/auto.master.distrib.dpkg-new    \
306          /etc/samba/*.SID /etc/samba/*.tdb /dev/mouse* /dev/cdrom* /dev/cdwriter* \
307          /var/run/*/* /var/run/* /var/log/ksymoops/* /var/lock/*/* /var/lock/*    \
308          /var/state/*/* /var/state/* /var/log/nessus/*                            \
309          /halt /reboot /ash.static /etc/dhcpc/*.info /etc/dhcpc/resolv*           \
310          /etc/resolv.conf /etc/*passwd- /etc/*shadow- /etc/*group-                \
311          /var/spool/postfix/maildrop/*                                            \
312          /etc/lvm/.cache 2>/dev/null
313   rm -rf /tmp/* /var/tmp/* /var/tmp/.* /var/backups/* /.ssh /root/.ssh /home/*/.ssh \
314          /home/grml/* /home/grml/.??* /var/lib/texmf/ls-R  /var/spool/texmf/ls-R    \
315          /var/lib/mysql /var/log/lilo_log.* 2>/dev/null
316   nuke   /var/log       /var/cache
317   zero   /var/local     /var/log /var/spool \
318          /var/lib/games /var/cache/man      \
319          /var/lib/nfs   /var/lib/xkb        \
320          /var/mail/grml /var/account/pacct
321   mkdir -p /etc/sysconfig/network-scripts /etc/sysconfig/provider
322   # Recreate empty utmp and wtmp
323   :>/var/run/utmp
324   :>/var/run/wtmp
325 eend $?
326
327 einfo "Setting up /etc/resolv.conf"
328   if [ -d /etc/dhcpc ] ; then
329     cat >/etc/dhcpc/resolv.conf <<EOT
330 # insert nameservers here
331 # nameserver 127.0.0.1
332 EOT
333     chmod 644 /etc/dhcpc/resolv.conf
334   else
335     cat >/etc/resolv.conf <<EOT
336 # insert nameservers here
337 # nameserver 127.0.0.1
338 EOT
339     chmod 644 /etc/resolv.conf
340   fi
341
342    # is resolvconf available?
343    if [ -r /etc/resolvconf/run/resolv.conf ] ; then
344       ln -sf /etc/resolvconf/run/resolv.conf /etc/
345    else
346       ln -s /etc/dhcpc/resolv.conf /etc/
347    fi
348 eend $?
349
350 if [ -x /usr/sbin/update-ca-certificates ] ; then
351   einfo "Updating ca-certificates"
352   update-ca-certificates 1>/dev/null
353   eend $?
354 else
355   ewarn "update-ca-certificates not installed" ; eend 0
356 fi
357
358 # some stuff does not have to run every single time...
359 if [ -n "$FAST" ] ; then
360    einfo "\$FAST is set, skipping some tasks" ; eend 0
361 else
362
363 # regenerate module dependencies and ls.so.cache
364 if [ -x /sbin/ldconfig ] ; then
365   einfo "Updating ld.so.cache"
366   ldconfig
367   eend $?
368 else
369   ewarn "ldconfig not installed" ; eend 0
370 fi
371
372 #if [ -r /lib/modules/$KERNEL/ ] ; then
373 #  einfo "Searching for modules not compiled against $KERNEL"
374 #  ( cd /lib/modules/$KERNEL/ && modinfo **/*.ko | grep vermagic | grep -v $KERNEL )
375 #  eend 0
376 #else
377 #  eerror "/lib/modules/$KERNEL/ not found" ; eend 1
378 #fi
379
380 if [ -x /usr/bin/update-menus ] ; then
381   einfo "Updating windowmanager menus"
382   update-menus -v
383   eend $?
384 else
385   ewarn "update-menus not installed" ; eend 0
386 fi
387
388 if [ -x /usr/bin/mktexlsr ] ; then
389   einfo "Updating texhash"
390   mktexlsr
391   eend $?
392 else
393   ewarn "mktexlsr not installed" ; eend 0
394 fi
395
396 if [ -x /usr/bin/mandb ] ; then
397   einfo "Updating mandb"
398   mandb -c
399   man doesnotexist >/dev/null 2>&1
400   eend $?
401 else
402   ewarn "mandb not installed" ; eend 0
403 fi
404
405 if [ -n "$GRML_SMALL" ] ; then
406   einfo "Cleaning up /etc/skel..."
407   rm -rf /etc/skel/.fluxbox 
408   rm -rf /etc/skel/.gkrellm2 
409   rm -rf /etc/skel/.idesktop 
410   rm -rf /etc/skel/.pekwm
411   eend $?
412
413   einfo "Cleaning documentation directories..."
414   mv /usr/share/doc/grml-docs /tmp/
415   rm -rf /usr/share/doc
416   mkdir /usr/share/doc
417   mv /tmp/grml-docs /usr/share/doc/grml-docs
418   rm -rf /usr/share/gtk-doc/
419   rm -rf /usr/share/man/
420   rm -rf /usr/man
421   rm -rf /usr/share/info
422   eend $?
423
424   einfo "Creating /usr/share/info/..."
425   mkdir -p /usr/share/info/
426   eend $?
427 fi
428
429 if [ -d /var/lib/clamav/ ] ; then
430   einfo "Cleaning /var/lib/clamav/"
431   rm -f /var/lib/clamav/clamav-* ; eend $?
432   if [ -f /var/lib/clamav/daily.cvd ] ; then
433      ( mkdir -p /usr/share/doc/clamav-freshclam/examples/ && \
434        cd /usr/share/doc/clamav-freshclam/examples/ && \
435        ln -sf /var/lib/clamav/daily.cvd . && \
436        ln -sf /var/lib/clamav/main.cvd  . )
437   fi
438 else
439   ewarn "clamav[-freshclam] not installed" ; eend 0
440 fi
441
442 if ! [ -r /etc/ld.so.nohwcap ] ; then
443    einfo "Creating /etc/ld.so.nohwcap"
444    touch /etc/ld.so.nohwcap
445 fi
446
447 if [ -x /usr/bin/updatedb ] ; then
448   einfo "Updating locate-database"
449   mount -t proc none /proc
450   updatedb --prunepaths='/tmp /usr/tmp /var/tmp /grml /root /proc /sys'
451   umount /proc
452   eend $?
453 else
454   ewarn "updatedb not installed" ; eend 0
455 fi
456
457 fi # end of $FAST-check
458
459 einfo "Unmounting all filesystems"
460   umount -a
461 eend $?
462
463 ################################################################################
464 # MISC STUFF
465 # ~~~~~~~~~~
466 #
467 # Security-audit:
468 # find / \! -type l -perm -2000 -ls > audit/sgid-files 
469 # find / \! -type l -perm -0002 -ls > audit/world-writeable-files
470 # find / \! -type l -perm -4000 -ls > audit/suid-files
471 # find / ( -perm 4000 -o -perm -2000 \) -exec ls -ld {} \;
472 # find / -perm -0777 -type d -ls > audit/writeable-dirs
473 # find / -size +3000k > audit/big_files.new
474 # find /all_filesystems -name ".. " -print -xdev | cat -vet
475 # find / -name ".*" -print -xdev | cat -v > audit/hidden-files
476 # find / -name ".. " -print -xdev | cat -vet > audit/unusal-file-names
477 #
478 # list non-stripped files:
479 # file /usr/sbin/* | grep -v ', stripped' | grep -v 'perl' | grep -v 'Bourne' | grep -v 'python' | grep -v 'symbolic link'
480 #
481 # einfo "Removing unused architecture Kernel sources: "
482 #   for i in `ls -1 /usr/src/linux/arch/ | grep -v i386`; do
483 #     echo -n "$i "; rm -rf /usr/src/linux/arch/"$i" /usr/src/linux/include/asm-"$i"
484 #   done
485 # eend $?
486 #
487 # Is software by grml-repos up2date?
488 # grep-dctrl --field Maintainer 'Michael Prokop' /var/lib/apt/lists/grml.org_repos_._* | grep Package | awk '{print $2}' | xargs echo 
489 #
490 # runlevel.conf:
491 # for file in /etc/init.d/* ; do grep $file -q /etc/runlevel.conf.hdinstall || echo $file not found ; done
492 ################################################################################
493 # ISO
494 # ~~~
495 # /usr/bin/mksquashfs /home/mika/grml/chroot/grml_uncompressed.large/* \
496 # /home/mika/grml/chroot/grml_cd.large/GRML/GRML  -noappend -sort /home/mika/grml/svn/grml/remastering/sort
497 #
498 # mkisofs -V "grml 0.8" -publisher 'Michael Prokop <mika@grml.org>' -l -r \
499 # -J -no-emul-boot -boot-load-size 4 -boot-info-table -c boot/isolinux/boot.cat \
500 # -b boot/isolinux/isolinux.bin -o /home/mika/grml/chroot/grml_0.8.iso .
501
502 # vim grml_cd.large/GRML/grml-version
503
504 # find . -type f -not -name md5sums -not -name boot.cat -exec md5sum {} \; > GRML/md5sums
505 ################################################################################
506 #
507 ## END OF FILE #################################################################