Rewrote vcached in bash
authorMark Huang <mlhuang@cs.princeton.edu>
Fri, 30 Jul 2004 16:46:24 +0000 (16:46 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Fri, 30 Jul 2004 16:46:24 +0000 (16:46 +0000)
scripts/S20vcached [new file with mode: 0755]
scripts/vcached [new file with mode: 0755]

diff --git a/scripts/S20vcached b/scripts/S20vcached
new file mode 100755 (executable)
index 0000000..5e2eefa
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/sh
+#
+# chkconfig: 2345 20 80
+# description: vcached startup script
+# pidfile: /var/run/vcached.pid
+#
+. /etc/init.d/functions
+RETVAL=0
+
+start() {
+    echo -n "Starting vcached: "
+    initlog -c /usr/sbin/vcached
+    RETVAL=$?
+    [ "$RETVAL" -eq 0 ] && success $"vcached start" || failure $"vcached start"
+    echo
+    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/vcached
+    return $RETVAL
+}
+
+stop() {
+    echo -n "Stopping vcached: "
+    if [[ -e /var/run/vcached.pid ]]; then
+        /bin/kill `cat /var/run/vcached.pid`
+        RETVAL=$?
+    else
+        RETVAL=1
+    fi
+    [ "$RETVAL" -eq 0 ] && success $"vcached shutdown" || failure $"vcached shutdown
+"
+    echo
+    rm -f /var/lock/subsys/vcached
+    rm -f /var/run/vcached.pid
+    return $RETVAL
+}    
+
+restart() {
+    stop
+    start
+}    
+
+case "$1" in
+    start)
+        start
+        ;;
+    stop)
+        stop
+        ;;
+    restart)
+        restart
+        ;;
+    *)
+        echo $"Usage: $0 {start|stop|restart}"
+        exit 1
+esac
+exit $?
diff --git a/scripts/vcached b/scripts/vcached
new file mode 100755 (executable)
index 0000000..8f53374
--- /dev/null
@@ -0,0 +1,89 @@
+#!/bin/bash
+#
+# vcached: VServer cache daemon
+#
+# 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.
+#
+# Based on work by:
+# 
+# Brent Chun - bnc@intel-research.net
+# Tristan Koo - tristan.koo@intel-research.net
+# William Wung - wungism@uclink.berkeley.edu
+#
+# Copyright (c) 2004  The Trustees of Princeton University (Trustees).
+#
+# $Id$
+#
+
+# 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
+
+# parse options
+while getopts 'f' OPT ; do
+    case "$OPT" in
+        f) foreground=1 ;;
+    esac
+    shift $(($OPTIND-1))
+done
+
+# check if we are already running
+[ -f $pidfile ] && exit 1
+
+# daemonize
+if [ $foreground -eq 0 ] ; then
+    nohup nice -n $nice -- $0 $* -f >>$logfile 2>&1 &
+    exit 0
+fi
+
+# record PID
+trap "rm -f /var/run/vcached.pid && exit 255" EXIT
+echo $$ > /var/run/vcached.pid
+
+: ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
+test -e "$UTIL_VSERVER_VARS" || {
+    echo "Can not find util-vserver installation; aborting..."
+    exit 1
+}
+. "$UTIL_VSERVER_VARS"
+
+# take out the trash
+chattr -R -i "$VROOTDIR/.vtmp"
+rm -rf "$VROOTDIR/.vtmp"
+
+mkdir -p "$VROOTDIR/.vcache"
+mkdir -p "$VROOTDIR/.vtmp"
+
+# loop forever
+while : ; do
+    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
+           "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$VROOTDIR/.vtmp/v$i"
+           # move it to .vcache when complete
+           mv "$VROOTDIR/.vtmp/v$i" "$VROOTDIR/.vcache/v$i"
+           echo "$(date) v$i ready"
+       fi
+    done
+    echo "$(date) Sleeping for $period seconds"
+    sleep $period
+done