clean up /usr/src/linux-headers-... in /etc/grml/fai/grml/grml_cleanup_chroot
[grml-live.git] / etc / grml / fai / grml / grml_cleanup_chroot
1 #!/bin/bash
2 # Filename:      /etc/grml/fai/grml/grml_cleanup_chroot
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 licensed under the GPL v2.
7 # Latest change: Mon Sep 17 22:14:33 CEST 2007 [mika]
8 ################################################################################
9
10 # misc stuff
11   umask 022
12   [ -n "$NOCOLORS" ] || . /etc/grml_colors
13   . /etc/grml/lsb-functions || exit 6
14   [ -x /lib64 ] && GRML64=yes || GRML64=''
15
16 # detect kernel version, assume newest kernel version
17   KERNEL=$(ls -1 /lib/modules/ | sort -r -u | head -1)
18
19 if [ -n "$LATEX_CLEANUP" ] ; then
20    rm -rf /usr/share/doc/texlive-latex-recommended/latex/ \
21           /usr/share/doc/texlive-latex-base/latex/ \
22           /usr/share/doc/texlive-base-bin/pdftex/thanh/ \
23           /usr/share/doc/texlive-latex-base/latex/base/ \
24           /usr/share/doc/texlive-latex-base/latex/hyperref/ \
25           /usr/share/doc/texlive-latex-base/generic/babel/  \
26           /usr/share/doc/texlive-latex-recommended/latex/koma-script/ \
27           /usr/share/doc/texmf/pgf/pgfmanual.pdf.gz
28 fi
29
30 if [ -f /usr/share/fonts/X11/misc/artwiz-cursor.pcf.gz ] ; then
31   if dpkg-divert --list artwiz-cursor | grep -q /usr/share/fonts/X11/misc/cursor.pcf.gz ; then
32      eerror "Broken diversion of artwiz-cursor, see #341397, trying to fix it"
33      ( cd /usr/share/fonts/X11/misc/ &&
34        dpkg-divert --remove /usr/share/fonts/X11/misc/cursor.pcf.gz && mkfontdir )
35      eend 0
36   fi
37 fi
38
39 # start of main cleanup process...
40 if ! [ -r /boot/config-"$KERNEL" ] ; then
41   eerror "/boot/config-$KERNEL not available. \$KERNEL not set to correct value?"
42   eend 1
43   exit 9
44 fi
45
46 einfo "Preparing grml-system: $(cat /etc/grml_version)" ; eend 0
47
48 einfo "Setting up /etc/motd.tail"
49 echo "" > /etc/motd.tail ; eend 0
50
51 einfo "Setting up /etc/modules: "
52 cat>/etc/modules<<EOF
53 # /etc/modules: kernel modules to load at boot time.
54 #
55 # This file should contain the names of kernel modules that are
56 # to be loaded at boot time, one per line.  Comments begin with
57 # a \`#', and everything on the line after them are ignored.
58 EOF
59 echo "" >> /etc/modules
60 grep '^[a-z]' /etc/grml/modules >> /etc/modules
61 eend 0
62
63 einfo "Setting up /etc/inittab"
64 cp /etc/inittab.grml  /etc/inittab
65 eend $?
66
67 einfo "Cleaning up /usr/src/linux-headers-$KERNEL"
68 ( cd /usr/src && make clean && make symlinks )
69 eend $?
70
71 if [ -L /usr/include/linux ] ; then
72   einfo "/usr/include/linux is ok" ; eend 0
73 elif [ -n "$GRML64" ] ; then
74     if [ -d /usr/src/linux-headers-"$KERNEL" ] ; then
75        einfo "grml64 detected, assuming /usr/src/linux-headers-$KERNEL is ok." ; eend 0
76     else
77        eerror "/usr/src/linux-headers-$KERNEL not found, exiting." ; eend 1
78        exit 10
79     fi
80 elif [ -d /usr/src/linux-headers-"$KERNEL" ] ; then
81      eerror "/usr/include/linux is NOT a symlink to /usr/src/linux/include/linux/ - error"
82      eend 1
83      exit 10
84 else
85      ewarn "linux-headers-$KERNEL not installed." ; eend 0
86 fi
87
88 einfo "Creating ~/.zshrc"
89   touch ~/.zshrc
90 eend $?
91
92 if [ -r /usr/share/lintian/overrides/ion3 ] ; then
93   einfo "Stripping ion3-stuff"
94   cat /usr/share/lintian/overrides/ion3 | sed 's#.*./usr#/usr#' | \
95   sed 's/.comment//' | xargs strip --strip-unneeded ; eend $?
96 else
97   ewarn "ion3 not installed" ; eend 0
98 fi
99
100 if [ -d /usr/lib/valgrind/x86-linux/ ] ; then
101   einfo "Stripping /usr/lib/valgrind/x86-linux/"
102   strip  --strip-unneeded  /usr/lib/valgrind/x86-linux/*
103   eend $?
104 else
105   ewarn "valgrind not installed" ; eend 0
106 fi
107
108 if [ -x /usr/sbin/localepurge ] ; then
109   einfo "Running localepurge"
110   localepurge ; eend $?
111 else
112   ewarn "localepurg not available" ; eend 0
113 fi
114
115 if [ -d /var/lib/backuppc/pc/localhost/ ] ; then
116   einfo "Cleaning /var/lib/backuppc/pc/localhost/"
117   rm -rf /var/lib/backuppc/pc/localhost/* ; eend $?
118 else
119   ewarn "backuppc not installed" ; eend 0
120 fi
121
122 # find /var -name \*.pid
123 einfo "Removing pid-files"
124   find /var/run -name \*.pid -exec rm {} \;
125 eend $?
126
127 einfo "Unmounting proc, /dev/pts, /dev/capi"
128   umount /proc     2>/dev/null
129   umount /dev/pts  2>/dev/null
130   umount /dev/capi 2>/dev/null
131   umount -a 2>/dev/null
132 eend $?
133
134 einfo "Removing /var/lib/apt/lists/*-stuff, dpkg-status-old and pkgcache.bin"
135   rm -f /var/lib/apt/lists/*Packages
136   rm -f /var/lib/apt/lists/*Release
137   rm -f /var/lib/apt/lists/*Sources
138   rm -f /var/lib/apt/lists/*.gpg
139   rm -f /var/cache/apt-show-versions/*
140   rm -f /var/cache/debconf/templates.dat-old
141   rm -f /var/cache/apt/*.bin
142   apt-get check 2>/dev/null
143   dpkg --clear-avail
144   apt-cache gencaches 2>/dev/null
145   rm -f /var/lib/dpkg/status-old
146   rm -f /var/lib/dpkg/available-old
147 eend $?
148
149 if [ -x /usr/bin/grep-dctrl ] ; then
150    einfo "Cleaning up /var/lib/dpkg/status"
151    grep-dctrl -v -F Status "purge ok not-installed" /var/lib/dpkg/status > status.new && \
152    cp status.new /var/lib/dpkg/status && chmod 644 /var/lib/dpkg/status  && \
153    chown root:root /var/lib/dpkg/status && rm status.new && eend 0 || eend 1
154 else
155    ewarn "Warning: /usr/bin/grep-dctrl not available."
156 fi
157
158 einfo "Copying original runlevel.conf to /etc/runlevel.conf"
159 cp /etc/runlevel.conf.livecd /etc/runlevel.conf
160 eend $?
161
162 einfo "Removing x-server-conffiles"
163   rm -f /etc/X11/xorg.conf /etc/X11/XF86Config-4 \
164         /etc/X11/XF86Config /etc/XF86Config /etc/XF86Config-4 
165 eend $?
166
167 einfo "Removing ssh-keys"
168   rm -f /etc/ssh/*key*
169 eend $?
170
171 # remove only "temporary" or saved files in the given directories
172 nuke(){
173   for i in `find "$@" -name \*.gz -o -name \*.bz2 -o -name \*.0 2>/dev/null`; do
174     rm -f "$i"
175   done
176 }
177
178 # set all files in the given directories to a length of zero
179 zero(){
180   for i in `find "$@" -type f -size +0 -not -name \*.ini 2>/dev/null`; do
181     :> "$i"
182   done
183 }
184
185 #for i in `find /usr/*/man -name \*.\[0-9ln\]` ; do
186 #  [ -f "$i".gz -o -f "$i".bz2 ] && rm -f "$i"
187 #done
188
189 if [ -d /var/spool/squid/ ] ; then
190   einfo "Cleaning /var/spool/squid/0*"
191   rm -rf /var/spool/squid/0*
192   eend $?
193 else
194   ewarn "update-ca-certificates not installed" ; eend 0
195 fi
196
197 # delete old dowloaded packages
198 einfo "Running apt-get clean"
199   apt-get clean
200 eend $?
201
202 if [ -x /usr/bin/nvi ] ; then
203   if ! [ -d /var/tmp/vi.recover ] ; then
204     einfo "Creating nvi /var/tmp directory"
205     mkdir -p /var/tmp/vi.recover
206     chown root:root /var/tmp/vi.recover
207     chmod 1777 /var/tmp/vi.recover
208     eend $?
209   fi
210 else
211   ewarn "nvi not installed" ; eend 0
212 fi
213
214 if [ -x /sbin/depmod ] ; then
215   if [ -r /lib/modules/$KERNEL ] ; then
216     einfo "Updating modules.dep for kernel $KERNEL"
217     depmod -ae -F /boot/System.map-"$KERNEL" "$KERNEL"
218     eend $?
219   fi
220 else
221   ewarn "depmod not installed" ; eend 0
222 fi
223
224 if [ -n "$NOONLINE" ] ; then
225    einfo "\$NOONLINE is set, skipping online tasks" ; eend 0
226 else
227
228 if [ -x /usr/bin/freshclam ] ; then
229   einfo "Updating clamav database via running freshclam"
230   /usr/bin/freshclam ; eend $?
231 else
232   ewarn "freshclam not installed" ; eend 0
233 fi
234
235 if [ -x /usr/bin/update-pciids ] ; then
236   einfo "Updating PCI- and USB-ids"
237   update-pciids
238   update-usbids
239   eend $?
240 else
241   ewarn "update-pciids/update-usbids not installed" ; eend 0
242 fi
243
244 if [ -x /usr/sbin/nessus-update-plugins ] ; then
245    einfo "Updating nessus-plugins"
246    nessus-update-plugins
247    eend $?
248 else
249   ewarn "nessus-update-plugins not installed" ; eend 0
250 fi
251
252 fi # end of $NOONLINE-check
253
254 einfo "Cleaning and removing some misc files and directories"
255   rm -f  /etc/sysconfig/* /etc/motd.dpkg-new /etc/auto.master.distrib.dpkg-new    \
256          /etc/samba/*.SID /etc/samba/*.tdb /dev/mouse* /dev/cdrom* /dev/cdwriter* \
257          /var/run/*/* /var/run/* /var/log/ksymoops/* /var/lock/*/* /var/lock/*    \
258          /var/state/*/* /var/state/* /var/log/nessus/*                            \
259          /halt /reboot /ash.static /etc/dhcpc/*.info /etc/dhcpc/resolv*           \
260          /etc/*passwd- /etc/*shadow- /etc/*group- /var/spool/postfix/maildrop/*   \
261          /etc/lvm/.cache 2>/dev/null
262   rm -rf /tmp/* /var/tmp/* /var/tmp/.* /var/backups/* /.ssh /root/.ssh /home/*/.ssh \
263          /home/grml/* /home/grml/.??* /var/lib/texmf/ls-R  /var/spool/texmf/ls-R    \
264          /var/lib/mysql /var/log/lilo_log.* /core* 2>/dev/null
265   nuke   /var/log       /var/cache
266   zero   /var/local     /var/log /var/spool \
267          /var/lib/games /var/cache/man      \
268          /var/lib/nfs   /var/lib/xkb        \
269          /var/mail/grml /var/account/pacct
270   mkdir -p /etc/sysconfig/network-scripts /etc/sysconfig/provider
271   # Recreate empty utmp and wtmp
272   :>/var/run/utmp
273   :>/var/run/wtmp
274 eend $?
275
276 if [ -x /usr/sbin/update-ca-certificates ] ; then
277   einfo "Updating ca-certificates"
278   update-ca-certificates 1>/dev/null
279   eend $?
280 else
281   ewarn "update-ca-certificates not installed" ; eend 0
282 fi
283
284 # some stuff does not have to run every single time...
285 if [ -n "$FAST" ] ; then
286    einfo "\$FAST is set, skipping some tasks" ; eend 0
287 else
288
289 # regenerate module dependencies and ls.so.cache
290 if [ -x /sbin/ldconfig ] ; then
291   einfo "Updating ld.so.cache"
292   ldconfig
293   eend $?
294 else
295   ewarn "ldconfig not installed" ; eend 0
296 fi
297
298 if [ -x /usr/bin/update-menus ] ; then
299   einfo "Updating windowmanager menus"
300   update-menus -v
301   eend $?
302 else
303   ewarn "update-menus not installed" ; eend 0
304 fi
305
306 if [ -x /usr/bin/mktexlsr ] ; then
307   einfo "Updating texhash"
308   mktexlsr
309   eend $?
310 else
311   ewarn "mktexlsr not installed" ; eend 0
312 fi
313
314 if [ -x /usr/bin/mandb ] ; then
315   einfo "Updating mandb"
316   mandb -c
317   man doesnotexist >/dev/null 2>&1
318   eend $?
319 else
320   ewarn "mandb not installed" ; eend 0
321 fi
322
323 if [ -n "$REMOVE_DOCS" ] ; then
324   einfo "Cleaning documentation directories..."
325   if [ -d /usr/share/doc/grml-docs ] ; then
326      mv /usr/share/doc/grml-docs /tmp/
327   fi
328
329   rm -rf /usr/share/doc
330   mkdir /usr/share/doc
331
332   if [ -d /tmp/grml-docs ] ; then
333      mv /tmp/grml-docs /usr/share/doc/grml-docs
334   fi
335
336   rm -rf /usr/share/gtk-doc/
337   rm -rf /usr/share/man/
338   rm -rf /usr/man
339   rm -rf /usr/share/info
340   eend $?
341
342   einfo "Creating /usr/share/info/..."
343   mkdir -p /usr/share/info/
344   eend $?
345 fi
346
347 if [ -d /var/lib/clamav/ ] ; then
348   einfo "Cleaning /var/lib/clamav/"
349   rm -f /var/lib/clamav/clamav-* ; eend $?
350   if [ -f /var/lib/clamav/daily.cvd ] ; then
351      ( mkdir -p /usr/share/doc/clamav-freshclam/examples/ && \
352        cd /usr/share/doc/clamav-freshclam/examples/ && \
353        ln -sf /var/lib/clamav/daily.cvd . && \
354        ln -sf /var/lib/clamav/main.cvd  . )
355   fi
356 else
357   ewarn "clamav[-freshclam] not installed" ; eend 0
358 fi
359
360 if ! [ -r /etc/ld.so.nohwcap ] ; then
361    einfo "Creating /etc/ld.so.nohwcap"
362    touch /etc/ld.so.nohwcap
363 fi
364
365 # installation of resolvconf in chroot *with* /proc
366 # is different from an installation without /proc,
367 # so make sure it is OK in any case; don't use /dev/shm
368 if [ -d /etc/resolvconf ] ; then
369    rm -rf /etc/resolvconf/run
370    mkdir /etc/resolvconf/run
371    touch /etc/resolvconf/run/enable-updates
372    mkdir /etc/resolvconf/run/interface
373    cat > /etc/resolvconf/run/resolv.conf << EOF
374 # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
375 #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
376 EOF
377 fi
378
379 if [ -x /usr/bin/updatedb ] ; then
380   einfo "Updating locate-database"
381   mount -t proc none /proc
382   updatedb --prunepaths='/tmp /usr/tmp /var/tmp /grml /root /proc /sys'
383   umount /proc
384   eend $?
385 else
386   ewarn "updatedb not installed" ; eend 0
387 fi
388
389 fi # end of $FAST-check
390
391 einfo "Unmounting all filesystems"
392   umount -a
393 eend $?
394
395 ################################################################################
396 # MISC STUFF
397 # ~~~~~~~~~~
398 # list dangling symlinks: ls **/*(-@)
399 #
400 # Security-audit:
401 # find / \! -type l -perm -2000 -ls > audit/sgid-files 
402 # find / \! -type l -perm -0002 -ls > audit/world-writeable-files
403 # find / \! -type l -perm -4000 -ls > audit/suid-files
404 # find / ( -perm 4000 -o -perm -2000 \) -exec ls -ld {} \;
405 # find / -perm -0777 -type d -ls > audit/writeable-dirs
406 # find / -size +3000k > audit/big_files.new
407 # find /all_filesystems -name ".. " -print -xdev | cat -vet
408 # find / -name ".*" -print -xdev | cat -v > audit/hidden-files
409 # find / -name ".. " -print -xdev | cat -vet > audit/unusal-file-names
410 #
411 # list non-stripped files:
412 # file /usr/sbin/* | grep -v ', stripped' | grep -v 'perl' | grep -v 'Bourne' | grep -v 'python' | grep -v 'symbolic link'
413 #
414 # Is software by grml-repos up2date?
415 # grep-dctrl --field Maintainer 'Michael Prokop' /var/lib/apt/lists/grml.org_repos_._* | grep Package | awk '{print $2}' | xargs echo 
416 #
417 # runlevel.conf:
418 # for file in /etc/init.d/* ; do grep $file -q /etc/runlevel.conf.hdinstall || echo $file not found ; done
419 ################################################################################
420
421 ## END OF FILE #################################################################