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