This commit was generated by cvs2svn to compensate for changes in r2562,
[util-vserver.git] / scripts / vserver-build.fai
diff --git a/scripts/vserver-build.fai b/scripts/vserver-build.fai
new file mode 100644 (file)
index 0000000..b59951c
--- /dev/null
@@ -0,0 +1,171 @@
+#
+# Copyright (C) 2006 Sam Vilain <sam.vilain@catalyst.net.nz>
+#  
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#  
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#  
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+tmp=$(getopt -o '+d:f:n:a' --long debug,pkgmgmt -n "$0" -- "$@") || exit 1
+eval set -- "$tmp"
+
+. "$_LIB_VSERVER_BUILD_FUNCTIONS_PKGMGMT"
+. "$__PKGLIBDIR/vserver.functions"
+
+DISTRIBUTION=:
+
+FAI_VSERVER=
+FAI_NFSROOT=/usr/lib/fai/nfsroot
+FAI_DIR=/usr/local/share/fai
+AUTO=1
+
+use_pkgmgmt=
+while true; do
+    case "$1" in
+       -f)             FAI_VSERVER=$2; shift; ;;
+       -n)             FAI_NFSROOT=$2; shift; ;;
+       -d)             FAI_DIR=$2; shift; ;;
+       -a)             AUTO=;;
+       --debug)        DEBUG=1; SH_DEBUG=-x; set -x;;
+       --)             shift; break ;;
+       *)              echo "vserver-build.fai: internal error: unrecognized option '$1'" >&2
+                       exit 1
+                       ;;
+    esac
+    shift
+done
+
+getDistribution '' 1
+
+base.init
+base.initFilesystem "$OPTION_FORCE"
+
+setup_writeOption "$VSERVER_NAME"
+setup_writeInitialFstab
+
+test -z "$BUILD_INITPRE"  || "$BUILD_INITPRE" "$SETUP_CONFDIR"  "$UTIL_VSERVER_VARS"
+
+# migrate is a 2.6 feature, we need 2.6 for the secure_mount ccap
+if ! $_VSERVER_INFO - FEATURE migrate; then
+    panic $"\
+The fai build method requires kernel features that are not present
+in your kernel."
+fi
+
+# if they specified a vserver where FAI is installed, then prefix its
+# root to the FAI_NFSROOT etc (unless they have already specified a
+# $ROOTDIR-relative path)
+REAL_ROOTDIR=`getPhysicalDir "$ROOTDIR"`
+REAL_VDIR=`getPhysicalDir "$VDIR"`
+echo $"ROOTDIR is $REAL_ROOTDIR"
+echo $"VDIR    is $REAL_VDIR"
+if [ -n "$FAI_VSERVER" ]
+then
+    echo "FAI_VSERVER is $FAI_VSERVER"
+    if [ `expr "$FAI_NFSROOT" : "$REAL_ROOTDIR"` -eq 0 ]
+    then
+        FAI_NFSROOT="$REAL_ROOTDIR/$FAI_VSERVER$FAI_NFSROOT"
+    fi
+    if [ `expr "$FAI_DIR" : "$REAL_ROOTDIR"` -eq 0 ]
+    then
+        FAI_DIR="$REAL_ROOTDIR/$FAI_VSERVER$FAI_DIR"
+    fi
+fi
+echo $"FAI_NFSROOT is $FAI_NFSROOT"
+echo $"FAI_DIR     is $FAI_DIR"
+FAI_ROOT=/tmp/target
+
+if [ ! -e "$FAI_NFSROOT/usr/sbin/fai" ]
+then
+    panic $"NFSROOT not found - did you run fai-setup?"
+fi
+
+if ! grep -qw VIRTUAL "$FAI_NFSROOT/usr/sbin/fai"
+then
+    panic $"\
+ERROR: Looks like you don't have the patched FAI.
+See http://faiwiki.informatik.uni-koeln.de/index.php/Using_FAI_as_a_vserver_build_method
+for information on where to get it."
+fi
+
+set -e
+
+# suppresses color logo, as it does not leave the terminal in a nice
+# state.
+console=dummy
+
+killprocs() {
+    set +e
+    S_CONTEXT=
+    if isVserverRunning "$SETUP_CONFDIR" S_CONTEXT
+    then
+       $_VKILL -c "$S_CONTEXT" -s 15
+       sleep 2
+       $_VKILL -c "$S_CONTEXT" -s 9
+       sleep 2
+       isVserverRunning "$SETUP_CONFDIR" && warning $"\
+Vserver '$VSERVER_NAME' still running unexpectedly; please investigate it
+manually."
+    fi
+    [ -n "$HAVE_INTERFACES" ] && disableInterfaces "$SETUP_CONFDIR"
+    base.__cleanup
+}
+
+trap killprocs INT EXIT
+
+# last chance to abort!
+if [ -z "$AUTO" ]
+then
+    echo -n "Press ENTER to begin, or Ctrl+C to cancel: "
+    read junk
+fi
+
+# set up interfaces, so that the vserver can be built with restricted
+# network access.
+generateOptions "$SETUP_CONFDIR"
+enableInterfaces "$SETUP_CONFDIR"
+HAVE_INTERFACES=1
+
+rwpaths="/var/lock /var/log /dev /var/run /etc/sysconfig"
+[ -d "$FAI_NFSROOT/etc/network/run" ] && rwpaths="$rwpaths /etc/network/run"
+
+export VDIR ROOTDIR FAI_VSERVER FAI_DIR FAI_NFSROOT FAI_ROOT console
+
+[ -z "$AUTO" ] && FINAL_SHELL="exec /bin/sh -i"
+
+# set up /mnt with all the mounts required
+$_SECURE_MOUNT -n -o ro,dev --bind "$FAI_NFSROOT" /mnt
+$_SECURE_MOUNT -n -t tmpfs -o size=64m,mode=1777 vm /mnt/tmp
+$_MKDIR -p /mnt/tmp/target
+$_SECURE_MOUNT -n -o rw,dev --bind "$VDIR" /mnt/tmp/target
+$_MKDIR -p /mnt/tmp/target/proc
+$_SECURE_MOUNT -n -t proc proc /mnt/tmp/target/proc
+for x in $rwpaths
+do
+    /mnt/usr/lib/fai/mkrw -n "/mnt$x"
+done
+$_SECURE_MOUNT -n -o ro --bind "$FAI_DIR" /mnt/fai
+$_SECURE_MOUNT -n -t proc proc /mnt/proc
+
+cd /mnt
+$_CHBIND "${CHBIND_OPTS[@]}" \
+    $_VCONTEXT --create "${OPTS_VCONTEXT_CREATE[@]}" -- \
+    $_VUNAME --xid self --set -t nodename="${SETUP_HOSTNAME:-fai-build}" \
+       -t context="$SETUP_CONFDIR" -- \
+    $_VATTRIBUTE --set --xid self "${OPTS_VATTRIBUTE[@]}" --ccap secure_mount \
+    $_SAVE_CTXINFO "$SETUP_CONFDIR" \
+    $_VCONTEXT --migrate-self --endsetup --chroot $SILENT_OPT \
+       "${OPTS_VCONTEXT_MIGRATE[@]}" "${OPTS_VCONTEXT_ENTER[@]}" -- \
+    /bin/sh -c "/bin/sh $SH_DEBUG /etc/init.d/rcS -N vschild_build; $FINAL_SHELL"
+
+test -z "$BUILD_INITPOST" || "$BUILD_INITPOST" "$SETUP_CONFDIR" "$UTIL_VSERVER_VARS"
+
+base.setSuccess