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