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