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. Can also be
9 # run periodically with -s from /etc/cron.d.
13 # Brent Chun - bnc@intel-research.net
14 # Tristan Koo - tristan.koo@intel-research.net
15 # William Wung - wungism@uclink.berkeley.edu
17 # Mark Huang <mlhuang@cs.princeton.edu>
18 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
20 # $Id: vcached,v 1.8 2004/11/19 20:27:24 mlhuang Exp $
27 while getopts 'fdsl:' OPT ; do
32 l) exec 1>>$OPTARG ; exec 2>>$OPTARG ;;
36 # check if we are already running
37 if [ -f $pidfile ] ; then
38 echo "vcached(`cat $pidfile`) already running"
43 if [ $foreground -eq 0 ] ; then
44 nohup setsid nice -n $nice -- $0 $* -f -l $logfile >/dev/null 2>&1 </dev/null &
49 trap "rm -f $pidfile ; exit 255" EXIT
50 trap "exec 1>>$logfile ; exec 2>>$logfile" HUP
53 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
54 test -e "$UTIL_VSERVER_VARS" || {
55 echo "Can not find util-vserver installation; aborting..."
58 . "$UTIL_VSERVER_VARS"
61 rm -rf "$VROOTDIR/.vtmp"
63 mkdir -p "$VROOTDIR/.vcache"
64 mkdir -p "$VROOTDIR/.vtmp"
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"
76 # move it to .vcache when complete
77 if [ $RETVAL -eq 0 ] ; then
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"
85 mv "$TMP" "$VROOTDIR/.vcache/v$i"
86 echo "$(date) v$i ready"
88 echo "$(date) Error $RETVAL building v$i"
94 if [ $single -ne 0 ] ; then
97 [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"