- zap immutable bit before nuking failed vserver build
[util-vserver.git] / scripts / vcached
1 #!/bin/bash
2 #
3 # vcached: VServer cache daemon
4 #
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.
9 #
10 # Based on work by:
11
12 # Brent Chun - bnc@intel-research.net
13 # Tristan Koo - tristan.koo@intel-research.net
14 # William Wung - wungism@uclink.berkeley.edu
15 #
16 # Mark Huang <mlhuang@cs.princeton.edu>
17 # Copyright (c) 2004  The Trustees of Princeton University (Trustees).
18 #
19 # $Id: vcached,v 1.4 2004/08/26 16:48:48 mlh-pl_kernel Exp $
20 #
21
22 # get configuration
23 . /etc/vcached.conf
24
25 # parse options
26 while getopts 'fd' OPT ; do
27     case "$OPT" in
28         f) foreground=1 ;;
29         d) debug=1 ;;
30     esac
31 done
32
33 # check if we are already running
34 [ -f $pidfile ] && exit 1
35
36 # daemonize
37 if [ $foreground -eq 0 ] ; then
38     nohup nice -n $nice -- $0 $* -f >>$logfile 2>&1 &
39     exit 0
40 fi
41
42 # record PID
43 trap "rm -f $pidfile ; exit 255" EXIT
44 echo $$ > $pidfile
45
46 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
47 test -e "$UTIL_VSERVER_VARS" || {
48     echo "Can not find util-vserver installation; aborting..."
49     exit 1
50 }
51 . "$UTIL_VSERVER_VARS"
52
53 # take out the trash
54 chattr -R -i "$VROOTDIR/.vtmp"
55 rm -rf "$VROOTDIR/.vtmp"
56
57 mkdir -p "$VROOTDIR/.vcache"
58 mkdir -p "$VROOTDIR/.vtmp"
59
60 # loop forever
61 while : ; do
62     [ $debug -ne 0 ] && echo "$(date) Checking the cache"
63     for i in $(seq 0 $(($slots - 1))) ; do
64         if [ ! -d "$VROOTDIR/.vcache/v$i" ] ; then
65             echo "$(date) Caching v$i"
66             # build image in .vtmp
67             TMP=$(mktemp -d "$VROOTDIR/.vtmp/v$i.XXXXXX")
68             "$PKGLIBDIR/vbuild" "$VROOTDIR/vserver-reference" "$TMP"
69             RETVAL=$?
70             # move it to .vcache when complete
71             if [ $RETVAL -eq 0 ] ; then
72                 mv "$TMP" "$VROOTDIR/.vcache/v$i"
73                 echo "$(date) v$i ready"
74             else
75                 echo "$(date) Error $RETVAL building v$i"
76                 chattr -R -i "$TMP"
77                 rm -rf "$TMP"
78             fi
79         fi
80     done
81     [ $debug -ne 0 ] && echo "$(date) Sleeping for $period seconds"
82     sleep $period
83 done