PL3118 and PL3131 fix: do not reset the immutable bits on vservers
[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.8 2004/11/19 20:27:24 mlhuang 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 rm -rf "$VROOTDIR/.vtmp"
62
63 mkdir -p "$VROOTDIR/.vcache"
64 mkdir -p "$VROOTDIR/.vtmp"
65
66 # loop forever
67 while : ; do
68     [ $debug -ne 0 ] && echo "$(date) Checking the cache"
69     for i in $(seq 0 $(($slots - 1))) ; do
70         if [ ! -d "$VROOTDIR/.vcache/v$i" ] ; then
71             echo "$(date) Caching v$i"
72             # build image in .vtmp
73             TMP=$(mktemp -d "$VROOTDIR/.vtmp/v$i.XXXXXX")
74             "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$TMP"
75             RETVAL=$?
76             # move it to .vcache when complete
77             if [ $RETVAL -eq 0 ] ; then
78                  # sanity check
79                 vnewsize=$(du -s "$TMP" | awk "{ print \$1 }")
80                 vrefsize=$(du -s "$VROOTDIR/vserver-reference" | awk "{ print \$1 }")
81                 if [ $vnewsize -lt $vrefsize ] ; then
82                     echo "WARNING: Unexpected for 'du -s $VROOTDIR/$NAME'=$vnewsize to be less than 'du -s $VROOTDIR/vserver-reference'=$vrefsize"
83                 fi
84
85                 mv "$TMP" "$VROOTDIR/.vcache/v$i"
86                 echo "$(date) v$i ready"
87             else
88                 echo "$(date) Error $RETVAL building v$i"
89                 rm -rf "$TMP"
90             fi
91         fi
92     done
93     # just run once
94     if [ $single -ne 0 ] ; then
95         break
96     fi
97     [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"
98     sleep $period
99 done