- add option "-s" to run a single pass (e.g. along with "-f" from
[util-vserver.git] / scripts / vcached
1 #!/bin/bash
2 #
3 # vcached: VServer cache daemon
4 #
5 # Description: A daemon that periodically preallocates vservers and stores
6 # them in a cache.  Preallocated vservers from the cache may be then used to 
7 # instantiate real vservers. Requires that /var/run/vcached.pid does not
8 # exist on startup. Should start/stop/restart from /etc/init.d. Can also be
9 # run periodically with -s from /etc/cron.d.
10 #
11 # Based on work by:
12
13 # Brent Chun - bnc@intel-research.net
14 # Tristan Koo - tristan.koo@intel-research.net
15 # William Wung - wungism@uclink.berkeley.edu
16 #
17 # Mark Huang <mlhuang@cs.princeton.edu>
18 # Copyright (c) 2004  The Trustees of Princeton University (Trustees).
19 #
20 # $Id: vcached,v 1.7 2004/11/17 20:34:25 mef Exp $
21 #
22
23 # get configuration
24 . /etc/vcached.conf
25
26 # parse options
27 while getopts 'fdsl:' OPT ; do
28     case "$OPT" in
29         f) foreground=1 ;;
30         d) debug=1 ;;
31         s) single=1 ;;
32         l) exec 1>>$OPTARG ; exec 2>>$OPTARG ;;
33     esac
34 done
35
36 # check if we are already running
37 if [ -f $pidfile ] ; then
38     echo "vcached(`cat $pidfile`) already running"
39     exit 1
40 fi
41
42 # daemonize
43 if [ $foreground -eq 0 ] ; then
44     nohup setsid nice -n $nice -- $0 $* -f -l $logfile >/dev/null 2>&1 </dev/null &
45     exit 0
46 fi
47
48 # record PID
49 trap "rm -f $pidfile ; exit 255" EXIT
50 trap "exec 1>>$logfile ; exec 2>>$logfile" HUP
51 echo $$ > $pidfile
52
53 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
54 test -e "$UTIL_VSERVER_VARS" || {
55     echo "Can not find util-vserver installation; aborting..."
56     exit 1
57 }
58 . "$UTIL_VSERVER_VARS"
59
60 # take out the trash
61 chattr -R -i "$VROOTDIR/.vtmp"
62 rm -rf "$VROOTDIR/.vtmp"
63
64 mkdir -p "$VROOTDIR/.vcache"
65 mkdir -p "$VROOTDIR/.vtmp"
66
67 # loop forever
68 while : ; do
69     [ $debug -ne 0 ] && echo "$(date) Checking the cache"
70     for i in $(seq 0 $(($slots - 1))) ; do
71         if [ ! -d "$VROOTDIR/.vcache/v$i" ] ; then
72             echo "$(date) Caching v$i"
73             # build image in .vtmp
74             TMP=$(mktemp -d "$VROOTDIR/.vtmp/v$i.XXXXXX")
75             "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$TMP"
76             RETVAL=$?
77             # move it to .vcache when complete
78             if [ $RETVAL -eq 0 ] ; then
79                  # sanity check
80                 vnewsize=$(du -s "$TMP" | awk "{ print \$1 }")
81                 vrefsize=$(du -s "$VROOTDIR/vserver-reference" | awk "{ print \$1 }")
82                 if [ $vnewsize -lt $vrefsize ] ; then
83                     echo "WARNING: Unexpected for 'du -s $VROOTDIR/$NAME'=$vnewsize to be less than 'du -s $VROOTDIR/vserver-reference'=$vrefsize"
84                 fi
85
86                 mv "$TMP" "$VROOTDIR/.vcache/v$i"
87                 echo "$(date) v$i ready"
88             else
89                 echo "$(date) Error $RETVAL building v$i"
90                 chattr -R -i "$TMP"
91                 rm -rf "$TMP"
92             fi
93         fi
94     done
95     # just run once
96     if [ $single -ne 0 ] ; then
97         break
98     fi
99     [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"
100     sleep $period
101 done