undoing previous change - thanks Daniel
[sliceimage.git] / vserver-reference.init
1 #!/bin/bash
2 #
3 # vserver-reference     Updates VServer reference
4 #
5 # Load before nm, vcached, and vservers
6 # chkconfig: 3 60 80
7 # description: Builds VServer reference image
8 #
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Copyright (C) 2004 The Trustees of Princeton University
11 #
12 # $Id$
13 #
14
15 case "$1" in
16     start|restart|reload)
17         ;;
18     stop|status)
19         exit 0
20         ;;
21     *)
22         echo $"Usage: $0 {start|stop|restart|status}"
23         exit 1
24         ;;
25 esac
26
27 # Source function library.
28 . /etc/init.d/functions
29
30 # VServer definitions
31 . /usr/lib/util-vserver/util-vserver-vars
32
33 # Save stdout and stderr
34 exec 3>&1
35 exec 4>&2
36
37 # Redirect stdout and stderr to a log file
38 exec >>/var/log/vserver-reference.log
39 exec 2>&1
40
41 echo "--- STARTING $(date) ---"  >&3 2>&4
42
43 # Parse PLC configuration
44 if [ -r /etc/planetlab/plc_config ] ; then
45     . /etc/planetlab/plc_config
46 else
47     PLC_NAME="PlanetLab"
48     PLC_SLICE_PREFIX="pl"
49     PLC_BOOT_HOST="boot.planet-lab.org"
50 fi
51
52 shopt -s nullglob
53
54 # Make sure the barrier bit is set
55 chmod 0000 "$__DEFAULT_VSERVERDIR"
56 setattr --barrier "$__DEFAULT_VSERVERDIR"
57
58 # Set the attribute to unlink so vclone does the right thing wrt to 
59 # conserving space by linking and not copying unified files.
60
61 if [ -f /proc/virtual/info ] ; then
62         x=$[ 16#`awk '/^VCIKernel:/ { print $2 }' /proc/virtual/info` ]; 
63         y=$[ (x >> 9) & 1]
64         if test "$y" = 1 ; then 
65                 # COW support
66                 find "$__DEFAULT_VSERVERDIR/.vref"/* -type f -print0 | xargs -0 setattr --iunlink
67         else 
68                 # no COW; 
69                 find "$__DEFAULT_VSERVERDIR/.vref"/* -type f -print0 | xargs -0 setattr --~iunlink
70         fi
71 fi
72
73
74 # Build reference images for system slices
75 # xxx if several instances of systemslices get installed, only one gets instanciated
76 for systemvserver in "$__DEFAULT_VSERVERDIR/.vstub/"*/*.cloned ; do
77     # e.g. NAME=planetflow
78     NAME=$(basename $systemvserver .cloned)
79     DIR=$(dirname $systemvserver)
80     # e.g. SLICEFAMILY=planetlab-f8-i386
81     SLICEFAMILY=$(cat $systemvserver)
82     # deduce the actual name used in .vref by replacing the first part of slice-family 
83     # (pldistro) with the slice name
84     # e.g. VREFNAME=planetflow-f8-i386
85     VREFNAME=$(echo $SLICEFAMILY | sed -e "s,^[^-]*-,$NAME-,")
86
87     # Copy base reference image
88     if [ ! -d "$__DEFAULT_VSERVERDIR/.vref/$VREFNAME" ] ; then
89         echo -n $"Building VServer reference image for $NAME in $VREFNAME: " >&3 2>&4
90
91         # Build in temporary directory
92         mkdir -p "$__DEFAULT_VSERVERDIR/.vtmp"
93         TMP=$(mktemp -d "$__DEFAULT_VSERVERDIR/.vtmp/$NAME.XXXXXX")
94         mkdir -p "$__DEFAULT_VSERVERDIR/.vref"
95
96         # build the systemvserver from the one it was originally cloned from
97         REF="$__DEFAULT_VSERVERDIR/.vref/$SLICEFAMILY"
98         if [ -d "$REF" ] ; then
99             "$_VCLONE" "$REF"/ "$TMP"/
100             RETVAL=$?
101         else
102             RETVAL=1
103         fi
104
105         # merge the stub with the reference to get the system vserver
106         if [ $RETVAL -eq 0 ] ; then
107             (cd "$DIR/$NAME"/ && find . | cpio -m -d -u -p "$TMP"/)
108             RETVAL=$?
109         fi
110
111         # Clean RPM state
112         rm -f "$TMP/var/lib/rpm/__db"*
113
114         # Move it to its permanent location when complete
115         if [ $RETVAL -eq 0 ] ; then
116             mv "$TMP" "$__DEFAULT_VSERVERDIR/.vref/$VREFNAME"
117             success >&3 2>&4
118         else
119             rm -rf "$TMP"
120             failure >&3 2>&4
121         fi
122         echo >&3 2>&4
123     fi
124 done
125
126 echo -n $"Updating VServer reference images: " >&3 2>&4
127
128 VROOTS="$__DEFAULT_VSERVERDIR/.vref/* $__DEFAULT_VSERVERDIR/.vcache/* $__DEFAULT_VSERVERDIR/${PLC_SLICE_PREFIX}_*"
129
130 # Copy configuration files from host to slices
131 for file in /etc/hosts /etc/resolv.conf /etc/planetlab/node_id \
132             /etc/planetlab/plc_config* /etc/planetlab/php/* \
133             /etc/pki/rpm-gpg/* ; do
134     if [ -r $file ] ; then
135         for vroot in $VROOTS ; do
136             install -D -m 644 $file $vroot/$file
137         done
138     fi
139 done
140
141 # Remove stale RPM locks
142 rm -f $vroot/var/lib/rpm/__db*
143
144 # (Re)install GPG signing keys
145 if [ -d /etc/pki/rpm-gpg ] ; then
146     for vroot in $VROOTS ; do
147         chroot $vroot rpm --allmatches -e gpg-pubkey || :
148         chroot $vroot rpm --import /etc/pki/rpm-gpg/* || :
149     done
150 fi
151
152 # Old versions of the Boot Manager copied portions of the Boot CD to
153 # /mnt/cdrom/bootme, to support old scripts which assumed that the
154 # Boot CD was mounted even in production mode. Now, it just copies it
155 # to /usr/boot/cacert.pem. In any case, copy the boot server
156 # certificate to the place(s) where BootServerRequest expects to find
157 # it (/usr/boot/cacert.pem by default, /mnt/cdrom/bootme in old
158 # versions).
159 CACERT="/usr/boot/cacert.pem /mnt/cdrom/bootme/cacert/$PLC_BOOT_HOST/cacert.pem"
160 for cacert in $CACERT ; do
161     if [ -r $cacert ] ; then
162         for vroot in $VROOTS ; do
163             # Install boot server certificate
164             install -D -m 644 $cacert $vroot/usr/boot/cacert.pem
165             echo $PLC_BOOT_HOST > $vroot/usr/boot/boot_server
166
167             # Also install in /mnt/cdrom/bootme for backward compatibility
168             install -D -m 644 $cacert $vroot/mnt/cdrom/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
169             echo $PLC_BOOT_HOST > $vroot/mnt/cdrom/bootme/BOOTSERVER
170         done
171         break
172     fi
173 done
174
175 success >&3 2>&4
176 echo "--- DONE $(date) ---"  >&3 2>&4
177 echo >&3 2>&4
178
179 exit 0