I have been happily using Gentoo for a long time and now got Debian on my laptop. The idea is not mine but comes from the Gentoo forums http://forums.gentoo.org/viewtopic-t-37 ... tmpfs.html
So, tmpfs allows you to use part of your RAM as a normal filesystem. Unlike a ramdisk, the unused space is not wasted. Provided your kernel is recent enough, you have it already set at /dev/shm. You can find and set how big it should be (maximum size) in
Code: Select all
cat /etc/default/tmpfs
Code: Select all
tmpfs /tmp tmpfs defaults,noexec,nosuid 0 0
Now, I find it cleaner not to clutter /etc/fstab with that kind of things and I prefer to mount --bind stuff in /dev/shm, I achieved that with the following initscripts:
/etc/default/my-tmpfs.sh
Code: Select all
# 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"
# Size of the ramdisk, note that the unoccupied space in tmpfs is not
# wasted so that this parameter can be made artificially high without
# impairing performance
# if empty, defaults to the value in /etc/default/tmpfs
#SHM_SIZE=64
# "quiet mount" will pass the -n option to the mount command
# mount(8) for more information
# Leave empty to unset
#QUIET=""
Code: Select all
#! /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
:
Then
Code: Select all
chmod 755 /etc/init.d/my-tmpfs.sh
cd /etc/rcS.d
ln -s ../init.d/my-tmpfs.sh S37my-tmpfs.sh
That should do it.
Consider my script "(c) Mathias Laurin, Tokyo, 2007, GPL2+" after an idea from the Gentoo forums see the link above and modify as you see fits.
Now there will probably be a few (not fatal) error messages due to missing directories if you use the same list as me.
mounting /var/log is not recommended because your log will not be available at the next reboot, but if on a laptop with suspend working, for example, you probably do not care much about the logs anyway, so you can tweak /etc/syslog.conf not to log "news" at all (so that it does not complain /var/log/news is missing)
You can also add "case"s in my script for special... cases you think of. Follow what I have done for /tmp.
Also read http://www.debian-administration.org/articles/57 and following discussion about making /tmp noexec.
And note that options in /etc/default/rcS can put /var/run and /var/lock in tmpfs (but no other dir)