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