- remove daemon functionality entirely
authorMark Huang <mlhuang@cs.princeton.edu>
Sun, 21 Aug 2005 21:37:10 +0000 (21:37 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Sun, 21 Aug 2005 21:37:10 +0000 (21:37 +0000)
- use new 0.30.208 util-vserver-vars variable names
- setattr now installed in /usr/sbin

scripts/vcached

index 19e0dff..62886f6 100755 (executable)
@@ -1,12 +1,11 @@
 #!/bin/bash
 #
-# vcached: VServer cache daemon
+# vcached: VServer cache allocator
 #
-# 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. Can also be
-# run periodically with -s from /etc/cron.d.
+# Description: A script that 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 run periodically as a cron job.
 #
 # Based on work by:
 # 
 # William Wung - wungism@uclink.berkeley.edu
 #
 # Mark Huang <mlhuang@cs.princeton.edu>
-# Copyright (c) 2004  The Trustees of Princeton University (Trustees).
+# Copyright (c) 2004-2005 The Trustees of Princeton University
 #
-# $Id: vcached,v 1.9 2004/11/23 14:47:35 mlhuang Exp $
+# $Id: vcached,v 1.10 2004/11/29 21:56:45 mlhuang Exp $
 #
 
-# get configuration
-. /etc/vcached.conf
+# number of images to keep cached
+slots=32
+
+# PID file
+pidfile=/var/run/vcached.pid
+
+# log file
+logfile=/var/log/vcached.log
+
+# debug
+debug=0
+
+usage()
+{
+    echo "usage: vcached [OPTION...]"
+    echo "     -s [slots]      number of images to keep cached"
+    echo "     -p [pidfile]    PID file"
+    echo "     -l [logfile]    log file"
+    echo "     -d              debug"
+    exit 1
+}
 
 # parse options
-while getopts 'fdsl:' OPT ; do
+while getopts 's:p:l:dh' OPT ; do
     case "$OPT" in
-        f) foreground=1 ;;
+       s) slots=$OPTARG ;;
+       p) pidfile=$OPTARG ;;
+       l) logfile=$OPTARG ;;
         d) debug=1 ;;
-       s) single=1 ;;
-       l) exec 1>>$OPTARG ; exec 2>>$OPTARG ;;
+       h|*) usage ;;
     esac
 done
 
+# append output to log file
+exec 1>>$logfile
+exec 2>>$logfile
+
 # check if we are already running
-if [ -f $pidfile ] ; then
+if [ -f $pidfile ] && kill -0 `cat $pidfile` >/dev/null 2>&1 ; then
     echo "vcached(`cat $pidfile`) already running"
     exit 1
 fi
-
-# daemonize
-if [ $foreground -eq 0 ] ; then
-    nohup setsid nice -n $nice -- $0 $* -f -l $logfile >/dev/null 2>&1 </dev/null &
-    exit 0
-fi
-
-# record PID
-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}
+# clean up lock file before exiting
+trap "rm -f $pidfile" EXIT
+
+: ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
 test -e "$UTIL_VSERVER_VARS" || {
     echo "Can not find util-vserver installation; aborting..."
     exit 1
@@ -58,45 +74,38 @@ test -e "$UTIL_VSERVER_VARS" || {
 . "$UTIL_VSERVER_VARS"
 
 # make sure barrier bit is set on /vservers to prevent chroot() escapes
-$PKGLIBDIR/setattr --barrier $VROOTDIR
+setattr --barrier $__DEFAULT_VSERVERDIR
 
 # take out the trash
-rm -rf "$VROOTDIR/.vtmp"
-
-mkdir -p "$VROOTDIR/.vcache"
-mkdir -p "$VROOTDIR/.vtmp"
-
-# loop forever
-while : ; do
-    [ $debug -ne 0 ] && echo "$(date) Checking the cache"
-    for i in $(seq 0 $(($slots - 1))) ; do
-       if [ ! -d "$VROOTDIR/.vcache/v$i" ] ; then
-           echo "$(date) Caching v$i"
-           # build image in .vtmp
-           TMP=$(mktemp -d "$VROOTDIR/.vtmp/v$i.XXXXXX")
-           "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$TMP"
-           RETVAL=$?
-           # move it to .vcache when complete
-           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"
+rm -rf "$__DEFAULT_VSERVERDIR/.vtmp"
+
+mkdir -p "$__DEFAULT_VSERVERDIR/.vcache"
+mkdir -p "$__DEFAULT_VSERVERDIR/.vtmp"
+
+[ $debug -ne 0 ] && echo "$(date) Checking the cache"
+for i in $(seq 0 $(($slots - 1))) ; do
+    if [ ! -d "$__DEFAULT_VSERVERDIR/.vcache/v$i" ] ; then
+       echo "$(date) Caching v$i"
+       # build image in .vtmp
+       TMP=$(mktemp -d "$__DEFAULT_VSERVERDIR/.vtmp/v$i.XXXXXX")
+       "$__PKGLIBDIR/vbuild" "$__DEFAULT_VSERVERDIR/vserver-reference" "$TMP"
+       RETVAL=$?
+       # move it to .vcache when complete
+       if [ $RETVAL -eq 0 ] ; then
+           # sanity check
+           vnewsize=$(du -s "$TMP" | awk "{ print \$1 }")
+           vrefsize=$(du -s "$__DEFAULT_VSERVERDIR/vserver-reference" | awk "{ print \$1 }")
+           if [ $vnewsize -lt $vrefsize ] ; then
+               echo "WARNING: Unexpected for 'du -s $__DEFAULT_VSERVERDIR/$NAME'=$vnewsize to be less than 'du -s $__DEFAULT_VSERVERDIR/vserver-reference'=$vrefsize"
            fi
+
+           mv "$TMP" "$__DEFAULT_VSERVERDIR/.vcache/v$i"
+           echo "$(date) v$i ready"
+       else
+           echo "$(date) Error $RETVAL building v$i"
+           rm -rf "$TMP"
        fi
-    done
-    # just run once
-    if [ $single -ne 0 ] ; then
-       break
     fi
-    [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"
-    sleep $period
 done
+
+exit 0