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