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.9 2004/11/23 14:47:35 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"
60 # make sure barrier bit is set on /vservers to prevent chroot() escapes
61 $PKGLIBDIR/setattr --barrier $VROOTDIR
64 rm -rf "$VROOTDIR/.vtmp"
66 mkdir -p "$VROOTDIR/.vcache"
67 mkdir -p "$VROOTDIR/.vtmp"
71 [ $debug -ne 0 ] && echo "$(date) Checking the cache"
72 for i in $(seq 0 $(($slots - 1))) ; do
73 if [ ! -d "$VROOTDIR/.vcache/v$i" ] ; then
74 echo "$(date) Caching v$i"
75 # build image in .vtmp
76 TMP=$(mktemp -d "$VROOTDIR/.vtmp/v$i.XXXXXX")
77 "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$TMP"
79 # move it to .vcache when complete
80 if [ $RETVAL -eq 0 ] ; then
82 vnewsize=$(du -s "$TMP" | awk "{ print \$1 }")
83 vrefsize=$(du -s "$VROOTDIR/vserver-reference" | awk "{ print \$1 }")
84 if [ $vnewsize -lt $vrefsize ] ; then
85 echo "WARNING: Unexpected for 'du -s $VROOTDIR/$NAME'=$vnewsize to be less than 'du -s $VROOTDIR/vserver-reference'=$vrefsize"
88 mv "$TMP" "$VROOTDIR/.vcache/v$i"
89 echo "$(date) v$i ready"
91 echo "$(date) Error $RETVAL building v$i"
97 if [ $single -ne 0 ] ; then
100 [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"