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