GRMLBASE/98-clean-chroot: get rid of all lvm backup files
[grml-live.git] / etc / grml / fai / config / scripts / GRMLBASE / 98-clean-chroot
1 #!/bin/bash
2 # Filename:      ${GRML_FAI_CONFIG}/config/scripts/GRMLBASE/98-clean-chroot
3 # Purpose:       clean up chroot system
4 # Authors:       grml-team (grml.org), (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 ################################################################################
8
9 set -u
10 set -e
11
12 if ! ls $target/boot/config-* &>/dev/null ; then
13   echo "No kernel config files (/boot/config-*) found. No kernel-image package installed?" >&2
14   exit 1
15 fi
16
17 echo "Creating ~/.zshrc"
18 touch $target/root/.zshrc
19
20 $ROOTCMD rm -f /etc/apt/apt.conf.d/90grml-apt-proxy.conf
21
22 if [ -x $target/usr/sbin/localepurge ] ; then
23   echo "Running localepurge"
24   $ROOTCMD localepurge
25 else
26   echo "Warning: localepurge not installed"
27 fi
28
29 # revert dpkg-divert of hooks/instsoft.GRMLBASE, which is
30 # used to work around /etc/kernel/postinst.d/zz-update-grub failing
31 # inside openvz environment, see #597084
32 if $ROOTCMD dpkg-divert --list | grep -q '/usr/sbin/update-grub' ; then
33   echo "Undoing dpkg-divert of update-grub executable"
34   $ROOTCMD rm -f /usr/sbin/update-grub
35   $ROOTCMD dpkg-divert --rename --remove /usr/sbin/update-grub
36 fi
37
38 # revert udev workaround of hooks/updatebase.GRMLBASE
39 if grep -q 'updatebase.GRMLBASE' ${target}/etc/udev/kernel-upgrade 2>/dev/null ; then
40   echo "Removing /etc/udev/kernel-upgrade created by updatebase.GRMLBASE"
41   $ROOTCMD rm -f /etc/udev/kernel-upgrade
42 fi
43
44 echo "Updating package list"
45 $ROOTCMD apt-get update
46
47 echo "Cleaning apt places"
48 $ROOTCMD apt-get check 2>/dev/null
49 $ROOTCMD dpkg --clear-avail
50 $ROOTCMD apt-cache gencaches 2>/dev/null
51 $ROOTCMD apt-get clean
52
53 rm -f $target/var/lib/dpkg/status-old $target/var/lib/dpkg/available-old
54
55 if ! [ -x $target/usr/bin/grep-dctrl ] ; then
56   echo "Warning: grep-dctrl not installed"
57 else
58   echo "Cleaning up /var/lib/dpkg/status"
59   if $ROOTCMD grep-dctrl -v -F Status "purge ok not-installed" \
60     /var/lib/dpkg/status > $target/var/lib/dpkg/status.new ; then
61     mv $target/var/lib/dpkg/status.new $target/var/lib/dpkg/status
62     chmod 644 $target/var/lib/dpkg/status
63     chown root:root $target/var/lib/dpkg/status
64   fi
65 fi
66
67 echo "Removing host ssh-keys"
68 rm -f $target/etc/ssh/*key*
69
70 echo "Removing dbus machine-id"
71 rm -f $target/var/lib/dbus/machine-id
72
73 if [ -d $target/var/spool/squid/ ] ; then
74   echo "Cleaning /var/spool/squid/0*"
75   rm -rf $target/var/spool/squid/0*
76 fi
77
78 echo "Cleaning and removing some misc files and directories"
79 find $target/etc -type f -name *.pre_fcopy -delete
80 find $target/etc -type l -name *.pre_fcopy -delete
81 rm -rf --one-file-system $target/etc/sysconfig/* \
82        $target/etc/motd.dpkg-* $target/etc/auto.master.*dpkg* \
83        $target/etc/samba/*.SID $target/etc/samba/*.tdb \
84        $target/var/log/ksymoops/* \
85        $target/var/state/* $target/var/log/nessus/* \
86        $target/halt $target/reboot $target/ash.static \
87        $target/etc/dhcpc/*.info $target/etc/dhcpc/resolv* \
88        $target/etc/*passwd- $target/etc/*shadow- \
89        $target/etc/*group- $target/var/spool/postfix/maildrop/* \
90        $target/etc/*.old $target/etc/*.original \
91        $target/etc/lvm/.cache $target/etc/lvm/cache/.cache \
92        $target/etc/lvm/backup/* $target/tmp/* \
93        $target/var/tmp/* $target/var/backups/* \
94        $target/var/lib/mysql $target/var/log/lilo_log.* $target/core*
95
96 # remove only "temporary" or saved files in the given directories
97 nuke(){
98   for i in $(find "$@" -name \*.gz -o -name \*.bz2 -o -name \*.0 2>/dev/null); do
99     rm -f --one-file-system "$i"
100   done
101 }
102
103 # set all files in the given directories to a length of zero
104 zero(){
105   for i in $(find "$@" -type f -size +0 -not -name \*.ini -not -path '*/fai/*' -not -name install_packages.list 2>/dev/null); do
106     :> "$i"
107   done
108 }
109
110 echo "Cleaning log and cache directories"
111 nuke ${target}/var/log       ${target}/var/cache
112 zero ${target}/var/account/pacct \
113      ${target}/var/cache/man \
114      ${target}/var/lib/games \
115      ${target}/var/lib/nfs   \
116      ${target}/var/lib/xkb   \
117      ${target}/var/local     \
118      ${target}/var/log       \
119      ${target}/var/mail/grml
120
121 # on /run we don't have to create it
122 if [ -d ${target}/var/run ] ; then
123   echo "Recreate empty utmp and wtmp"
124   :>${target}/var/run/utmp
125   :>${target}/var/run/wtmp
126 fi
127
128 if ! [ -x $target/usr/sbin/update-ca-certificates ] ; then
129   echo "Warning: update-ca-certificates not installed"
130 else
131   echo "Updating ca-certificates"
132   $ROOTCMD update-ca-certificates
133 fi
134
135 # regenerate ls.so.cache
136 if ! [ -x $target/sbin/ldconfig ] ; then
137   echo "Warning: ldconfig not installed"
138 else
139   echo "Updating ld.so.cache"
140   $ROOTCMD ldconfig
141 fi
142
143 if ! [ -x $target/usr/bin/update-menus ] ; then
144   echo "Warning: update-menus not installed"
145 else
146   echo "Updating windowmanager menus"
147   $ROOTCMD update-menus -v
148 fi
149
150 if ! [ -x $target/usr/bin/mandb ] ; then
151   echo "Warning: mandb not installed"
152 else
153   echo "Updating mandb"
154   $ROOTCMD mandb -c
155   $ROOTCMD man doesnotexist >/dev/null 2>&1 || true
156 fi
157
158 if ! [ -d $target/var/lib/clamav/ ] ; then
159   echo "Warning: clamav[-freshclam] not installed"
160 else
161   echo "Cleaning /var/lib/clamav/"
162   $ROOTCMD rm -rf /var/lib/clamav/clamav-*
163
164   echo "Setting up daily.cvd and main.cvd symlinks"
165   if [ -f $target/var/lib/clamav/daily.cvd ] ; then
166     mkdir -p $target/usr/share/doc/clamav-freshclam/examples/
167     ln -sf /var/lib/clamav/daily.cvd $target/usr/share/doc/clamav-freshclam/examples/
168     ln -sf /var/lib/clamav/main.cvd  $target/usr/share/doc/clamav-freshclam/examples/
169   fi
170 fi
171
172 if ! [ -r $target/etc/ld.so.nohwcap ] ; then
173    echo "Creating /etc/ld.so.nohwcap"
174    touch $target/etc/ld.so.nohwcap
175 fi
176
177 # installation of resolvconf in chroot *with* /proc
178 # is different from an installation without /proc,
179 # so make sure it is OK in any case
180 if ! [ -d $target/etc/resolvconf ] ; then
181   echo "Warning: resolvconf not installed"
182 else
183   echo "Setting up resolvconf"
184   if [ ! -L $target/etc/resolvconf/run ] ; then # resolvconf without symlink?!
185     RESOLV_CONF=/etc/resolvconf/run
186     rm -rf   ${target}/${RESOLV_CONF}
187     mkdir -p ${target}/${RESOLV_CONF}
188
189     touch ${target}/${RESOLV_CONF}/enable-updates
190     mkdir ${target}/${RESOLV_CONF}/interface
191
192     cat > ${target}/${RESOLV_CONF}/resolv.conf << EOF
193 # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
194 #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
195 EOF
196   fi
197   rm -f ${target}/etc/resolv.conf
198   $ROOTCMD ln -s /etc/resolvconf/run/resolv.conf /etc/resolv.conf
199 fi
200
201 if ! $ROOTCMD test -x /usr/bin/updatedb ; then
202   echo "Warning: updatedb not installed"
203 else
204   echo "Updating locate-database"
205   $ROOTCMD updatedb --prunepaths='/tmp /usr/tmp /var/tmp /grml /root /proc /sys'
206 fi
207
208 ## END OF FILE #################################################################
209 # vim:ft=sh expandtab ai tw=80 tabstop=4 shiftwidth=2