HOWTO: Using tmpfs for /tmp, /var/{log,run,lock...}

Share your own howto's etc. Not for support questions!

HOWTO: Using tmpfs for /tmp, /var/{log,run,lock...}

Postby synss » 2007-06-27 06:07

Note: this tip is not intended to the beginner as it will change things early in the boot process. I expect you know how Debian boots using sys v and that you understand what you are doing.

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

It is a very interesting to know and can be safely used for /tmp. You can mount as many tmpfs as you want, so that a line in /etc/fstab such as:
Code: Select all
tmpfs /tmp tmpfs defaults,noexec,nosuid 0 0
actually does the job.

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=""


And /etc/init.d/my-tmpfs.sh
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

:


I call it my- because I like to put my- in front of my own scripts to recognize them, put if you do not like it change the name.

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

and reboot.

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)
Last edited by synss on 2007-07-31 04:32, edited 7 times in total.
synss
 
Posts: 28
Joined: 2007-06-22 15:35
Location: Japan

Postby synss » 2007-07-02 07:31

Updates in the first post:
    A working QUIET option for people using read-only media
    MNT_TMPFS allows to chose another directory than /dev/shm as mount point
    SHM_SIZE sets the size in ram used for tmpfs

and a bit more comments for the documentation.
synss
 
Posts: 28
Joined: 2007-06-22 15:35
Location: Japan

Postby synss » 2007-07-12 10:02

Update:
    Fixed a stupid bug

Thanks for the feedback anyway.
synss
 
Posts: 28
Joined: 2007-06-22 15:35
Location: Japan

Postby Dargor » 2007-07-12 10:15

Thanks for the howto, ill definitively be giving this a go.
User avatar
Dargor
 
Posts: 671
Joined: 2006-08-14 08:54
Location: New Zealand, Hamilton

Postby synss » 2007-07-17 13:44

Dargor wrote:Thanks for the howto, ill definitively be giving this a go.


Thank you for your feedback! and here I want to repeat that mounting /var/log on tmpfs, although in principal, it should be OK on laptops, is not for the beginner! but it is fun!

I update a couple of "cases" in the original post.

/var/log on tmpfs: /etc/init.d/ipw3945d needs to be updated as well as it will not create /var/log/ipw3945d, so in case you need it, you need to create that directory and give it the expected permission. The script does it for /var/run/ipw3945d so it is trivial to adapt. If anyone is interested, I can put the required changes to /etc/init.d/ipw3945d in full here.
synss
 
Posts: 28
Joined: 2007-06-22 15:35
Location: Japan

Postby perlhacker14 » 2007-07-17 16:52

Just curious, but how much RAM is reccommended for this? It seems useful and interesting, but I am a bit short on RAM.
Arven bids you a good day...

My Laptop: Toshiba Satellite A25-S3072; 3.06 GHz Pentium 4; 473 MiB RAM; Debian Testing/Unstable/Experimental / Slackware 12; Whatever WM/DE I feel like at the moment
User avatar
perlhacker14
 
Posts: 465
Joined: 2007-06-19 20:19
Location: 127.0.0.1

Postby synss » 2007-07-18 02:19

perlhacker14 wrote:Just curious, but how much RAM is reccommended for this? It seems useful and interesting, but I am a bit short on RAM.


Well, depends on what you want to include there, if you only go for /var/run and /var/lock, you can do it in /etc/default/rcS (and that is probably <12Mb), if you do include more, then it depends on how often you reboot. You can always use logrotate, too. But anyway, it should not be much, I used to do it on my late computer with 512Mb (running openbox). You can always check the size of the directories you would include with
du -sh <path/to/directory>
anyway, for /tmp /var/log /var/run /var/lock, it is not much but for some reason I do not know, df does not show me how much is used in this way.

Also note that tmpfs does not waste the unused space à la ramdisk.

But I would say that 64Mb are enough and will prevent RAM to fill to one half if the logger goes crazy.
synss
 
Posts: 28
Joined: 2007-06-22 15:35
Location: Japan

Postby Dargor » 2007-07-27 05:18

So how are you guys monitoring the writes to disk, to see if its being effective.
User avatar
Dargor
 
Posts: 671
Joined: 2006-08-14 08:54
Location: New Zealand, Hamilton

Postby synss » 2007-07-27 06:32

Dargor wrote:So how are you guys monitoring the writes to disk, to see if its being effective.


Not sure what you mean.

It should not make a big difference anyway, just make the tmp dirs *really* temporary and avoid some unnecessary spinning up of your hard drive. If the dirs content's gone after you reboot, then it's working.
synss
 
Posts: 28
Joined: 2007-06-22 15:35
Location: Japan

Postby Dargor » 2007-07-27 08:05

How do you monitor the number of writes to the disk the frequency of writes, so i can compare my gateways day with/without doing this tmpfs mod.
User avatar
Dargor
 
Posts: 671
Joined: 2006-08-14 08:54
Location: New Zealand, Hamilton

Postby synss » 2007-07-31 03:35

Dargor wrote:How do you monitor the number of writes to the disk the frequency of writes, so i can compare my gateways day with/without doing this tmpfs mod.


Well, actually I don't. Performance will depend on the dirs you put on tmpfs, of course, but this is not so important to me. I see it more as a convenience trick than a performance one. I even do not really think the overall performance of the computer will change significantly.
However, if you want to mount your filesystem read-only, tmpfs will help as well.
I have a few more exceptions for the /var/log section I will add later today. EDIT: done! but I think the cases should be taken into account into the corresponding /etc/init.d/* and not in my script... but it looks like the Debian ppl never thought anyone would dish /var/log/*!
synss
 
Posts: 28
Joined: 2007-06-22 15:35
Location: Japan

Thanks

Postby camason » 2008-12-28 10:39

I know this is an old topic, but this script has been very useful in setting up the correct log folders on my Asus Eee 901 (Ubuntu) with an Apache server.

So I decided to take the time and register to say thanks!
camason
 
Posts: 1
Joined: 2008-12-28 10:35

Thanks too

Postby bill_baroud » 2009-10-03 20:00

More and more older topic, but still usefull, thanks :)

Used with a mini atx box, Via 1GHz 512 SDRAM CF 1Go Lenny 2.6-26-2-486
bill_baroud
 
Posts: 1
Joined: 2009-10-03 19:56

Re: HOWTO: Using tmpfs for /tmp, /var/{log,run,lock...}

Postby bsod » 2010-12-07 12:23

Hi,

This year I'm the one who say thanks, I'm about to use your great script to save the hard drive of an old laptop that will run 24/24 (I know it's not a very good idea).

I plan to add more directory in tmpfs and maybe rsync them daily on the hd (it wouldn't be a big loss if I loose the last day of data/log/etc.).

So thank you for this work!

bsod
bsod
 
Posts: 1
Joined: 2004-09-15 10:06

Re: HOWTO: Using tmpfs for /tmp, /var/{log,run,lock...}

Postby friese » 2011-07-06 18:37

Hi,

I also just registered to say thanks. We will use this on our self-build ThinClients to save the CF they boot from. I made a small addition and added a "Provides"-info at the top:
Code: Select all
### BEGIN INIT INFO
# Provides:          var_to_ram
# Required-Start:    mountall

This way it can be activated by insserv or even rcconf and you can use the info in "Provides" to prevent other services from beeing started before this script ran by using
Code: Select all
# Required-Start:    var_to_ram

in their scripts.

Thanks again, Jens
friese
 
Posts: 7
Joined: 2011-07-06 18:26

Next

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: magnusk and 4 guests

fashionable