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