#!/bin/bash

# Illustrating how to set up an FC1 system to 
# save and restore log files from a tmpfs on system
# startup/shutdown.

# Where we are building the system
# Warning: everything under this directory will be
# removed.
BUILDDIR=/extra_disk/wansys

# tar up contents of some dirs that will be \
# tmpfs'es when the system is running, then \
# replace those dirs with symlinks.
cat >/tmp/saved-state.txt.$$ <<EOF
var/log/
var/spool/
var/lib/ntp/
var/lock/
var/run/
var/lib/logrotate.status
EOF

tar -T /tmp/saved-state.txt.$$ -C $BUILDDIR -z \
-c -f $BUILDDIR/var/saved-state.tgz
for file in `cat /tmp/saved-state.txt.$$`
do
  rm -rf $BUILDDIR/$file
done

# Actually create all the links
mkdir $BUILDDIR/var/impermanent
mount -t tmpfs $BUILDDIR/var/impermanent \
$BUILDDIR/var/impermanent
tar -C $BUILDDIR/var/impermanent -z -x -f \
$BUILDDIR/var/saved-state.tgz
( cd $BUILDDIR/var && ln -s impermanent/var/log . )
( cd $BUILDDIR/var && \
ln -s impermanent/var/spool . )
( cd $BUILDDIR/var/lib && \
ln -s ../impermanent/var/lib/ntp . )
( cd $BUILDDIR/var && ln -s impermanent/var/lock . )
( cd $BUILDDIR/var && ln -s impermanent/var/run . )
( cd $BUILDDIR/var/lib && \
ln -s ../impermanent/var/lib/logrotate.status . )
umount $BUILDDIR/var/impermanent

# Symlink /var/tmp to /tmp to make it tmpfs.
rm -rf $BUILDDIR/var/tmp
( cd $BUILDDIR/var && ln -s ../tmp . )