Debian Using tmpfs for /tmp, /var/(log,run,lock...)

From wiki.linuxonlinehelp.de
Jump to: navigation, search

To Save CF-Flash-Cards from burned do:

nano /etc/fstab

add:

tmpfs /tmp tmpfs defaults,noexec,nosuid 0 0

create: nano /etc/default/tmpfs.sh insert:

MNT_TMPFS="/dev/shm"
DIRS="/tmp /var/log /var/run /var/lock"
OPTS_TMPFS="nodev,noexec"
#SHM_SIZE=64
#QUIET=""

nano /etc/init.d/tmpfs.sh insert:

    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:         
    # Required-Start:    mountall
    # Required-Stop:
    # Should-Start:     
    # Should-Stop:
    # Default-Start:     S
    # Default-Stop:
    # Short-Description: Mount directories in tmpfs
    # Description: This initscript can bind directories in a tmpfs ramdisk, the
    #              primary goal is to allow laptop users to let the hard-disk
    #              drive spin up most of the time, it can also be used by people
    #              having their system on a USB disk or a CF card.
    # Author: Mathias Laurin
    # License: GNU General Public License version 2 or higher
    # Version: 1.0.4
    ### END INIT INFO

    PATH=/sbin:/bin
    NAME="my-tmpfs.sh"

    . /lib/init/vars.sh
    . /lib/lsb/init-functions

    # Source tmpfs before $NAME so that SHM_SIZE gets overwritten
    [ -e /etc/default/tmpfs ] && . /etc/default/tmpfs
    [ -e /etc/default/$NAME ] && . /etc/default/$NAME

    # no directory to mount, exit graciously
    [ -z "$DIRS" ] && exit 0
    [ -n "$QUIET" ] && OPTS_TMPFS_QUIET="-n"
    [ -z "MNT_TMPFS" ] && MNT_TMPFS="/dev/shm"

    # no /dev/shm, error and exit
    [ ! -d "/dev/shm" ] && echo "Enable tmpfs in the kernel" >&2 && exit 1

    do_start() {
       # Prepare mount point
       [ ! -d "$MNT_TMPFS" ] && mkdir "$MNT_TMPFS"
       
       # Mount options
       MNT_OPTS=${MNT_OPTS:-"rw,nodev,nosuid"}
       [ -n "$SHM_SIZE" ] && MNT_OPTS="$MNT_OPTS",size=$SHM_SIZE

       [ -n "$(grep $MNT_TMPFS /proc/mounts)" ] && umount "$MNT_TMPFS"
       
       mount -t tmpfs -o "$MNT_OPTS" tmpfs "$MNT_TMPFS"
       
       for MY_DIR in $DIRS; do
          MY_TMPFS="$MNT_TMPFS/$MY_DIR"
          [ ! -d "$MY_TMPFS" ] && mkdir -p "$MY_TMPFS"

          mount --bind $OPTS_TMPFS_QUIET "$MY_TMPFS" "$MY_DIR" -o "$OPTS_TMPFS"

          # special cases handled here, using "case" allows more flexibility
          case $MY_DIR in
             /tmp|/var/tmp)
             mount -o,remount,nodev,nosuid $MY_DIR
             chmod 1777 $MY_DIR
             ;;
             /var/log)
             touch /var/log/lastlog
             touch /var/log/wtmp
             chgrp utmp /var/log/lastlog
             chmod 0644 /var/log/lastlog
             chmod 0644 /var/log/wtmp
             chmod 0600 /var/log/btmp
             mkdir /var/log/news
             chown news:news /var/log/news
             chmod g+s /var/log/news
             [ -f /etc/init.d/mpd ] &&
             mkdir /var/log/mpd &&
             chown mpd:audio /var/log/mpd
             [-f /etc/init.d/ipw3945d ] &&
             mkdir /var/log/ipw3945d &&
             chown Debian-ipw3945d:Debian-ipw3945d /var/log/ipw3945d
             ;;
             /var/run)
             mount -o,remount,nodev,nosuid,noexec /var/run
             chmod 1777 /var/run
             [ ! -d /var/run/screen ] && mkdir -m 0777 /var/run/screen
             touch /var/run/utmp
             chgrp utmp /var/run/utmp
             chmod 0644 /var/run/utmp
             ;;
          esac
       done
    }

    case "$1" in
      start|"")
       do_start
       ;;
      restart|reload|force-reload)
       echo "Error: argument '$1' not supported" >&2
       exit 3
       ;;
      stop)
       # No-op
       ;;
      *)
       echo "Usage: my-tmpfs.sh [start|stop]" >&2
       exit 3
       ;;
    esac

    :

now do:

chmod 755 /etc/init.d/my-tmpfs.sh
cd /etc/rcS.d
ln -s ../init.d/tmpfs.sh S37tmpfs.sh 

reboot..