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