Setting tag VserverReference-4.2-14
[vserver-reference.git] / vserver-reference.init
index 4ac3e5a..410e807 100755 (executable)
 #!/bin/bash
 #
-# Builds VServer reference image. Requires the web and boot servers to
-# be up, which complicates bootstrap. Alternatively, we could require
-# the build server to host a local yum repository.
+# vserver-reference    Updates VServer reference
+#
+# Load before nm, vcached, and vservers
+# chkconfig: 3 60 80
+# description: Builds VServer reference image
 #
 # Mark Huang <mlhuang@cs.princeton.edu>
-# Copyright (C) 2004-2005 The Trustees of Princeton University
+# Copyright (C) 2004 The Trustees of Princeton University
 #
-# $Id: vserver-reference.init,v 1.9 2005/09/01 17:08:50 mlhuang Exp $
+# $Id$
 #
 
-YUM_CONF="http://www.planet-lab.org/PlanetLabConf/v3-yum.conf"
-
-# Make /vservers
-VROOT=$PWD/vservers/vserver-reference
-install -d -m 755 $VROOT
-
-MAKEDEV ()
-{
-    rm -rf $VROOT/dev
-    mkdir -p $VROOT/dev
-    mknod -m 666 $VROOT/dev/null c 1 3
-    mknod -m 666 $VROOT/dev/zero c 1 5
-    mknod -m 666 $VROOT/dev/full c 1 7
-    mknod -m 644 $VROOT/dev/random c 1 8
-    mknod -m 644 $VROOT/dev/urandom c 1 9
-    mknod -m 666 $VROOT/dev/tty c 5 0
-    mknod -m 666 $VROOT/dev/ptmx c 5 2
-    # For bash command substitution
-    ln -nsf ../proc/self/fd /dev/fd
-    # For df and linuxconf
-    touch $VROOT/dev/hdv1
-    # For TUN/TAP
-    mkdir -p $VROOT/dev/net
-    mknod -m 600 $VROOT/dev/net/tun c 10 200
-}
-
-# Initialize /dev in reference image
-MAKEDEV
-
-# Mount /dev/pts in reference image
-mkdir -p $VROOT/dev/pts
-mount -t devpts none $VROOT/dev/pts
-
-# Mount /proc in reference image
-mkdir -p $VROOT/proc
-mount -t proc none $VROOT/proc
-
-# Clean up before exiting if anything goes wrong
-trap "umount $VROOT/proc ; umount $VROOT/dev/pts ; exit 255" ERR
-
-# Create a dummy /etc/fstab in reference image
-mkdir -p $VROOT/etc
-cat > $VROOT/etc/fstab <<EOF
-# This fake fstab exists only to please df and linuxconf.
-/dev/hdv1      /       ext2    defaults        1 1
-EOF
-cp $VROOT/etc/fstab $VROOT/etc/mtab
-
-# Prevent all locales from being installed in reference image
-mkdir -p $VROOT/etc/rpm
-cat > $VROOT/etc/rpm/macros <<EOF
-%_install_langs en_US:en
-%_excludedocs 1
-%__file_context_path /dev/null
-EOF
-
-# This tells the Boot Manager that it is okay to update
-# /etc/resolv.conf and /etc/hosts whenever the network configuration
-# changes. Users are free to delete this file.
-touch $VROOT/etc/AUTO_UPDATE_NET_FILES
-
-# Trick rpm and yum, who read the real root /etc/rpm/macros file
-# rather than the one installed in the reference image, despite what
-# you might expect the --root and --installroot options to mean. Both
-# programs always read $HOME/.rpmmacros.
-export HOME=$PWD
-ln -sf $VROOT/etc/rpm/macros $PWD/.rpmmacros
-
-# Initialize RPM database in reference image
-mkdir -p $VROOT/var/lib/rpm
-rpm --root $VROOT --initdb
-
-# Go, baby, go
-yum -c $YUM_CONF --installroot=$VROOT -y groupinstall VServer
-
-# Freshen the RPM set with any just built. This does not help when a
-# completely new PlanetLab package must be installed in the reference
-# image. To work around this limitation, introduce the new package in
-# one release, then include it in the VServer yumgroup in the next.
-FRESHEN=$RPM_BUILD_DIR/../RPMS/*/*.rpm
-if [ -n "$FRESHEN" ] ; then
-    rpm --root $VROOT --freshen --verbose $FRESHEN
+case "$1" in
+    start|restart|reload)
+       ;;
+    stop|status)
+       exit 0
+       ;;
+    *)
+       echo $"Usage: $0 {start|stop|restart|status}"
+       exit 1
+       ;;
+esac
+
+# Source function library.
+. /etc/init.d/functions
+
+# VServer definitions
+init_paths="/usr/lib64/util-vserver/util-vserver-vars /usr/lib/util-vserver/util-vserver-vars"
+for init_path in $init_paths ; do
+    [ -f $init_path ] && { source $init_path ; break; }
+done
+[ "$PACKAGE_NAME" = "util-vserver" ] || echo "WARNING: could not find util-vserver init file among $init_paths"
+
+# Save stdout and stderr
+exec 3>&1
+exec 4>&2
+
+# Redirect stdout and stderr to a log file
+exec >>/var/log/vserver-reference.log
+exec 2>&1
+
+echo "--- STARTING $(date) ---"  >&3 2>&4
+
+# Parse PLC configuration
+if [ -r /etc/planetlab/plc_config ] ; then
+    . /etc/planetlab/plc_config
+else
+    PLC_NAME="PlanetLab"
+    PLC_SLICE_PREFIX="pl"
+    PLC_BOOT_HOST="boot.planet-lab.org"
 fi
 
-# Remove stale RPM locks
-rm -f $VROOT/var/lib/rpm/__db*
+shopt -s nullglob
+
+# Make sure the barrier bit is set
+chmod 0000 "$__DEFAULT_VSERVERDIR"
+setattr --barrier "$__DEFAULT_VSERVERDIR"
+
+# Set the attribute to unlink so vclone does the right thing wrt to 
+# conserving space by linking and not copying unified files.
+
+if [ -f /proc/virtual/info ] ; then
+       x=$[ 16#`awk '/^VCIKernel:/ { print $2 }' /proc/virtual/info` ]; 
+       y=$[ (x >> 9) & 1]
+       if test "$y" = 1 ; then 
+               # COW support
+               find "$__DEFAULT_VSERVERDIR/.vref"/* -type f -print0 | xargs -0 setattr --iunlink
+       else 
+               # no COW; 
+               find "$__DEFAULT_VSERVERDIR/.vref"/* -type f -print0 | xargs -0 setattr --~iunlink
+       fi
+fi
 
-# Clean up /dev in reference image
-umount $VROOT/dev/pts
-MAKEDEV
 
-# Disable all services in reference image
-chroot $VROOT /bin/sh -c "chkconfig --list | awk '{ print \$1 }' | xargs -i chkconfig {} off"
+# Build reference images for system slices
+# xxx if several instances of systemslices get installed, only one gets instanciated
+for systemvserver in "$__DEFAULT_VSERVERDIR/.vstub/"*/*.cloned ; do
+    # e.g. NAME=planetflow
+    NAME=$(basename $systemvserver .cloned)
+    DIR=$(dirname $systemvserver)
+    # e.g. SLICEFAMILY=planetlab-f8-i386
+    SLICEFAMILY=$(cat $systemvserver)
+    # deduce the actual name used in .vref by replacing the first part of slice-family 
+    # (pldistro) with the slice name
+    # e.g. VREFNAME=planetflow-f8-i386
+    VREFNAME=$(echo $SLICEFAMILY | sed -e "s,^[^-]*-,$NAME-,")
+
+    # Copy base reference image
+    if [ ! -d "$__DEFAULT_VSERVERDIR/.vref/$VREFNAME" ] ; then
+       echo -n $"Building VServer reference image for $NAME in $VREFNAME: " >&3 2>&4
+
+       # Build in temporary directory
+       mkdir -p "$__DEFAULT_VSERVERDIR/.vtmp"
+       TMP=$(mktemp -d "$__DEFAULT_VSERVERDIR/.vtmp/$NAME.XXXXXX")
+       mkdir -p "$__DEFAULT_VSERVERDIR/.vref"
+
+       # build the systemvserver from the one it was originally cloned from
+       REF="$__DEFAULT_VSERVERDIR/.vref/$SLICEFAMILY"
+       if [ -d "$REF" ] ; then
+           "$_VCLONE" "$REF"/ "$TMP"/
+           RETVAL=$?
+       else
+           RETVAL=1
+       fi
+
+       # merge the stub with the reference to get the system vserver
+       if [ $RETVAL -eq 0 ] ; then
+           (cd "$DIR/$NAME"/ && find . | cpio -m -d -u -p "$TMP"/)
+           RETVAL=$?
+       fi
+
+        # Clean RPM state
+       rm -f "$TMP/var/lib/rpm/__db"*
+
+       # Move it to its permanent location when complete
+       if [ $RETVAL -eq 0 ] ; then
+           mv "$TMP" "$__DEFAULT_VSERVERDIR/.vref/$VREFNAME"
+           success >&3 2>&4
+       else
+           rm -rf "$TMP"
+           failure >&3 2>&4
+       fi
+       echo >&3 2>&4
+    fi
+done
+
+echo -n $"Updating VServer reference images: " >&3 2>&4
+
+VROOTS="$__DEFAULT_VSERVERDIR/.vref/* $__DEFAULT_VSERVERDIR/.vcache/* $__DEFAULT_VSERVERDIR/${PLC_SLICE_PREFIX}_*"
+
+# Copy configuration files from host to slices
+for file in /etc/hosts /etc/resolv.conf /etc/planetlab/node_id \
+            /etc/planetlab/plc_config* /etc/planetlab/php/* \
+            /etc/pki/rpm-gpg/* ; do
+    if [ -r $file ] ; then
+       for vroot in $VROOTS ; do
+           install -D -m 644 $file $vroot/$file
+       done
+    fi
+done
+
+
+for vroot in $VROOTS ; do
+    # NOTE: without this step, the 'rpm --allmatches' step can hang on boot
+    # Remove stale RPM locks from all VROOT dirs
+    rm -f $vroot/var/lib/rpm/__db*
+done
+
+# (Re)install GPG signing keys
+if [ -d /etc/pki/rpm-gpg ] ; then
+    for vroot in $VROOTS ; do
+       chroot $vroot rpm --allmatches -e gpg-pubkey || :
+       chroot $vroot rpm --import /etc/pki/rpm-gpg/* || :
+    done
+fi
 
-# Clean up
-umount $VROOT/proc
+# Old versions of the Boot Manager copied portions of the Boot CD to
+# /mnt/cdrom/bootme, to support old scripts which assumed that the
+# Boot CD was mounted even in production mode. Now, it just copies it
+# to /usr/boot/cacert.pem. In any case, copy the boot server
+# certificate to the place(s) where BootServerRequest expects to find
+# it (/usr/boot/cacert.pem by default, /mnt/cdrom/bootme in old
+# versions).
+CACERT="/usr/boot/cacert.pem /mnt/cdrom/bootme/cacert/$PLC_BOOT_HOST/cacert.pem"
+for cacert in $CACERT ; do
+    if [ -r $cacert ] ; then
+       for vroot in $VROOTS ; do
+           # Install boot server certificate
+           install -D -m 644 $cacert $vroot/usr/boot/cacert.pem
+           echo $PLC_BOOT_HOST > $vroot/usr/boot/boot_server
+
+           # Also install in /mnt/cdrom/bootme for backward compatibility
+           install -D -m 644 $cacert $vroot/mnt/cdrom/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
+           echo $PLC_BOOT_HOST > $vroot/mnt/cdrom/bootme/BOOTSERVER
+       done
+       break
+    fi
+done
+
+success >&3 2>&4
+echo "--- DONE $(date) ---"  >&3 2>&4
+echo >&3 2>&4
 
 exit 0