62e5be100ea26e3dd4c2125cfd0be02f6a7a942a
[grml-live.git] / rewrite / grml.prepare
1 #!/bin/bash
2 # Filename:      grml.clean
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 copyright by Michael Prokop
7 # Latest change: Sun Apr 22 14:15:03 CEST 2007 [mika]
8 ################################################################################
9
10 # misc stuff
11   umask 022
12   . /etc/grml_colors        || exit 5
13   . /etc/grml/lsb-functions || exit 6
14
15 # variables
16   grep -q small /etc/grml_version && GRML_SMALL=yes  || GRML_SMALL=''
17   if [ -n "$GRML_SMALL" ] ; then
18      echo -n 'Are you sure you want to run GRML_SMALL stuff? [y|N] '
19      read a
20      if [ "$a" == 'y' ] ; then
21         echo 'Continuing as requested.'
22      else
23         echo 'Exiting as requested.'
24         unset GRML_SMALL
25         exit 1
26      fi
27   fi
28
29 # detect kernel version, assume newest kernel version
30   KERNEL=$(ls -1 /lib/modules/ | sort -r -u | head -1)
31
32 if [ -f /usr/share/fonts/X11/misc/artwiz-cursor.pcf.gz ] ; then
33   if dpkg-divert --list artwiz-cursor | grep -q /usr/share/fonts/X11/misc/cursor.pcf.gz ; then
34      eerror "Broken diversion of artwiz-cursor, see #341397"
35      exit 1
36   fi
37 fi
38
39 #if [ -r /usr/share/fonts/X11/misc/cursor.pcf.gz ] ; then
40 #  einfo "Fixing the broken artwiz-cursor"
41 #  ( cd /usr/share/fonts/X11/misc/ && \
42 #    dpkg-divert --remove /usr/share/fonts/X11/misc/cursor.pcf.gz && mkfontdir )
43 #  eend $?
44 #fi
45
46 # start of main cleanup process...
47 if ! [ -r /boot/config-"$KERNEL" ] ; then
48   eerror "/boot/config-$KERNEL not available. \$KERNEL not set to correct value?"
49   eend 1
50   exit 9
51 fi
52
53 einfo "Preparing grml-system: `cat /etc/grml_version`." ; eend 0
54
55 einfo "Setting up /etc/motd.tail"
56 echo "" > /etc/motd.tail ; eend 0
57
58 einfo "Do not forget to run the final check:"
59  eindent
60   einfo 'adjust grml-version: vim -X /etc/grml_version'
61   einfo 'is runlevel.conf ok: vimdiff -X /etc/runlevel.conf /etc/runlevel.conf.livecd'
62   einfo 'find old files:      find /etc -name \*dpkg\*'
63   einfo "delete conffiles:    dpkg --purge \`dpkg --list | grep '^rc' | awk '{print \$2}'\`"
64   einfo 'dangling symlinks:   ls **/*(-@)'
65  eoutdent
66
67 einfo "Setting up /etc/modules: "
68 cat>/etc/modules<<EOF
69 # /etc/modules: kernel modules to load at boot time.
70 #
71 # This file should contain the names of kernel modules that are
72 # to be loaded at boot time, one per line.  Comments begin with
73 # a \`#', and everything on the line after them are ignored.
74 EOF
75 echo "" >> /etc/modules
76 grep '^[a-z]' /etc/grml/modules >> /etc/modules
77 eend 0
78
79 if [ -z "$GRML_SMALL" ] ; then
80   if [ -L /usr/include/linux ] ; then
81     einfo "/usr/include/linux is ok" ; eend 0
82   else
83     if [ -d /usr/src/linux-headers-"$KERNEL" ] ; then
84        eerror "/usr/include/linux is NOT a symlink to /usr/src/linux/include/linux/ - error"
85        eend 1
86        exit 10
87     else
88        ewarn "linux-headers-$KERNEL not installed." ; eend 0
89     fi
90   fi
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 if [ -d /var/lib/clamav/ ] ; then
128   einfo "Cleaning /var/lib/clamav/"
129   rm -f /var/lib/clamav/clamav-* ; eend $?
130   if [ -f /var/lib/clamav/daily.cvd ] ; then
131      ( cd /usr/share/doc/clamav-freshclam/examples/ && \
132      ln -sf /var/lib/clamav/daily.cvd . && \
133      ln -sf /var/lib/clamav/main.cvd  . )
134   fi
135 else
136   ewarn "clamav[-freshclam] not installed" ; eend 0
137 fi
138
139 # find /var -name \*.pid
140 einfo "Removing pid-files"
141   find /var/run -name \*.pid -exec rm {} \;
142 eend $?
143
144 einfo "Unmounting proc, /dev/pts, /dev/capi"
145   umount /proc    2>/dev/null
146   umount /dev/pts 2>/dev/null
147   umount /dev/capi 2>/dev/null
148   umount -a
149 eend $?
150
151 einfo "Removing initrd-images"
152   rm -f /boot/initrd.img-*
153 eend $?
154
155 einfo "Removing /var/lib/apt/lists/*-stuff, dpkg-status-old and pkgcache.bin"
156   # for i in $(find /var/lib/apt/lists -type f \( -name \*Packages -o -name \*Sources \) 2>/dev/null); do  :>"$i"; done
157   # for i in /var/lib/apt/lists/*_{Release,Packages,Sources}; do :>$i; touch -t 198103190000.00 $i; done
158   # sync-available 2>/dev/null
159   # apt-get check
160   rm -f /var/lib/apt/lists/*Packages
161   rm -f /var/lib/apt/lists/*Release
162   rm -f /var/lib/apt/lists/*Sources
163   rm -f /var/lib/apt/lists/*.gpg
164   # :> /var/cache/apt/pkgcache.bin
165   # :> /var/cache/apt/srcpkgcache.bin
166   rm -f /var/cache/debconf/templates.dat-old
167   rm -f /var/cache/apt/*.bin
168   apt-get check 2>/dev/null
169   dpkg --clear-avail
170   apt-cache gencaches 2>/dev/null
171   rm -f /var/lib/dpkg/status-old
172   rm -f /var/lib/dpkg/available-old
173 eend $?
174
175 if [ -x /usr/bin/grep-dctrl ] ; then
176    einfo "Cleaning up /var/lib/dpkg/status"
177    grep-dctrl -v -F Status "purge ok not-installed" /var/lib/dpkg/status > status.new && \
178    cp status.new /var/lib/dpkg/status && chmod 644 /var/lib/dpkg/status  && \
179    chown root:root /var/lib/dpkg/status && rm status.new && eend 0 || eend 1
180 else
181    ewarn "Warning: /usr/bin/grep-dctrl not available."
182 fi
183
184 einfo "Copying original runlevel.conf to /etc/runlevel.conf"
185   if ! [ -z $GRML_SMALL ] ; then
186     cp /etc/runlevel.conf.livecd.small /etc/runlevel.conf
187   else
188     cp /etc/runlevel.conf.livecd /etc/runlevel.conf
189   fi
190 eend $?
191
192 einfo "Removing x-server-conffiles"
193   rm -f /etc/X11/xorg.conf /etc/X11/XF86Config-4 \
194         /etc/X11/XF86Config /etc/XF86Config /etc/XF86Config-4 
195 eend $?
196
197 einfo "Removing ssh-keys"
198   rm -f /etc/ssh/*key*
199 eend $?
200
201 # remove only "temporary" or saved files in the given directories
202 nuke(){
203   for i in `find "$@" -name \*.gz -o -name \*.bz2 -o -name \*.0 2>/dev/null`; do
204     rm -f "$i"
205   done
206 }
207
208 # set all files in the given directories to a length of zero
209 zero(){
210   for i in `find "$@" -type f -size +0 -not -name \*.ini 2>/dev/null`; do
211     :> "$i"
212   done
213 }
214
215 #rmdir /mnt/cdrom?* /mnt/hd?* 2>/dev/null
216
217 #for i in `find /usr/*/man -name \*.\[0-9ln\]` ; do
218 #  [ -f "$i".gz -o -f "$i".bz2 ] && rm -f "$i"
219 #done
220
221 if [ -d /var/spool/squid/ ] ; then
222   einfo "Cleaning /var/spool/squid/0*"
223   rm -rf /var/spool/squid/0*
224   eend $?
225 else
226   ewarn "update-ca-certificates not installed" ; eend 0
227 fi
228
229 # delete old dowloaded packages
230 einfo "Running apt-get clean"
231   apt-get clean
232 eend $?
233
234 # remove files from packages which have been removed
235 # COLUMNS=200 dpkg --purge `dpkg -l | grep '^rc' | awk '{ print $2 }'` 
236
237 if [ -x /usr/bin/nvi ] ; then
238   if ! [ -d /var/tmp/vi.recover ] ; then
239     einfo "Creating nvi /var/tmp directory"
240     mkdir -p /var/tmp/vi.recover
241     chown root:root /var/tmp/vi.recover
242     chmod 1777 /var/tmp/vi.recover
243     eend $?
244   fi
245 else
246   ewarn "nvi not installed" ; eend 0
247 fi
248
249 if [ -x /sbin/depmod ] ; then
250   if [ -r /lib/modules/$KERNEL ] ; then
251     einfo "Updating modules.dep for kernel $KERNEL"
252     depmod -a $KERNEL
253     eend $?
254   fi
255 else
256   ewarn "depmod not installed" ; eend 0
257 fi
258
259 if [ -n "$NOONLINE" ] ; then
260    einfo "\$NOONLINE is set, skipping online tasks" ; eend 0
261 else
262
263 if [ -x /usr/bin/freshclam ] ; then
264   einfo "Updating clamav database via running freshclam"
265   /usr/bin/freshclam ; eend $?
266 else
267   ewarn "freshclam not installed" ; eend 0
268 fi
269
270 if [ -x /usr/bin/update-pciids ] ; then
271   einfo "Updating PCI- and USB-ids"
272   update-pciids
273   update-usbids
274 #  ls -la /usr/share/misc/*.ids* 
275 #  ls -la /var/lib/usbutils/usb*
276   eend $?
277 else
278   ewarn "update-pciids/update-usbids not installed" ; eend 0
279 fi
280
281 if [ -x /usr/sbin/nessus-update-plugins ] ; then
282    einfo "Updating nessus-plugins"
283    nessus-update-plugins
284    eend $?
285 else
286   ewarn "nessus-update-plugins not installed" ; eend 0
287 fi
288
289 fi # end of $NOONLINE-check
290
291 einfo "Cleaning and removing some misc files and directories"
292   rm -f  /etc/sysconfig/* /etc/motd.dpkg-new /etc/auto.master.distrib.dpkg-new    \
293          /etc/samba/*.SID /etc/samba/*.tdb /dev/mouse* /dev/cdrom* /dev/cdwriter* \
294          /var/run/*/* /var/run/* /var/log/ksymoops/* /var/lock/*/* /var/lock/*    \
295          /var/state/*/* /var/state/* /var/log/nessus/*                            \
296          /halt /reboot /ash.static /etc/dhcpc/*.info /etc/dhcpc/resolv*           \
297          /etc/resolv.conf /etc/*passwd- /etc/*shadow- /etc/*group-                \
298          /var/spool/postfix/maildrop/*                                            \
299          /etc/lvm/.cache 2>/dev/null
300   rm -rf /tmp/* /var/tmp/* /var/tmp/.* /var/backups/* /.ssh /root/.ssh /home/*/.ssh \
301          /home/grml/* /home/grml/.??* /var/lib/texmf/ls-R  /var/spool/texmf/ls-R    \
302          /var/lib/mysql 2>/dev/null
303   nuke   /var/log       /var/cache
304   zero   /var/local     /var/log /var/spool \
305          /var/lib/games /var/cache/man      \
306          /var/lib/nfs   /var/lib/xkb        \
307          /var/mail/grml /var/account/pacct
308   mkdir -p /etc/sysconfig/network-scripts /etc/sysconfig/provider
309   # Recreate empty utmp and wtmp
310   :>/var/run/utmp
311   :>/var/run/wtmp
312 eend $?
313
314 einfo "Setting up /etc/resolv.conf"
315   if [ -d /etc/dhcpc ] ; then
316     cat >/etc/dhcpc/resolv.conf <<EOT
317 # insert nameservers here
318 # nameserver 127.0.0.1
319 EOT
320     chmod 644 /etc/dhcpc/resolv.conf
321   else
322     cat >/etc/resolv.conf <<EOT
323 # insert nameservers here
324 # nameserver 127.0.0.1
325 EOT
326     chmod 644 /etc/resolv.conf
327   fi
328
329    # is resolvconf available?
330    if ! [ -r /etc/resolv.conf ] ; then
331       if [ -r /etc/resolvconf/run/resolv.conf ] ; then
332          ln -sf /etc/resolvconf/run/resolv.conf /etc/
333       else
334          ln -s /etc/dhcpc/resolv.conf /etc/
335       fi
336    fi
337 eend $?
338
339 if [ -x /usr/sbin/update-ca-certificates ] ; then
340   einfo "Updating ca-certificates"
341   update-ca-certificates 1>/dev/null
342   eend $?
343 else
344   ewarn "update-ca-certificates not installed" ; eend 0
345 fi
346
347 # some stuff does not have to run every single time...
348 if [ -n "$FAST" ] ; then
349    einfo "\$FAST is set, skipping some tasks" ; eend 0
350 else
351
352 # regenerate module dependencies and ls.so.cache
353 if [ -x /sbin/ldconfig ] ; then
354   einfo "Updating ld.so.cache"
355   ldconfig
356   eend $?
357 else
358   ewarn "ldconfig not installed" ; eend 0
359 fi
360
361 #if [ -r /lib/modules/$KERNEL/ ] ; then
362 #  einfo "Searching for modules not compiled against $KERNEL"
363 #  ( cd /lib/modules/$KERNEL/ && modinfo **/*.ko | grep vermagic | grep -v $KERNEL )
364 #  eend 0
365 #else
366 #  eerror "/lib/modules/$KERNEL/ not found" ; eend 1
367 #fi
368
369 if [ -x /usr/bin/update-menus ] ; then
370   einfo "Updating windowmanager menus"
371   update-menus -v
372   eend $?
373 else
374   ewarn "update-menus not installed" ; eend 0
375 fi
376
377 if [ -x /usr/bin/mktexlsr ] ; then
378   einfo "Updating texhash"
379   mktexlsr
380   eend $?
381 else
382   ewarn "mktexlsr not installed" ; eend 0
383 fi
384
385 if [ -x /usr/bin/mandb ] ; then
386   einfo "Updating mandb"
387   mandb -c
388   man doesnotexist >/dev/null 2>&1
389   eend $?
390 else
391   ewarn "mandb not installed" ; eend 0
392 fi
393
394 if [ -n "$GRML_SMALL" ] ; then
395   einfo "Cleaning up /etc/skel..."
396   rm -rf /etc/skel/.fluxbox 
397   rm -rf /etc/skel/.gkrellm2 
398   rm -rf /etc/skel/.idesktop 
399   rm -rf /etc/skel/.pekwm
400   eend $?
401
402   einfo "Cleaning documentation directories..."
403   mv /usr/share/doc/grml-docs /tmp/
404   rm -rf /usr/share/doc
405   mkdir /usr/share/doc
406   mv /tmp/grml-docs /usr/share/doc/grml-docs
407   rm -rf /usr/share/gtk-doc/
408   rm -rf /usr/share/man/
409   rm -rf /usr/man
410   rm -rf /usr/share/info
411   eend $?
412
413   einfo "Creating /usr/share/info/..."
414   mkdir -p /usr/share/info/
415   eend $?
416 fi
417
418 if ! [ -r /etc/ld.so.nohwcap ] ; then
419    einfo "Creating /etc/ld.so.nohwcap"
420    touch /etc/ld.so.nohwcap
421 fi
422
423 if [ -x /usr/bin/updatedb ] ; then
424   einfo "Updating locate-database"
425   mount -t proc none /proc
426   updatedb --prunepaths='/tmp /usr/tmp /var/tmp /grml /root /proc /sys'
427   umount /proc
428   eend $?
429 else
430   ewarn "updatedb not installed" ; eend 0
431 fi
432
433 fi # end of $FAST-check
434
435 einfo "Unmounting all filesystems"
436   umount -a
437 eend $?
438
439 ################################################################################
440 # MISC STUFF
441 # ~~~~~~~~~~
442 #
443 # Security-audit:
444 # find / \! -type l -perm -2000 -ls > audit/sgid-files 
445 # find / \! -type l -perm -0002 -ls > audit/world-writeable-files
446 # find / \! -type l -perm -4000 -ls > audit/suid-files
447 # find / ( -perm 4000 -o -perm -2000 \) -exec ls -ld {} \;
448 # find / -perm -0777 -type d -ls > audit/writeable-dirs
449 # find / -size +3000k > audit/big_files.new
450 # find /all_filesystems -name ".. " -print -xdev | cat -vet
451 # find / -name ".*" -print -xdev | cat -v > audit/hidden-files
452 # find / -name ".. " -print -xdev | cat -vet > audit/unusal-file-names
453 #
454 # list non-stripped files:
455 # file /usr/sbin/* | grep -v ', stripped' | grep -v 'perl' | grep -v 'Bourne' | grep -v 'python' | grep -v 'symbolic link'
456 #
457 # einfo "Removing unused architecture Kernel sources: "
458 #   for i in `ls -1 /usr/src/linux/arch/ | grep -v i386`; do
459 #     echo -n "$i "; rm -rf /usr/src/linux/arch/"$i" /usr/src/linux/include/asm-"$i"
460 #   done
461 # eend $?
462 #
463 # Is software by grml-repos up2date?
464 # grep-dctrl --field Maintainer 'Michael Prokop' /var/lib/apt/lists/grml.org_repos_._* | grep Package | awk '{print $2}' | xargs echo 
465 #
466 # runlevel.conf:
467 # for file in /etc/init.d/* ; do grep $file -q /etc/runlevel.conf.hdinstall || echo $file not found ; done
468 ################################################################################
469 # ISO
470 # ~~~
471 # /usr/bin/mksquashfs /home/mika/grml/chroot/grml_uncompressed.large/* \
472 # /home/mika/grml/chroot/grml_cd.large/GRML/GRML  -noappend -sort /home/mika/grml/svn/grml/remastering/sort
473 #
474 # mkisofs -V "grml 0.8" -publisher 'Michael Prokop <mika@grml.org>' -l -r \
475 # -J -no-emul-boot -boot-load-size 4 -boot-info-table -c boot/isolinux/boot.cat \
476 # -b boot/isolinux/isolinux.bin -o /home/mika/grml/chroot/grml_0.8.iso .
477
478 # vim grml_cd.large/GRML/grml-version
479
480 # find . -type f -not -name md5sums -not -name boot.cat -exec md5sum {} \; > GRML/md5sums
481 ################################################################################
482 #
483 ## END OF FILE #################################################################