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