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