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