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