3 # vcached: VServer cache daemon
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.
12 # Brent Chun - bnc@intel-research.net
13 # Tristan Koo - tristan.koo@intel-research.net
14 # William Wung - wungism@uclink.berkeley.edu
16 # Mark Huang <mlhuang@cs.princeton.edu>
17 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
19 # $Id: vcached,v 1.6 2004/10/13 02:35:47 mlhuang Exp $
26 while getopts 'fdl:' OPT ; do
30 l) exec 1>>$OPTARG ; exec 2>>$OPTARG ;;
34 # check if we are already running
35 if [ -f $pidfile ] ; then
36 echo "vcached(`cat $pidfile`) already running"
41 if [ $foreground -eq 0 ] ; then
42 nohup setsid nice -n $nice -- $0 $* -f -l $logfile >/dev/null 2>&1 </dev/null &
47 trap "rm -f $pidfile ; exit 255" EXIT
48 trap "exec 1>>$logfile ; exec 2>>$logfile" HUP
51 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
52 test -e "$UTIL_VSERVER_VARS" || {
53 echo "Can not find util-vserver installation; aborting..."
56 . "$UTIL_VSERVER_VARS"
59 chattr -R -i "$VROOTDIR/.vtmp"
60 rm -rf "$VROOTDIR/.vtmp"
62 mkdir -p "$VROOTDIR/.vcache"
63 mkdir -p "$VROOTDIR/.vtmp"
67 [ $debug -ne 0 ] && echo "$(date) Checking the cache"
68 for i in $(seq 0 $(($slots - 1))) ; do
69 if [ ! -d "$VROOTDIR/.vcache/v$i" ] ; then
70 echo "$(date) Caching v$i"
71 # build image in .vtmp
72 TMP=$(mktemp -d "$VROOTDIR/.vtmp/v$i.XXXXXX")
73 "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$TMP"
75 # move it to .vcache when complete
76 if [ $RETVAL -eq 0 ] ; then
78 vnewsize=$(du -s "$TMP" | awk "{ print \$1 }")
79 vrefsize=$(du -s "$VROOTDIR/vserver-reference" | awk "{ print \$1 }")
80 if [ $vnewsize -lt $vrefsize ] ; then
81 echo "WARNING: Unexpected for 'du -s $VROOTDIR/$NAME'=$vnewsize to be less than 'du -s $VROOTDIR/vserver-reference'=$vrefsize"
84 mv "$TMP" "$VROOTDIR/.vcache/v$i"
85 echo "$(date) v$i ready"
87 echo "$(date) Error $RETVAL building v$i"
93 [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"