#! /bin/sh
### BEGIN INIT INFO
# Provides:          log2tmpfs
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Move log files to RAM disk tmpfs
# Description:       Move all quickly changing files such as log files to
#                    a RAM disk, as to reduce flash wear/disk spin-up and down
#                    Does require sufficient RAM space, so only /var/log
#                    directory is moved.
#                    To move /var/run and /var/lock, update /etc/default/rcS
# (but first see http://twobit.us/blog/2010/01/using-tmpfs-to-minimize-disk-io/)
### END INIT INFO

# Move /var/log to ram file system, to reduce write actions to flash
# note: this will loose all new log files when crashing!!!
# (could copy back during normal shut-down)
# Note: /var/run, /var/lock are placed in tmpfs via /etc/default/rcS
# /var/tmp would also be a candidate...

# runlevel 2-5 (2=default, 0=halt, 6=shutdown)
# suggested install: use update-rc.d log2tmpfs.sh defaults 19 71 

# Author: Kees Moerman, May 2010

# find recently modified files:
# find /var/ -newer <some_file> -print
# find /var/ -type f -newer <some_file> -exec ls -al '{}' \;
#
# Some files are located in other directories
# /var/lib/ntp/ntp.drift, /var/cache/samba/browse.dat 
# ntp has -f <driftfile> (or driftfile <driftfile> config) option
# update this to point into /var/log/ntpstats, and copy the original file here
# How to best initialise the ntp drift file?
#
# To see if it is working, you can check memory space
# in use by var/log (not when unmounted): df -h
# To see all current mounts: cat /proc/mounts

# check if samba cache directory not yet replaced, if so move and link
if [ ! -d /var/log/cache/samba ] && [ -d /var/cache/samba ]
then
    if [ ! -d /var/log/cache ]
    then
      /bin/mkdir -p /var/log/cache
    fi
    /bin/mv -f /var/cache/samba /var/log/cache/
    /bin/ln -s /var/log/cache/samba /var/cache/samba
fi

# create directory for temp mount point
if [ ! -d /var/log.oldstate ]
then
    /bin/mkdir -p /var/log.oldstate
fi

do_start() {
    if [ ! -f /var/log.oldstate/messages ]
    then
        # create ramdisk and copy current log content
        /bin/mkdir -p /mnt/tmpvarlog
        /bin/mount -t tmpfs tmpfs /mnt/tmpvarlog
        
        # rsyslogd is still hard linked to the files on ext3,
        # such as /var/log/syslog, /var/log.oldstate/auth.log
        /etc/init.d/rsyslog stop >/dev/null 2>&1
        
        /bin/cp -rp /var/log/* /mnt/tmpvarlog/
        
        # swap old log with copy, old log in /var/log.oldstate
        /bin/mount -o bind /var/log /var/log.oldstate
        /bin/mount -o bind /mnt/tmpvarlog /var/log
        
        /etc/init.d/rsyslog start >/dev/null 2>&1
        
        # could unmount tmpvarlog, if you don't want this visible in 'mount'
        # is safe, as umount keeps track of reference counts
        # /bin/umount /mnt/tmpvarlog
    fi
}

do_stop () {
    # to copy back current ram-based log to real log (note -p, -u)
    if [ -f /var/log.oldstate/messages ]
    then
      /bin/cp -rup /var/log/* /var/log.oldstate/
      /etc/init.d/rsyslog stop >/dev/null 2>&1
      /bin/sync
      /bin/umount /var/log
      /bin/umount /var/log.oldstate
      /bin/sync
      /bin/umount /mnt/tmpvarlog
      /etc/init.d/rsyslog start >/dev/null 2>&1
    fi
}

case "$1" in
  start)
    do_start
	;;
  stop)
    do_stop
	;;
  restart|force-reload)
	;;
  *)
	echo "Usage: mount_data {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

# end of script

