Drop grml-postfix
[grml-scripts.git] / usr_sbin / prepare_tmpfs.sh
1 #!/bin/zsh
2 # Filename:      prepare_tmpfs.sh
3 # Purpose:       set up a tmpfs of a selected directory
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 ################################################################################
8
9 if [ $(id -u) != 0 ] ; then
10   echo "Error: $0 requires root permissions. Exiting."
11   exit 1
12 fi
13
14 setopt nonomatch
15
16 usage(){
17   echo "Usage: $0 <directory> <start|stop>"
18 }
19
20 if ! [ -n "$1" -a -n "$2" ] ; then
21   usage
22   exit 1
23 fi
24
25 DIRECTORY="$1"
26
27 prepare_start () {
28   if ! mount | grep -q "tmpfs on ${DIRECTORY}" ; then
29    if [ -d $DIRECTORY ] ; then
30     if ! [ -d $DIRECTORY.tmpfile ] ; then
31      echo -n "Setting up tmpfs ${DIRECTORY}: "
32      mv $DIRECTORY/ $DIRECTORY.tmpfile && \
33      mkdir $DIRECTORY && \
34      if mount $TMPFS -t tmpfs tmpfs $DIRECTORY ; then
35        cp -a $DIRECTORY.tmpfile/*  $DIRECTORY  &>/dev/null
36        cp -a $DIRECTORY.tmpfile/.* $DIRECTORY  &>/dev/null
37        echo done
38      else
39        echo failed
40      fi
41     else
42      echo "Erorr: tmpdir $DIRECTORY.tmpfile exists already. Exiting."
43      exit 1
44     fi
45    else
46     echo "Error: $DIRECTORY does not exist. Exiting."
47     exit 1
48    fi
49   else
50     echo "Error: $DIRECTORY already mounted. Exiting."
51     exit 1
52   fi
53 }
54
55 prepare_stop () {
56   if mount | grep -q $DIRECTORY ; then
57     echo -n "Unmounting tmpfs ${DIRECTORY}: "
58     umount ${DIRECTORY} && \
59     rmdir $DIRECTORY && \
60     mv $DIRECTORY.tmpfile $DIRECTORY && echo done || echo failed
61   else
62     echo "Error: ${DIRECTORY} not mounted."
63     exit 1
64   fi
65 }
66
67 case "$2" in
68   start)
69      prepare_start || exit 1
70      ;;
71   stop)
72      prepare_stop || exit 1
73      ;;
74   *)
75      usage
76      exit 1
77 esac
78
79 exit 0
80
81 ## END OF FILE #################################################################