X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=scripts%2Fvcached;h=0e09c7e76ac29844f0b92771750262e67d3134ba;hb=a741f6faf2baae1e823d334012f6a09e6a1bda51;hp=e79b5b93990a5ca2a841a67a6bca97f34f526e5b;hpb=8a51c2aaf8fbdb032f528bdb75844a0f8772c1e6;p=util-vserver.git diff --git a/scripts/vcached b/scripts/vcached index e79b5b9..0e09c7e 100755 --- a/scripts/vcached +++ b/scripts/vcached @@ -1,11 +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. +# 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: # @@ -14,80 +14,93 @@ # William Wung - wungism@uclink.berkeley.edu # # Mark Huang -# Copyright (c) 2004 The Trustees of Princeton University (Trustees). +# Copyright (c) 2004-2005 The Trustees of Princeton University # -# $Id: vcached,v 1.1 2004/07/30 16:46:24 mlh-pl_kernel Exp $ +# $Id: vcached,v 1.14 2007/07/05 19:05:14 dhozac Exp $ # +PATH=/sbin:/usr/sbin:$PATH + # 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 +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 'fd' 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 ;; + h|*) usage ;; esac done -# check if we are already running -[ -f $pidfile ] && exit 1 +# append output to log file +exec 1>>$logfile +exec 2>>$logfile -# daemonize -if [ $foreground -eq 0 ] ; then - nohup nice -n $nice -- $0 $* -f >>$logfile 2>&1 & - exit 0 +# check if we are already running +if [ -f $pidfile ] && kill -0 `cat $pidfile` >/dev/null 2>&1 ; then + echo "vcached(`cat $pidfile`) already running" + exit 1 fi - -# record PID -trap "killall -q -w vbuild ; rm -f $pidfile ; exit 255" EXIT 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 } . "$UTIL_VSERVER_VARS" +# make sure barrier bit is set on /vservers to prevent chroot() escapes +setattr --barrier $__DEFAULT_VSERVERDIR + # 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 - [ $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 - "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$VROOTDIR/.vtmp/v$i" - # move it to .vcache when complete - mv "$VROOTDIR/.vtmp/v$i" "$VROOTDIR/.vcache/v$i" +#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") + "$_VCLONE" "$__DEFAULT_VSERVERDIR/.vref/default/" "$TMP"/ + RETVAL=$? + # move it to .vcache when complete + if [ $RETVAL -eq 0 ] ; then + 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 - [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds" - sleep $period + fi done + +exit 0