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.7 2004/11/17 20:34:25 mef 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 chattr -R -i "$VROOTDIR/.vtmp"
62 rm -rf "$VROOTDIR/.vtmp"
64 mkdir -p "$VROOTDIR/.vcache"
65 mkdir -p "$VROOTDIR/.vtmp"
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"
77 # move it to .vcache when complete
78 if [ $RETVAL -eq 0 ] ; then
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"
86 mv "$TMP" "$VROOTDIR/.vcache/v$i"
87 echo "$(date) v$i ready"
89 echo "$(date) Error $RETVAL building v$i"
96 if [ $single -ne 0 ] ; then
99 [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"