The changeset revamps the vserver-reference package by changing the
[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 2>&1
39 exec &>>/var/log/vserver-reference.log
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 # Build reference images for system slices
59 for systemvserver in "$__DEFAULT_VSERVERDIR/.vstub/"*.cloned ; do
60     NAME=$(basename $systemvserver .cloned)
61
62     # Copy base reference image
63     if [ ! -d "$__DEFAULT_VSERVERDIR/.vref/$NAME" ] ; then
64         echo -n $"Building VServer reference image for $NAME: " >&3 2>&4
65
66         # Build in temporary directory
67         mkdir -p "$__DEFAULT_VSERVERDIR/.vtmp"
68         TMP=$(mktemp -d "$__DEFAULT_VSERVERDIR/.vtmp/$NAME.XXXXXX")
69         mkdir -p "$__DEFAULT_VSERVERDIR/.vref"
70
71         # build the systemvserver from the one it was originally cloned from
72         TYPE=$(cat $systemvserver)
73         REF="$__DEFAULT_VSERVERDIR/.vref/$TYPE"
74         "$_VCLONE" "$REF"/ "$TMP"/
75         RETVAL=$?
76
77         # merge the stub with the reference to get the system slice
78         if [ $RETVAL -eq 0 ] ; then
79             rsync -a "$__DEFAULT_VSERVERDIR/.vstub/$NAME"/ "$TMP"/
80             RETVAL=$?
81         fi
82
83         # Clean RPM state
84         rm -f "$TMP/var/lib/rpm/__db"*
85
86         # Move it to its permanent location when complete
87         if [ $RETVAL -eq 0 ] ; then
88             mv "$TMP" "$__DEFAULT_VSERVERDIR/.vref/$NAME"
89             success >&3 2>&4
90         else
91             failure >&3 2>&4
92         fi
93         echo >&3 2>&4
94     fi
95 done
96
97 echo -n $"Updating VServer reference images: " >&3 2>&4
98
99 VROOTS="$__DEFAULT_VSERVERDIR/.vref/* $__DEFAULT_VSERVERDIR/.vcache/* $__DEFAULT_VSERVERDIR/${PLC_SLICE_PREFIX}_*"
100
101 # Copy configuration files from host to slices
102 for file in /etc/hosts /etc/resolv.conf /etc/yum.conf /etc/planetlab/node_id \
103             /etc/planetlab/plc_config* /etc/planetlab/php/* \
104             /etc/pki/rpm-gpg/* ; do
105     if [ -r $file ] ; then
106         for vroot in $VROOTS ; do
107             install -D -m 644 $file $vroot/$file
108         done
109     fi
110 done
111
112 # Remove stale RPM locks
113 rm -f $vroot/var/lib/rpm/__db*
114
115 # (Re)install GPG signing keys
116 if [ -d /etc/pki/rpm-gpg ] ; then
117     for vroot in $VROOTS ; do
118         chroot $vroot rpm --allmatches -e gpg-pubkey || :
119         chroot $vroot rpm --import /etc/pki/rpm-gpg/* || :
120     done
121 fi
122
123 # Old versions of the Boot Manager copied portions of the Boot CD to
124 # /mnt/cdrom/bootme, to support old scripts which assumed that the
125 # Boot CD was mounted even in production mode. Now, it just copies it
126 # to /usr/boot/cacert.pem. In any case, copy the boot server
127 # certificate to the place(s) where BootServerRequest expects to find
128 # it (/usr/boot/cacert.pem by default, /mnt/cdrom/bootme in old
129 # versions).
130 CACERT="/usr/boot/cacert.pem /mnt/cdrom/bootme/cacert/$PLC_BOOT_HOST/cacert.pem"
131 for cacert in $CACERT ; do
132     if [ -r $cacert ] ; then
133         for vroot in $VROOTS ; do
134             # Install boot server certificate
135             install -D -m 644 $cacert $vroot/usr/boot/cacert.pem
136             echo $PLC_BOOT_HOST > $vroot/usr/boot/boot_server
137
138             # Also install in /mnt/cdrom/bootme for backward compatibility
139             install -D -m 644 $cacert $vroot/mnt/cdrom/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
140             echo $PLC_BOOT_HOST > $vroot/mnt/cdrom/bootme/BOOTSERVER
141         done
142         break
143     fi
144 done
145
146 success >&3 2>&4
147 echo "--- DONE $(date) ---"  >&3 2>&4
148 echo >&3 2>&4
149
150 exit 0