# Description: A daemon that periodically preallocates vservers and stores
# them in a cache. Preallocated vservers from the cache may be then used to
# instantiate real vservers. Requires that /var/run/vcached.pid does not
-# exist on startup. Should start/stop/restart from /etc/init.d.
+# exist on startup. Should start/stop/restart from /etc/init.d. Can also be
+# run periodically with -s from /etc/cron.d.
#
# Based on work by:
#
# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (c) 2004 The Trustees of Princeton University (Trustees).
#
-# $Id: vcached,v 1.1 2004/07/30 16:46:24 mlh-pl_kernel Exp $
+# $Id: vcached,v 1.9 2004/11/23 14:47:35 mlhuang Exp $
#
-# number of images to keep cached
-slots=32
-
-# fill the cache periodically
-period=$((60 * 15))
-
-# nice adjustment
-nice=10
-
-# PID file
-pidfile=/var/run/vcached.pid
-
-# log file
-logfile=/var/log/vcached.log
-
-# run in foreground
-foreground=0
-
-# debug
-debug=0
+# get configuration
+. /etc/vcached.conf
# parse options
-while getopts 'fd' OPT ; do
+while getopts 'fdsl:' OPT ; do
case "$OPT" in
f) foreground=1 ;;
d) debug=1 ;;
+ s) single=1 ;;
+ l) exec 1>>$OPTARG ; exec 2>>$OPTARG ;;
esac
done
# check if we are already running
-[ -f $pidfile ] && exit 1
+if [ -f $pidfile ] ; then
+ echo "vcached(`cat $pidfile`) already running"
+ exit 1
+fi
# daemonize
if [ $foreground -eq 0 ] ; then
- nohup nice -n $nice -- $0 $* -f >>$logfile 2>&1 &
+ nohup setsid nice -n $nice -- $0 $* -f -l $logfile >/dev/null 2>&1 </dev/null &
exit 0
fi
# record PID
-trap "killall -q -w vbuild ; rm -f $pidfile ; exit 255" EXIT
+trap "rm -f $pidfile ; exit 255" EXIT
+trap "exec 1>>$logfile ; exec 2>>$logfile" HUP
echo $$ > $pidfile
: ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
}
. "$UTIL_VSERVER_VARS"
+# make sure barrier bit is set on /vservers to prevent chroot() escapes
+$PKGLIBDIR/setattr --barrier $VROOTDIR
+
# take out the trash
-chattr -R -i "$VROOTDIR/.vtmp"
rm -rf "$VROOTDIR/.vtmp"
mkdir -p "$VROOTDIR/.vcache"
if [ ! -d "$VROOTDIR/.vcache/v$i" ] ; then
echo "$(date) Caching v$i"
# build image in .vtmp
- "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$VROOTDIR/.vtmp/v$i"
+ TMP=$(mktemp -d "$VROOTDIR/.vtmp/v$i.XXXXXX")
+ "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$TMP"
+ RETVAL=$?
# move it to .vcache when complete
- mv "$VROOTDIR/.vtmp/v$i" "$VROOTDIR/.vcache/v$i"
- echo "$(date) v$i ready"
+ if [ $RETVAL -eq 0 ] ; then
+ # sanity check
+ vnewsize=$(du -s "$TMP" | awk "{ print \$1 }")
+ vrefsize=$(du -s "$VROOTDIR/vserver-reference" | awk "{ print \$1 }")
+ if [ $vnewsize -lt $vrefsize ] ; then
+ echo "WARNING: Unexpected for 'du -s $VROOTDIR/$NAME'=$vnewsize to be less than 'du -s $VROOTDIR/vserver-reference'=$vrefsize"
+ fi
+
+ mv "$TMP" "$VROOTDIR/.vcache/v$i"
+ echo "$(date) v$i ready"
+ else
+ echo "$(date) Error $RETVAL building v$i"
+ rm -rf "$TMP"
+ fi
fi
done
+ # just run once
+ if [ $single -ne 0 ] ; then
+ break
+ fi
[ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"
sleep $period
done