reverted rev 82
[grml-scripts.git] / usr_sbin / prepare_tmpfs.sh
1 #!/bin/zsh
2 # Filename:      prepare_tmpfs.sh
3 # Purpose:       setup a tmpfs
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Sam Mai 27 15:13:03 CEST 2006 [mika]
8 ################################################################################
9
10 if [ $UID != 0 ]; then
11   echo "Error: $0 requires root permissions. Exiting."
12   exit 1
13 fi
14
15 setopt nonomatch
16
17 usage(){
18   echo "Usage: $0 <directory> <start|stop>"
19 }
20
21 if ! [ -n "$1" -a -n "$2" ] ; then
22   usage
23   exit 1
24 fi
25
26 [ -d /UNIONFS ] && UNIONFS="/UNIONFS" # running from live-CD?
27
28 DIRECTORY="$1"
29
30 prepare_start () {
31   if ! mount | grep -q "tmpfs on ${UNIONFS}${DIRECTORY}" ; then
32    if [ -d $DIRECTORY ] ; then
33     if ! [ -d $DIRECTORY.tmpfile ] ; then
34      echo -n "Setting up tmpfs ${DIRECTORY}: "
35      mv $DIRECTORY/ $DIRECTORY.tmpfile && \
36      mkdir $DIRECTORY && \
37      if mount $TMPFS -t tmpfs tmpfs $DIRECTORY ; then
38        cp -a $DIRECTORY.tmpfile/*  $DIRECTORY  &>/dev/null
39        cp -a $DIRECTORY.tmpfile/.* $DIRECTORY  &>/dev/null
40        echo done
41      else
42        echo failed
43      fi
44     else
45      echo "Erorr: tmpdir $DIRECTORY.tmpfile exists already. Exiting."
46      exit 1
47     fi
48    else
49     echo "Error: $DIRECTORY does not exist. Exiting."
50     exit 1
51    fi
52   else
53     echo "Error: $DIRECTORY already mounted. Exiting."
54     exit 1
55   fi
56 }
57
58 prepare_stop () {
59   if mount | grep -q $DIRECTORY ; then
60     echo -n "Unmounting tmpfs ${DIRECTORY}: "
61     umount ${UNIONFS}${DIRECTORY} && \
62     rmdir $DIRECTORY && \
63     mv $DIRECTORY.tmpfile $DIRECTORY && echo done || echo failed
64   else
65     echo "Error: ${DIRECTORY} not mounted."
66     exit 1
67   fi
68 }
69
70 case "$2" in
71   start)
72      prepare_start || exit 1
73      ;;
74   stop)
75      prepare_stop || exit 1
76      ;;
77   *)
78      usage
79      exit 1
80 esac
81
82 exit 0
83
84 ## END OF FILE #################################################################