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