User Tools

Site Tools


acdsn:tmpfs

tmpfs

/etc/default/tmpfs

# Where to mount the DIRS
# if empty, defaults to /dev/shm
MNT_TMPFS="/dev/shm"
 
# Set directories to be mounted in tmpfs
# /var/log is not recommended!
# /etc/default/rcS for /var/run and /var/lock
DIRS="/tmp /var/log /var/run /var/lock"
 
# Mount options for tmpfs
# if empty, defaults to rw,nodev,nosuid
# if you want to set the size= option, you need to do it in
#/etc/default/tmpfs
OPTS_TMPFS="nodev,noexec"
 
# SHM_SIZE sets the maximum size (in bytes) that the /dev/shm tmpfs can use.
# If this is not set then the size defaults to the value of TMPFS_SIZE
# if that is set; otherwise to the kernel's default.
#
# The size will be rounded down to a multiple of the page size, 4096 bytes.
SHM_SIZE=
 
# "quiet mount" will pass the -n option to the mount command
# mount(8) for more information
# Leave empty to unset
#QUIET=""

/etc/init.d/tmpfs

#! /bin/sh
### BEGIN INIT INFO
# Provides:          tmpfs       
# Required-Start:    mountall
# Required-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
# update-rc.d tmpfs defaults 10 
# update-rc.d tmpfs enable 2
### END INIT INFO
 
PATH=/sbin:/bin
NAME="tmpfs"
 
. /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
 
:

and update all with:

  # chmod a+x /etc/init.d/tmpfs
  # update-rc.d tmpfs defaults

mount

The original fs is like:

/dev/mmcblk0p2 on / type ext4 (rw,noatime)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/mmcblk0p1 on /media/mmc_p1 type vfat (rw,noatime)
/dev/mmcblk0p3 on /media/data type ext4 (rw,noatime)

and finally you obtain

/dev/mmcblk0p2 on / type ext4 (rw,noatime)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/mmcblk0p1 on /media/mmc_p1 type vfat (rw,noatime)
/dev/mmcblk0p3 on /media/data type ext4 (rw,noatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
/dev/shm/tmp on /tmp type none (rw,noexec,nosuid,nodev,bind)
/dev/shm/var/log on /var/log type none (rw,noexec,nodev,bind)
/dev/shm/var/run on /var/run type none (rw,noexec,nosuid,nodev,bind)
/dev/shm/var/lock on /var/lock type none (rw,noexec,nodev,bind)
acdsn/tmpfs.txt · Last modified: 2013/07/24 14:51 by jzaehrin