Mount /var/log into temporary file system and preserve the file structure after reboot

Note: The solution is running and tested under openSUSE 11.3. If you adjust it to another distribution please leave a comment.

The idea

There are cases where you want to mount your log files into RAM disk. Propably not on a server but in a desktop environment. The general advantages are:

  • lesser disk access - log files are changing constantly
  • logs are condemned after shutdown

The last one is also the only disadvantage. By the way, this line in /etc/fstab mounts /var/log to a temporary RAM disk:

tmpfs        /var/log        tmpfs       defaults,noatime,mode=0755        0 0

The problem

Some daemons rely on an existing log file. They stop logging if the file (or the directory it resides in) doesn't exist anymore. If you don't want to have log messages at all it would be easier to log into /dev/null. Therefore you need to recreate the file/directory structure in /var/log after mounting the RAM disk.

I resolved it by running one additional init script (see below) right after the file systems are mounted. Basically it only knows to options - start and create. Start is issued at boot time, to extract the file structure to /var/log (you don't have to worry about if you get the script started at boot time).

# /etc/init.d/boot.localfs-tmp usage
Usage: /etc/init.d/boot.localfs-tmp {start|create}

Running create will preserve the existing file structure. You will have to run it manually after each new installation of a daemon that doesn't log through rsyslog. In most cases it is enough to run it once after a fresh install before changing the mount point to tmpfs.

# /etc/init.d/boot.localfs-tmp create
created /etc/log-skel.tgz

As you can see the script will truncate all files in /var/log to a length of zero and pack them (including directories and ownership / permissions) into /etc/log-skel.tgz from where they will be extracted when the script is started (at boot time).

The code

Put the code into /etc/init.d/boot.localfs-tmp, chmod it to 744 and go to yast's run level editor to make it started by default.

#
# /etc/init.d/boot.localfs-tmp
#
### BEGIN INIT INFO
# Provides:          boot.localfs-tmp
# Required-Start:    boot.localfs
# Should-Start:      
# Required-Stop:     
# Should-Stop:       
# Default-Start:     B
# Default-Stop:
# Short-Description: create log file system in tmpfs
# Description:       some services need a given file skel in /var/log - create it
### END INIT INFO

. /etc/rc.status

# First reset status of this service
rc_reset

case "$1" in
  # create skeleton of /var/log at /etc/log-skel.tgz
  create)
    for file in `/bin/find /var/log -type f`; do
      > "$file";
    done
    files=`/bin/find /var/log`;
    /bin/tar czf /etc/log-skel.tgz /var/log --absolute-names;
    /bin/chown root.root /etc/log-skel.tgz;
    /bin/chmod 500 /etc/log-skel.tgz;
    echo "created /etc/log-skel.tgz";
  ;;
# when started uncompress skel into tmpfs at /var/log
  start)
    /bin/tar xzf /etc/log-skel.tgz --keep-old-files --absolute-names;
    echo -n "create log skeleton in tmpfs"
    rc_status -v
    rc_exit
  ;;
  # dummy stop section
  stop)
    echo -n "ha ha, brainless tmpfs";
    rc_status -v
    rc_exit
  ;;
  # usage:
  *)
    echo "Usage: $0 {start|create}";
    exit 1
  ;;
esac

rc_exit

--

Feel free to leave a comment.

Geben Sie einen Kommentar ab

Kommentare

  • This seems to be all they have left to work with, which has some people panicking

    Erstellt von replica watches, 07/12/2012 2:55pm (vor 12 Jahre)

  • Hi,
    I adapted the script for debian:
    -->START SCRIPT<--
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides: boot.localfs-tmp
    # Required-Start: $remote_fs $time
    # Required-Stop: umountnfs $time
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: create log file system in tmpfs
    # Description: some services need a given file skel in /var/log - create it
    ### END INIT INFO
    #
    # Author: http://www.nohl.eu/tech-resources/notes-to-linux/log-files-in-tmpfs-without-breaking-logging/ adapted for Debian by Pietro <pc@localhost>
    #

    # PATH should only include /usr/* if it runs after the mountnfs.sh script
    PATH=/sbin:/usr/sbin:/bin:/usr/bin
    DESC="create log file system in tmpfs"
    NAME=boot.localfs-tmp

    SCRIPTNAME=/etc/init.d/$NAME

    case "$1" in
    # create skeleton of /var/log at /etc/log-skel.tgz
    create)
    for file in `/usr/bin/find /var/log -type f`; do
    > "$file";
    done
    files=`/usr/bin/find /var/log`;
    /bin/tar czf /etc/log-skel.tgz /var/log --absolute-names;
    /bin/chown root.root /etc/log-skel.tgz;
    /bin/chmod 500 /etc/log-skel.tgz;
    echo "created /etc/log-skel.tgz";
    ;;
    # when started uncompress skel into tmpfs at /var/log
    start)
    /bin/tar xzf /etc/log-skel.tgz --keep-old-files --absolute-names;
    echo -n "create log skeleton in tmpfs"
    ;;
    # dummy stop section
    stop)
    echo -n "ha ha, brainless tmpfs";
    ;;
    # usage:
    *)
    echo "Usage: $0 {start|create}";
    exit 1
    ;;
    esac
    -->END SCRIPT<--

    and after:
    sudo update-rc.d boot.localfs-tmp start 01 2 3 4 5 .

    Erstellt von pietro, 12/06/2012 5:33pm (vor 13 Jahre)

RSS Feed für die Kommentare auf dieser Seite | RSS feed für alle Kommentare