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